Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
856 views
in Technique[技术] by (71.8m points)

classification - TypeError(Expected sequence or array-like) when assessing Decision Tree model in Python

I am working with Kaggle's Churn Modeling Dataset (https://www.kaggle.com/shrutimechlearn/churn-modelling), trying to predict customers who are going to leave the service.

The initial dataset looks like this:

RowNumber  CustomerId   Surname  CreditScore Geography Gender  Age  
 0          1    15634602  Hargrave          619    France  Female   42  

After wrangling the data, the dataset looks like this:

CreditScore Age Tenure  Balance NumOfProducts   HasCrCard   IsActiveMember  EstimatedSalary Germany Spain   Male
       0    619 42  2   0.00    1                  1        1               101348.88       0           0     0

I then split the data:

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y, test_size=0.25, random_state = 0)

... fit and predict the model:

from sklearn import tree
dectree_model = tree.DecisionTreeClassifier()
dectree_fit = dectree_model.fit(X_train, y_train)
dectree_prediction = dectree_fit.fit(X_train, y_train)

I then try to validate the model:

print(metrics.confusion_matrix(y_test, dectree_prediction))
print(metrics.classification_report(y_test, dectree_prediction))

but then I get this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-55-c8b0efbc711d> in <module>
      1 # Decision tree
----> 2 print(metrics.confusion_matrix(y_test, 
    dectree_prediction))
      3 print(metrics.classification_report(y_test, 
   dectree_prediction))
   ...
   TypeError: Expected sequence or array-like, got <class 'sklearn.tree._classes.DecisionTreeClassifier'>
question from:https://stackoverflow.com/questions/65863822/typeerrorexpected-sequence-or-array-like-when-assessing-decision-tree-model-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function you are calling, sklearn.metrics.confusion_matrix, expects you to feed y_true and y_pred - the ground truth and the estimated targets.

The dectree_prediction variable you provide for y_pred, however, is not the prediction of the estimator, but the estimator itself (that's what the fit function returns).

Seems like the actual predictions can be obtained with the dedicated predict function (as shown in this tutorial).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...