如何解决“找到的样本数量不一致的输入变量”?

问题描述

我在运行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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...