'"Expected sequence or array-like, got DecisionTreeClassifier" from sklearn

x_train,x_test,y_train,y_test=train_test_split(X,y,random_state=22)
model=DecisionTreeClassifier()
y_pred=model.fit(x_train,y_train)
print("precision: ",precision_score(y_test,y_pred))

Output: type error Expected sequence or array-like, got <class 'sklearn.tree._classes.DecisionTreeClassifier'>



Solution 1:[1]

model.fit() does not return the predictions. It fits the model and returns the model itself. I guess what you want is something like this instead:

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=22)
model = DecisionTreeClassifier()

model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print("precision: ", precision_score(y_test, y_pred))

I've followed the convention to capitalize X, because it is a 2-dimensional matrix, while y is a one-dimensional vector.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arne