from sklearn.ensemble import Bagging Classifier #导入Bagging分类器 from sklearn.tree import Decision Tree Classifier #导入决策树分类器 from sklearn.metrics import (f1_score, confusion_matrix) # 导入评估指标 dt = Bagging Classifier(Decision Tree Classifier()) # 只使用一棵决策树 dt.fit(X_train, y_train) # 拟合模型 y_pred = dt.predict(X_test) # 进行预测 print("决策树测试准确率: {:.2f}%".format(dt.score(X_test, y_test)*100)) print("决策树测试F1分数: {:.2f}%".format(f1_score(y_test, y_pred)*100)) bdt = Bagging Classifier(Decision Tree Classifier()) #树的Bagging bdt.fit(X_train, y_train)# 拟合模型 y_pred = bdt.predict(X_test) # 进行预测 print("决策树Bagging测试准确率: {:.2f}%".format(bdt.score(X_test, y_test)*100)) print("决策树Bagging测试F1分数: {:.2f}%".format(f1_score(y_test, y_pred)*100))