sklearn: Found input variables with inconsistent numbers of samples while using RandomForestClassifier

Viewed 69

I was working on a classification problem using RandomForestClassifier and a certain dataset, when I tried to run my program, an error message came showing:"Found input variables with inconsistent numbers of samples: [242, 61]". What should I do? The Code is:-

from sklearn.ensemble import RandomForestClassifier
np.random.seed(42)
X=heart_disease.drop('target',axis=1)
y=heart_disease['target']
X_train,y_train,X_test,y_test=train_test_split(X,y,test_size=0.2)
clf=RandomForestClassifier()
clf.fit(X_train, y_train)
clf.score(X_test,y_test)
1 Answers

That's because the return of the train_test_split is incorrect try this one out:

X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2)
Related