python - 如何将 pandas.core.series.Series 类型转换为二维数组?
问题描述
我正在尝试使用 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)
推荐阅读
- python - 为什么 python 在编辑列属性时比 r 快得多
- php - 如何从 PHP 中的 API 响应动态访问数组的值?
- css - 如何从我的网站中删除加载了脚本标签的 CSS 以及删除该脚本标签?
- python - 从 python 脚本重现 Dymola 信息层
- c# - C# 绘制所有 Y 轴相同 GridLine 位置的图表
- php - Laravel 调度器的日志
- vue.js - NativeScript 的 Calendar 组件上的圆角事件块
- mysql - 在 MySQL 中创建序列?
- python - 导入 win32api 时的问题
- angular - ngModel 'valid' 参数对于输入字段始终为真