如何解决在 Python 中评估决策树模型时出现类型错误预期序列或类似数组
我正在使用 kaggle 的流失建模数据集 (https://www.kaggle.com/shrutimechlearn/churn-modelling),试图预测将要离开服务的客户。
初始数据集如下所示:
RowNumber CustomerId Surname Creditscore Geography Gender Age
0 1 15634602 Hargrave 619 France Female 42
整理数据后,数据集如下所示:
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
然后我拆分数据:
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)
...拟合和预测模型:
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)
然后我尝试验证模型:
print(metrics.confusion_matrix(y_test,dectree_prediction))
print(metrics.classification_report(y_test,dectree_prediction))
但后来我收到此错误:
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'>
解决方法
您正在调用的函数 sklearn.metrics.confusion_matrix 期望您提供 y_true 和 y_pred - 基本事实和估计目标。
然而,您为 y_pred 提供的 dectree_prediction 变量不是估算器的预测,而是估算器本身(这就是 fit 函数返回的内容)。
似乎可以使用专用的预测功能(如this tutorial所示)获得实际预测。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。