【发布时间】:2015-11-18 03:22:10
【问题描述】:
我正在尝试使用 sklearn 库中的 SVM 来执行一些图像识别,但是当我调用 fit 方法时,我得到一个“ValueError: setting an array element with a sequence”。错误类型。我的代码如下。
我的 testing.py 文件:
import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
from imageToNumberArray import imageToNumberArray
classAndValuesFile = "../Classes_Values.txt"
classesFiles = "../"
testImage = "ImageToPerformTestOn.png"
x = []
y = []
def main():
i = 0
with open(classAndValuesFile) as f:
for line in f:
splitter = line.split(",", 2)
x.append(imageToNumberArray(classesFiles + splitter[0]))
y.append(splitter[1].strip())
clf = svm.SVC(gamma=0.001, C=100)
clf.fit(x,y)
#print clf.predict(testImage)
imageToNumberArray 文件为:
from PIL import Image
from numpy import array
def imageToNumberArray(path):
img = Image.open(path)
arr = array(img)
return arr
我收到以下错误:
Traceback (most recent call last):
File "D:\Research\project\testing.py", line 30, in <module>
main()
File "D:\Research\project\testing.py", line 23, in main
clf.fit(x,y)
File "C:\Python27\lib\site-packages\sklearn\svm\base.py", line 139, in fit
X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 344, in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.
如果我评论 clf.fit 行,它工作得很好。
另外,如果我打印 X 中矩阵的所有形状,我会得到类似这样的结果(有些是 2D,有些是 3D):
(59, 58, 4)
(49, 27, 4)
(570, 400, 3)
(471, 364)
(967, 729)
(600, 600, 3)
(325, 325, 3)
(386, 292)
(86, 36, 4)
(49, 26, 4)
(578, 244, 3)
(300, 300)
(995, 557, 3)
(1495, 677)
(400, 400, 3)
(200, 230, 3)
(74, 67, 4)
(49, 34, 4)
(240, 217, 3)
(594, 546, 4)
(387, 230, 3)
(297, 273, 4)
(400, 400, 3)
(387, 230, 3)
(86, 62, 4)
(50, 22, 4)
(499, 245, 3)
(800, 566, 4)
(1050, 750, 3)
(400, 400, 3)
(499, 245, 3)
(74, 53, 4)
(47, 26, 4)
(592, 348, 4)
(1050, 750, 3)
(1600, 1600)
(320, 320)
(84, 54, 4)
(47, 25, 4)
(600, 294, 3)
(400, 400, 3)
(1050, 750, 3)
(1478, 761)
(504, 300, 3)
(53, 84, 4)
(36, 42, 4)
(315, 600, 4)
(223, 425, 3)
(194, 325, 3)
前两个数字是图像的大小。
我该怎么做才能摆脱这个错误?
【问题讨论】:
-
在进行任何类型的机器学习之前,您几乎肯定希望从图像中提取特征(尽管我知道 KNN 可以很好地用于数字识别)。看看这个:codeproject.com/Articles/619039/…
-
或许this可以帮到你。
标签: python arrays machine-learning svm image-recognition