ValueError:无法强制系列,长度必须为 1:给定 300
请帮帮我...... ValueError:无法强制序列,长度必须为1:给定300
分割数据
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest = train_test_split(feature,target, test_size=0.3,random_state=101)
建模
from sklearn.neighbors import KNeighborsClassifier
knm = KNeighborsClassifier(n_neighbors=1)
knm.fit(xtrain,ytrain.values.ravel())
预测
Predictions = knm.predict(xtest)
结论
from sklearn.metrics import classification_report,confusion_matrix
print(classification_report(ytest,Predictions))
print(confusion_matrix(ytest,Predictions))
一切正常,但我从这里得到一个错误
error_rate = []
for i in range(1,40):
knm = KNeighborsClassifier(n_neighbors=i)
knm.fit(xtrain,ytrain.values.ravel())
pred_i = knm.predict(xtest)
error_rate.append(np.mean(pred_i != ytest))
Help Me, please...... ValueError: Unable to coerce to Series, the length must be 1: given 300
Splitting Data
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest = train_test_split(feature,target, test_size=0.3,random_state=101)
Modeling
from sklearn.neighbors import KNeighborsClassifier
knm = KNeighborsClassifier(n_neighbors=1)
knm.fit(xtrain,ytrain.values.ravel())
Predictions
Predictions = knm.predict(xtest)
Conclutions
from sklearn.metrics import classification_report,confusion_matrix
print(classification_report(ytest,Predictions))
print(confusion_matrix(ytest,Predictions))
Everything is ok but I got an error from here
error_rate = []
for i in range(1,40):
knm = KNeighborsClassifier(n_neighbors=i)
knm.fit(xtrain,ytrain.values.ravel())
pred_i = knm.predict(xtest)
error_rate.append(np.mean(pred_i != ytest))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够毫无问题地运行您发布的代码,但鉴于您没有发布原始数据框的示例,因此很难提供太多建议。
根据错误消息,我怀疑您的数组之一的形状错误。也许尝试使用
reshape()
来获得预期的形状?I was able to run the code you posted without problems, but given that you did not post an example of how your original dataframe looked like, it is hard to give much advice.
Based on the error message, I suspect one of your arrays have the wrong shape. Maybe try using
reshape()
to get the expected shape?