ValueError:无法强制系列,长度必须为 1:给定 300

发布于 2025-01-16 18:55:10 字数 1020 浏览 4 评论 0原文

请帮帮我...... 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2025-01-23 18:55:11

我能够毫无问题地运行您发布的代码,但鉴于您没有发布原始数据框的示例,因此很难提供太多建议。

根据错误消息,我怀疑您的数组之一的形状错误。也许尝试使用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文