2

I have this code:

X, y = make_classification(n_features=2,n_redundant=0,n_samples=400, random_state=17)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=17)

clf = DecisionTree(max_depth=4, criterion='gini')
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
prob_pred = clf.predict_proba(X_test)
accuracy = accuracy_score(y_test,y_pred)

However, there is an error Expected array-like (array or non-string sequence), got None in the last line accuracy = accuracy_score(y_test,y_pred). How can I fix it?

3
  • y_test and/or y_pred are None, debug your program and see where you don't get the expected values. Commented Jan 5, 2023 at 6:50
  • @Guy, you are absolutely right, I have 'None' in 'y_pred'. How to solve? Commented Jan 5, 2023 at 7:00
  • Do you mean DecisionTreeClassifier? Commented Jan 5, 2023 at 7:15

1 Answer 1

1

Your code with a minor fix works well:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

X, y = make_classification(n_features=2,n_redundant=0,n_samples=400, random_state=17)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=17)

clf = DecisionTreeClassifier(max_depth=4, criterion='gini')  # Not DecisionTree
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
prob_pred = clf.predict_proba(X_test)
accuracy = accuracy_score(y_test,y_pred)

Output:

>>> accuracy
0.8833333333333333
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.