繁体   English   中英

如何将pandas.core.series.Series类型转换为2D数组?

[英]How to convert pandas.core.series.Series type to 2D array?

我正在尝试使用KNNClassifier训练模型。 我将数据拆分如下:

X_train, X_test, y_train, y_test = train_test_split(X_bow, y, test_size=0.30, random_state=42)

y_train= y_train.astype('int')

neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, y_train)

尝试测试时,出现值错误。

pre = neigh.predict(y_test)

Expected 2D array, got 1D array instead:
array=[0. 1. 1. ... 0. 0. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a 
single feature or array.reshape(1, -1) if it contains a single sample.

我的y_test是pandas.core.series.Series类型

那么,如何将pandas.core.series.Series转换为2D数组以使此测试有效?

我试图将y_test转换为数据帧,然后转换为数组,但是出现另一个值错误,我被卡住了。

y_test = pd.DataFrame(y_test)
y_test = y_test.as_matrix().reshape(-1,1)
pre = neigh.predict(y_test)

ValueError: Incompatible dimension for X and Y matrices: X.shape[1] == 1 while Y.shape[1] == 6038

我猜您需要使用X_test变量/数组,而不是y_test

X_test是用于测试模型准确性的independent变量/特征, y_test是将与预测值进行比较的实际target

例:

pre = neigh.predict(X_test)

要测量准确性:

from sklearn.metrics import accuracy_score
accuracy_score(y_test, pre)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM