df = pd.DataFrame() df['model'] = model df['Roc_auc'] = roc_auc df['train_score'] = train_score df['test_score'] = test_score df Out[74]: model Roc_auc train_score test_score 0 DecisionTree 0.832536 0.982522 0.844444 1 KNeighbors 0.661085 0.717853 0.700000
时间: 2024-03-04 20:53:27 浏览: 75
这段代码中,首先创建了一个空的DataFrame对象,然后将模型、ROC曲线下面积,训练集得分和测试集得分分别作为数据列添加到DataFrame中。最后,通过打印输出DataFrame,展示了所有模型在不同评价指标下的表现情况,包括在训练集和测试集上的得分以及使用ROC曲线下面积作为评价指标的预测结果。从输出结果可以看出,DecisionTree模型在所有指标下的表现均优于KNeighbors模型。
相关问题
# 导入相关库 import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score,roc_auc_score,roc_curve # 读取数据 df = pd.read_csv('C:/Users/E15/Desktop/机器学习作业/第一次作业/第一次作业/三个数据集/Titanic泰坦尼克号.csv') # 数据预处理 df = df.drop(["Name", "Ticket", "Cabin"], axis=1) # 删除无用特征 df = pd.get_dummies(df, columns=["Sex", "Embarked"]) # 将分类特征转换成独热编码 df = df.fillna(df.mean()) # 使用平均值填充缺失值 # 划分数据集 X = df.drop(["Survived"], axis=1) y = df["Survived"] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 决策树 dtc = DecisionTreeClassifier(random_state=42) dtc.fit(X_train, y_train) y_pred_dtc = dtc.predict(X_test) # 剪枝决策树 pruned_dtc = DecisionTreeClassifier(random_state=42, ccp_alpha=0.015) pruned_dtc.fit(X_train, y_train) y_pred_pruned_dtc = pruned_dtc.predict(X_test) # 随机森林 rfc = RandomForestClassifier(n_estimators=100, random_state=42) rfc.fit(X_train, y_train) y_pred_rfc = rfc.predict(X_test) # 计算评价指标 metrics = {"Accuracy": accuracy_score, "Precision": precision_score, "Recall": recall_score, "F1-Score": f1_score, "AUC": roc_auc_score} results = {} for key in metrics.keys(): if key == "AUC": results[key] = {"Decision Tree": roc_auc_score(y_test, y_pred_dtc), "Pruned Decision Tree": roc_auc_score(y_test, y_pred_pruned_dtc), "Random Forest": roc_auc_score(y_test, y_pred_rfc)} else: results[key] = {"Decision Tree": metrics[key](y_test, y_pred_dtc), "Pruned Decision Tree": metrics[key](y_test, y_pred_pruned_dtc), "Random Forest": metrics[key](y_test, y_pred_rfc)} # 打印评价指标的表格 results_df = pd.DataFrame(results) print(results_df)怎么打印auv图
要打印AUC图,可以使用`roc_curve`函数获取ROC曲线的参数,然后使用`matplotlib`库绘制曲线。具体代码如下:
```
# 计算ROC曲线参数
fpr_dtc, tpr_dtc, thresholds_dtc = roc_curve(y_test, y_pred_dtc)
fpr_pruned_dtc, tpr_pruned_dtc, thresholds_pruned_dtc = roc_curve(y_test, y_pred_pruned_dtc)
fpr_rfc, tpr_rfc, thresholds_rfc = roc_curve(y_test, y_pred_rfc)
# 绘制ROC曲线
plt.figure(figsize=(8, 6))
plt.plot(fpr_dtc, tpr_dtc, label='Decision Tree')
plt.plot(fpr_pruned_dtc, tpr_pruned_dtc, label='Pruned Decision Tree')
plt.plot(fpr_rfc, tpr_rfc, label='Random Forest')
plt.plot([0, 1], [0, 1], linestyle='--', color='grey')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend()
plt.show()
```
这段代码将会绘制一个ROC曲线,其中每个分类器都使用不同的颜色表示,同时包含一个虚线表示随机分类器的结果。注意,这段代码需要在之前的代码块中运行,以确保`fpr`,`tpr`和`y_pred`参数已经存在。
# 导入模块 import prettytable as pt from sklearn.metrics import accuracy_score from sklearn.metrics import precision_score from sklearn.metrics import recall_score, f1_score from sklearn.metrics import roc_curve, auc # 创建表格对象 table = pt.PrettyTable() # 设置表格的列名 table.field_names = ["acc", "precision", "recall", "f1", "roc_auc"] # 循环添加数据 # 20个随机状态 for i in range(1): # # GBDT GBDT = GradientBoostingClassifier(learning_rate=0.1, min_samples_leaf=14, min_samples_split=6, max_depth=10, random_state=i, n_estimators=267 ) # GBDT = GradientBoostingClassifier(learning_rate=0.1, n_estimators=142,min_samples_leaf=80,min_samples_split=296,max_depth=7 , max_features='sqrt', random_state=66 # ) GBDT.fit(train_x, train_y) y_pred = GBDT.predict(test_x) # y_predprob = GBDT.predict_proba(test_x) print(y_pred) print('AUC Score:%.4g' % metrics.roc_auc_score(test_y.values, y_pred)) # print('AUC Score (test): %f' %metrics.roc_auc_score(test_y.values,y_predprob[:,1])) accuracy = GBDT.score(val_x, val_y) accuracy1 = GBDT.score(test_x, test_y) print("GBDT最终精确度:{},{}".format(accuracy, accuracy1)) y_predict3 = GBDT.predict(test_x) get_score(test_y, y_predict3, model_name='GBDT') acc = accuracy_score(test_y, y_predict3) # 准确率 prec = precision_score(test_y, y_predict3) # 精确率 recall = recall_score(test_y, y_predict3) # 召回率 f1 = f1_score(test_y, y_predict3) # F1 fpr, tpr, thersholds = roc_curve(test_y, y_predict3) roc_auc = auc(fpr, tpr) data1 = acc data2 = prec data3 = recall data4 = f1 data5 = roc_auc # 将数据添加到表格中 table.add_row([data1, data2, data3, data4, data5]) print(table) import pandas as pd # 将数据转换为DataFrame格式 df = pd.DataFrame(list(table), columns=["acc","prec","recall","f1","roc_auc"]) # 将DataFrame写入Excel文件 writer = pd.ExcelWriter('output.xlsx') df.to_excel(writer, index=False) writer.save(),出现上面的错误怎样更正
根据错误提示可以看出是因为缺少了sklearn库中的metrics模块,需要在开头添加如下代码:
```python
from sklearn import metrics
```
另外,在代码中出现了get_score函数的调用,但是并没有定义该函数,需要先定义该函数再进行调用。
阅读全文