Reputation: 65
I am trying to create a decision tree with a given data. But for some reason accuracy_score
gives
ValueError: Found input variables with inconsistent numbers of samples:
when i split my training data to validation(%20) and training(%80).
Here is how i split my data:
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
# stDt shuffled training set
stDt = shuffle(tDt)
#divide shuffled training set to training and validation set
stDt, vtDt = train_test_split(stDt,train_size=0.8, shuffle=False)
print(tDt.shape)
print(stDt.shape)
print(vtDt.shape)
Here is how i train data:
#attibutes and labels of training set
attributesT = stDt.values
labelsT = stDt.label
# Train Decision tree classifiers
from sklearn.tree import DecisionTreeClassifier
dtree1 = DecisionTreeClassifier(min_samples_split = 1.0)
dtree2 = DecisionTreeClassifier(min_samples_split = 3)
dtree3 = DecisionTreeClassifier(min_samples_split = 5)
fited1 = dtree1.fit(attributesT,labelsT)
fited2 = dtree2.fit(attributesT,labelsT)
fited3 = dtree3.fit(attributesT,labelsT)
Here is test and accuracy score part:
from sklearn.metrics import accuracy_score
ret1 = fited1.predict(stDt)
ret2 = fited2.predict(stDt)
ret3 = fited3.predict(stDt)
print(accuracy_score(vtDt.label,ret1))
Upvotes: 2
Views: 2634
Reputation: 60400
The error you get is expected, since you are trying to compare the predictions produced from your training set (ret1 = fited1.predict(stDt)
) to the labels of your validation set (vtDt.label
).
Here is the correct way to get both your training & validation accuracy for your fitted1
model (similarly for the others):
# predictions on the training set:
ret1 = fitted1.predict(stDt)
# training accuracy:
accuracy_score(stDt.label,ret1)
# predictions on the validation set:
pred1 = fitted1.predict(vtDt)
# validation accuracy:
accuracy_score(vtDt.label,pred1)
Upvotes: 3