我正在嘗試將 SVC 與線性內核一起用于圖像識別任務。我當前的數據是一個 2x5 矩陣[['Face 1' 'Face2' 'Face 3' 'Face 4' 'Face 5'] ['229.0' '230.0' '231.0' '230.0' '230.0']]我的第二行是我的 X 特征,它們是來自不同圖像的像素強度值。我的第一行是我的 Y 標簽,它們是從中提取像素的面部圖像。我正在嘗試不惜一切代價將我的數據輸入 SVC。我正在嘗試的是: m_array = [[229, 230, 231, 230, 230]] faces = [] faces = np.asarray(['Face 1', 'Face2', 'Face 3', 'Face 4', 'Face 5']).reshape(-1, 5) data = np.stack((faces, m_array)).reshape(2, 5) df = pd.DataFrame(data) X = data[1, :] Y = data[0, :]from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)from sklearn.svm import SVCsvclassifier = SVC(kernel='linear')svclassifier.fit(X_train, y_train)我想測試這些功能的識別率,但出現錯誤:類型錯誤:單例數組 array(162) 不能被視為有效集合。
1 回答

偶然的你
TA貢獻1841條經驗 獲得超3個贊
sklearn 期望您的 X_train 數組是一個二維數組,例如 (n_examples, 1),而 Y_train 是一維標簽數組,例如 (n_examples, )。
我重新格式化了您的代碼以刪除一些不必要的步驟并解決了問題:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
m_array = np.array([229, 230, 231, 230, 230])[:, np.newaxis]
faces = np.array(['Face 1', 'Face2', 'Face 3', 'Face 4', 'Face 5'])
X_train, X_test, y_train, y_test = train_test_split(m_array, faces, test_size = 0.20)
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)
添加回答
舉報
0/150
提交
取消