用决策树、Bagging和AdaBoost这三个模型分别训练糖尿病数据,比较分数高低代码表示
时间: 2024-09-27 22:07:29 浏览: 33
7.1 Bagging,RF&Adaboost.zip_Bagging代码_RF--ADABOOST_adaboost_bagg
在Python中,我们可以使用`sklearn`库来构建决策树、Bagging(随机森林)和AdaBoost模型,并在糖尿病数据集上训练它们。这里是一个简单的示例,展示了如何操作:
```python
import numpy as np
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.model_selection import train_test_split, cross_val_score
# 加载糖尿病数据集
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
# 划分数据集
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)
dt_scores = cross_val_score(dtc, X_train, y_train, cv=5)
print(f"Decision Tree scores: {np.mean(dtc_scores)} ± {np.std(dtc_scores)}")
# Bagging(随机森林)
rfc = RandomForestClassifier(n_estimators=100, random_state=42)
rf_scores = cross_val_score(rfc, X_train, y_train, cv=5)
print(f"Random Forest scores: {np.mean(rfc_scores)} ± {np.std(rfc_scores)}")
# AdaBoost模型
abc = AdaBoostClassifier(n_estimators=100, random_state=42)
abc_scores = cross_val_score(abc, X_train, y_train, cv=5)
print(f"AdaBoost scores: {np.mean(abc_scores)} ± {np.std(abc_scores)}")
# 比较分数
scores_comparison = [np.mean(score) for score in [dt_scores, rf_scores, abc_scores]]
best_model_index = np.argmax(scores_comparison)
best_model_name = ['Decision Tree', 'Random Forest', 'AdaBoost'][best_model_index]
print(f"The best model is {best_model_name} with a mean score of {scores_comparison[best_model_index]}.")
阅读全文