ValueError: Number of labels=25 does not match number of samples=56
ValueError: Number of labels=25 does not match number of samples=56
我在
收到这个
C:/Users/HP/.PyCharmCE2019.1/config/scratches/scratch.py Traceback
(most recent call last):
File "C:/Users/HP/.PyCharmCE2019.1/config/scratches/scratch.py", line 25,
in dtree.fit(x_train,y_train)
File "C:\Users\HP\PycharmProjects\untitled\venv\lib\site-packages\sklearn\tree\tree.py",
line 801, in fit X_idx_sorted=X_idx_sorted)
File "C:\Users\HP\PycharmProjects\untitled\venv\lib\site-packages\sklearn\tree\tree.py",
line 236, in fit "number of samples=%d" % (len(y), n_samples))
ValueError: Number of labels=45 does not match number of samples=36
我正在使用 DecisionTree 模型,但出现错误。帮助将不胜感激。
#importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#reading the dataset
df=pd.read_csv(r'C:\csv\kyphosis.csv')
print(df)
print(df.head())
#visualising the dataset
print(sns.pairplot(df,hue='Kyphosis',palette='Set1'))
plt.show()
#training and testing
from sklearn.modelselection import traintestsplit
c=df.drop('Kyphosis',axis=1) d=df['Kyphosis']
xtrain,ytrain,xtest,ytest=traintestsplit(c,d,testsize=0.30)
#Decision_Tree
from sklearn.tree import DecisionTreeClassifier
dtree=DecisionTreeClassifier()
dtree.fit(xtrain,ytrain)
#Predictions
predictions=dtree.predict(xtest) from sklearn.metrics import
classificationreport,confusionmatrix
print(classificationreport(ytest,predictions))
print(confusionmatrix(y_test,predictions))
预期结果应该是我的 classification_report
和 confusion_matrix
所以,错误是由函数dtree.fit(xtrain, ytrain)
抛出的,因为xtrain
和ytrain
的长度不等。
检查生成它的代码部分:
xtrain,ytrain,xtest,ytest=traintestsplit(c,d,testsize=0.30)
中的示例进行比较
import numpy as np
from sklearn.model_selection import train_test_split
[...]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
你可以看到两件事:
1 traintestsplit
应该是 train_test_split
2 通过更改 =
左侧变量的顺序,您可以为这些变量分配不同的数据。
所以,您的代码应该是:
xtrain, xtest, ytrain, ytest = train_test_split(c,d,testsize=0.30)
我在
收到这个C:/Users/HP/.PyCharmCE2019.1/config/scratches/scratch.py Traceback (most recent call last):
File "C:/Users/HP/.PyCharmCE2019.1/config/scratches/scratch.py", line 25, in dtree.fit(x_train,y_train)
File "C:\Users\HP\PycharmProjects\untitled\venv\lib\site-packages\sklearn\tree\tree.py", line 801, in fit X_idx_sorted=X_idx_sorted)
File "C:\Users\HP\PycharmProjects\untitled\venv\lib\site-packages\sklearn\tree\tree.py", line 236, in fit "number of samples=%d" % (len(y), n_samples))
ValueError: Number of labels=45 does not match number of samples=36
我正在使用 DecisionTree 模型,但出现错误。帮助将不胜感激。
#importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#reading the dataset
df=pd.read_csv(r'C:\csv\kyphosis.csv')
print(df)
print(df.head())
#visualising the dataset
print(sns.pairplot(df,hue='Kyphosis',palette='Set1'))
plt.show()
#training and testing
from sklearn.modelselection import traintestsplit
c=df.drop('Kyphosis',axis=1) d=df['Kyphosis']
xtrain,ytrain,xtest,ytest=traintestsplit(c,d,testsize=0.30)
#Decision_Tree
from sklearn.tree import DecisionTreeClassifier
dtree=DecisionTreeClassifier()
dtree.fit(xtrain,ytrain)
#Predictions
predictions=dtree.predict(xtest) from sklearn.metrics import
classificationreport,confusionmatrix
print(classificationreport(ytest,predictions))
print(confusionmatrix(y_test,predictions))
预期结果应该是我的 classification_report
和 confusion_matrix
所以,错误是由函数dtree.fit(xtrain, ytrain)
抛出的,因为xtrain
和ytrain
的长度不等。
检查生成它的代码部分:
xtrain,ytrain,xtest,ytest=traintestsplit(c,d,testsize=0.30)
中的示例进行比较
import numpy as np from sklearn.model_selection import train_test_split [...] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
你可以看到两件事:
1 traintestsplit
应该是 train_test_split
2 通过更改 =
左侧变量的顺序,您可以为这些变量分配不同的数据。
所以,您的代码应该是:
xtrain, xtest, ytrain, ytest = train_test_split(c,d,testsize=0.30)