如何解决如何解决“找到的样本数量不一致的输入变量”?
我在运行category_report和confussion_matrix代码时遇到错误。我不知道要解决它,我已经在google上寻找了,但还是没有。如果有人可以帮助我,我非常感谢:)和 这是我的完整代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
# reading csv file and extracting class column to y.
data = pd.read_csv("grapeleaves.csv")
# extracting two features
X = data.drop(['imgid','fold num'],axis=1)
y = X['label']
X = X.drop('label',axis=1)
print("\nTraining dataset:-\n")
print(X)
log = pd.read_csv("data_uji.csv")
log = log.tail(1)
X_ul = log.drop(['imgid',axis=1)
print("\nTest dataset:-\n")
print(X_ul)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.20)
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train,y_train)
pred = knn.predict(X_ul)
print(pred)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,pred))
print(classification_report(y_test,pred))
这是错误输出
ValueError Traceback (most recent call last)
<ipython-input-95-e2dee3e604ea> in <module>
34
35 from sklearn.metrics import classification_report,confusion_matrix
---> 36 print(confusion_matrix(y_test,pred))
37 print(classification_report(y_test,pred))
~\Ana3\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true,y_pred,labels,sample_weight)
251
252 """
--> 253 y_type,y_true,y_pred = _check_targets(y_true,y_pred)
254 if y_type not in ("binary","multiclass"):
255 raise ValueError("%s is not supported" % y_type)
~\Ana3\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true,y_pred)
69 y_pred : array or indicator matrix
70 """
---> 71 check_consistent_length(y_true,y_pred)
72 type_true = type_of_target(y_true)
73 type_pred = type_of_target(y_pred)
~\Ana3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
233 if len(uniques) > 1:
234 raise ValueError("Found input variables with inconsistent numbers of"
--> 235 " samples: %r" % [int(l) for l in lengths])
236
237
ValueError: Found input variables with inconsistent numbers of samples: [44,1]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。