Accuracy_score:0.995565 Precision_score:0.98370536854472908 Recall_score:0.971350751234545 F1_score:0.977489 Auc:0.9887765786
时间: 2024-06-07 12:09:26 浏览: 144
这是一个分类模型的评估指标结果。Accuracy_score表示模型的准确率,Precision_score表示模型的精确率,Recall_score表示模型的召回率,F1_score是综合考虑了精确率和召回率的指标,Auc表示模型的曲线下面积。从这些指标的值来看,这个分类模型表现不错,准确率高,精确率和召回率也比较均衡。
相关问题
accuracy_lst_rfc = [] precision_lst_rfc = [] recall_lst_rfc = [] f1_lst_rfc = [] auc_lst_rfc = [] rfc_sm = RandomForestClassifier() #rfc_params = {} rfc_params = {'max_features' : ['auto', 'sqrt', 'log2'], 'random_state' : [42], 'class_weight' : ['balanced','balanced_subsample'], 'criterion' : ['gini', 'entropy'], 'bootstrap' : [True,False]} rand_rfc = RandomizedSearchCV(rfc_sm, rfc_params, n_iter=4) for train, val in sss.split(X_train_sm, y_train_sm): pipeline_rfc = imbalanced_make_pipeline(SMOTE(sampling_strategy='minority'), rand_rfc) # SMOTE happens during Cross Validation not before.. model_rfc = pipeline_rfc.fit(X_train_sm, y_train_sm) best_est_rfc = rand_rfc.best_estimator_ prediction_rfc = best_est_rfc.predict(X_train_sm[val]) accuracy_lst_rfc.append(pipeline_rfc.score(X_train_sm[val], y_train_sm[val])) precision_lst_rfc.append(precision_score(y_train_sm[val], prediction_rfc)) recall_lst_rfc.append(recall_score(y_train_sm[val], prediction_rfc)) f1_lst_rfc.append(f1_score(y_train_sm[val], prediction_rfc)) auc_lst_rfc.append(roc_auc_score(y_train_sm[val], prediction_rfc)) print('---' * 45) print('') print("accuracy: {}".format(np.mean(accuracy_lst_rfc))) print("precision: {}".format(np.mean(precision_lst_rfc))) print("recall: {}".format(np.mean(recall_lst_rfc))) print("f1: {}".format(np.mean(f1_lst_rfc))) print('---' * 45)
这段代码主要是利用随机搜索(RandomizedSearchCV)和交叉验证(Cross Validation)来对随机森林(RandomForestClassifier)的参数进行优化,并计算模型在训练集上的各项指标。
具体来说,代码首先定义了一些空列表,用于保存每次交叉验证后模型的指标。接着,定义了一个随机森林分类器(rfc_sm),并设置了一些可能需要调整的参数(rfc_params),这些参数将会在随机搜索中进行优化。然后,使用RandomizedSearchCV构造了一个带有随机森林分类器和随机搜索优化器的管道(pipeline_rfc),并将其作为模型进行训练。注意,在管道中,使用了SMOTE算法对训练集进行了过采样处理,以解决数据不平衡的问题。
接下来,使用交叉验证对训练集进行了划分,并对每个验证集进行了预测,同时记录了各项指标的值,并打印出了平均值。
最后,需要注意的是,代码中使用的各种指标函数(precision_score、recall_score、f1_score、roc_auc_score)都是来自于sklearn库,它们的参数含义与数学定义略有不同,需要注意。
# 导入模块 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函数的调用,但是并没有定义该函数,需要先定义该函数再进行调用。
阅读全文