向 sklearn.svm.SVC() 提供数据
Providing data to sklearn.svm.SVC()
我正在尝试将这些训练数据提供给 sklearn.svm.SVC()
,但是当我尝试 clf.fit(v,v2)
时 returns 出现了错误 ValueError: setting an array element with a sequence.
。在将这些数据提供给 SVC()
之前,我们如何处理这些数据?
from PIL import Image
from sklearn import svm
for i in xrange(1,55):
t = list(Image.open("train/"+str(i)+".png").getdata())
v.append(t)
v = np.asarray(v)
v2 = np.array(["1","F","9","D","E","E","E","9","0","D","0","3","C","B","F","9","A","E","B","8","A","8","7",
"9","9","3","C","6","1","E","6","6","C","C","F","A","8","0","1","F","F","E","9","4","6","0",
"7","2","D","9","A","C","7","E"])
clf = svm.SVC()
我想你正在寻找这样的东西:
from scipy import misc
import glob
from sklearn import svm
filenames = glob.glob('train/*.png')
X = [misc.imread(each).flatten() for each in filenames]
y = ["1","F","9","D","E","E", ...]
model = svm.SVC().fit(X, y)
备注:
X
的形式为 (n_images, n_pixels)
其中 n_pixels=width*height
y
的长度为 n_images
(在您的示例中为 54)
这只是一个开始,您应该尝试为分类器提供比单个像素更有意义的特征。
我正在尝试将这些训练数据提供给 sklearn.svm.SVC()
,但是当我尝试 clf.fit(v,v2)
时 returns 出现了错误 ValueError: setting an array element with a sequence.
。在将这些数据提供给 SVC()
之前,我们如何处理这些数据?
from PIL import Image
from sklearn import svm
for i in xrange(1,55):
t = list(Image.open("train/"+str(i)+".png").getdata())
v.append(t)
v = np.asarray(v)
v2 = np.array(["1","F","9","D","E","E","E","9","0","D","0","3","C","B","F","9","A","E","B","8","A","8","7",
"9","9","3","C","6","1","E","6","6","C","C","F","A","8","0","1","F","F","E","9","4","6","0",
"7","2","D","9","A","C","7","E"])
clf = svm.SVC()
我想你正在寻找这样的东西:
from scipy import misc
import glob
from sklearn import svm
filenames = glob.glob('train/*.png')
X = [misc.imread(each).flatten() for each in filenames]
y = ["1","F","9","D","E","E", ...]
model = svm.SVC().fit(X, y)
备注:
X
的形式为(n_images, n_pixels)
其中n_pixels=width*height
y
的长度为n_images
(在您的示例中为 54)
这只是一个开始,您应该尝试为分类器提供比单个像素更有意义的特征。