我查看了其他答案,但仍然無法理解為什么問題仍然存在。Iris 數(shù)據(jù)集的經(jīng)典機器學習實踐。代碼:dataset=load_iris()X = np.array(dataset.data)y = np.array(dataset.target)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)model = KNeighborsClassifier()model.fit(X_train, y_train)prediction=model.predict(X_test)所有數(shù)組的形狀:X 形狀:(150, 4)y 形狀:(150,)X_train: (105, 4)X_test: (45, 4)y_train: (105,)y_test(45,)預測:(45,)試圖打印這個model.score(y_test, prediction)我得到了錯誤。我嘗試使用 .reshape(-1,1) 將 y_test 和預測轉(zhuǎn)換為二維數(shù)組,但出現(xiàn)另一個錯誤:查詢數(shù)據(jù)維度必須與訓練數(shù)據(jù)維度匹配。這不僅關乎解決方案,還關乎理解問題所在。
1 回答

GCT1015
TA貢獻1827條經(jīng)驗 獲得超4個贊
查看您使用的函數(shù)的簽名和文檔字符串通常很有用。model.score例如有
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Test samples.
y : array-like, shape = (n_samples) or (n_samples, n_outputs)
True labels for X.
在文檔字符串中向您顯示您應該提供的確切輸入類型。
在這里你會做model.score(X_test, y_test)
model.score將同時進行預測X_test和比較y_test
添加回答
舉報
0/150
提交
取消