0
我試圖使用帶有1022圖像和1022多類標籤(每個標籤有14個類)的python在SVM中使用SVM進行圖像分類。在訓練SVM對圖像進行分類時設置具有序列錯誤的數組元素
mypath = 'path'
k = listdir(mypath)
images = np.empty((len(k)-1), dtype=object)
resized_imgs = np.empty((len(k)-1),dtype=object)
for n in range(0, len(k)-1):
images[n] = cv2.imread(join(mypath,k[n]),0) #Reading images in grayscale
resized_imgs[n] = cv2.resize(images[n],(32,32)) #Resizing images
for i in range(0,len(k)-1):
a=resized_imgs[i].mean()
b=resized_imgs[i].std()
t=np.ndarray([32,32])
t.fill(1)
t=t*a
resized_imgs[i]=(resized_imgs[i]-a)/b
X_train = resized_imgs
for i in range(len(k)-1):
X_train[i] = X_train[i].flatten().tolist()
y_train = np.array(y_train)
for i in range(len(k)-1):
y_train[i] = y_train[i].flatten().tolist()
clf=svm.SVC(gamma=0.001)
clf.fit(X_train,y_train)
現在,爲了這個,我遇到了錯誤:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-469-bfab446776df> in <module>()
----> 1 clf.fit(X_train,y_train[:,0])
C:\Users\user\Anaconda3\lib\site-packages\sklearn\svm\base.py in fit(self, X, y, sample_weight)
148 self._sparse = sparse and not callable(self.kernel)
149
--> 150 X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
151 y = self._validate_targets(y)
152
C:\Users\user\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
371 force_all_finite)
372 else:
--> 373 array = np.array(array, dtype=dtype, order=order, copy=copy)
374
375 if ensure_2d:
ValueError: setting an array element with a sequence.
我看不出這可能是由於X_train的數組,因爲尺寸不匹配X_train的每個元素是1024個標準化像素值的列表,並且y_train的每個元素對應於14個屬性的列表。
任何幫助,將不勝感激。謝謝!