首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >决策树的准确性评分

决策树的准确性评分
EN

Stack Overflow用户
提问于 2022-05-30 17:20:51
回答 1查看 318关注 0票数 0

第1部分

代码语言:javascript
运行
复制
decision_tree.fit(X_train, y_train)
Y_val = decision_tree.predict(X_val)
acc_decision_tree_train = round(decision_tree.score(X_train, y_train) * 100, 2)
acc_decision_tree_train

第2部分

代码语言:javascript
运行
复制
acc_decision_tree_val = round(decision_tree.score(X_val, y_val) * 100, 2)
print('accuracy:', acc_decision_tree_val)

第三部分

代码语言:javascript
运行
复制
con_mat=confusion_matrix(y_val, Y_pred_val)
sns.heatmap(con_mat,annot=True,annot_kws= {"size":20},cmap="viridis")
plt.show()

第四部分

代码语言:javascript
运行
复制
acc_decision_tree_test = round(decision_tree.score(X_test, y_test) * 100, 2)
print('accuracy:', acc_decision_tree_test)
Y_pred_test = decision_tree.predict(X_test)

以上代码分为4部分。

Q1 ->在列车上的拟合和Val上的预测,在这一步骤中,模型通过对训练数据x_train的拟合来学习,但是我们并没有进行任何预测来获得y_train,所以在这种情况下,如何才能得到列车预测的准确性评分(模型正在学习,对吗?)Q2 ->In第2部分,正如我们前面所做的"Y_val = decision_tree.predict(X_val)“一样,我们可以计算验证的分数,这个分数是否与混淆矩阵中的准确性度量相同。Q3->在第4部分中,我只是要求测试数据的准确性评分,但是我没有对测试数据执行任何“预测”,但是它如何能够给我评分而不进行预测。

如果有什么不清楚的地方,请告诉我,谢谢:)

EN

回答 1

Stack Overflow用户

发布于 2022-05-31 13:26:41

我调整了代码片段,我得到了虹膜数据集的准确性。

代码语言:javascript
运行
复制
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix  
import matplotlib.pyplot as plt
import seaborn as sns

decision_tree = DecisionTreeClassifier(random_state=0)
iris = load_iris()

X_train, X_test, y_train, y_test= train_test_split(iris.data, iris.target, test_size= 0.25, random_state=0) 
X_train, X_val, y_train, y_val= train_test_split(X_train, y_train, test_size= 0.25, random_state=0) 

#score = cross_val_score(decision_tree, iris.data, iris.target, cv=10)

decision_tree.fit(X_train, y_train)
y_pred_val = decision_tree.predict(X_val)
acc_decision_tree_train = round(decision_tree.score(X_train, y_train) * 100, 2)
print("acc_decision_tree_train ", acc_decision_tree_train)

acc_decision_tree_val = round(decision_tree.score(X_val, y_val) * 100, 2)
print('accuracy:', acc_decision_tree_val)

con_mat=confusion_matrix(y_val, y_pred_val)
sns.heatmap(con_mat,annot=True,annot_kws= {"size":20},cmap="viridis")
plt.show()

acc_decision_tree_test = round(decision_tree.score(X_test, y_test) * 100, 2)
print('accuracy:', acc_decision_tree_test)
y_pred_test = decision_tree.predict(X_test)

输出:

代码语言:javascript
运行
复制
acc_decision_tree_train 100.0
accuracy: 100.0
accuracy: 97.37

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72437992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档