使用Adaboost实现Iris分类的代码 使用梯度提升树GBDT实现Boston房价预测
时间: 2023-07-11 19:12:22 浏览: 102
梯度提升树算法(GBT)详解及其原理应用
好的,下面是使用Adaboost实现Iris分类和使用梯度提升树GBDT实现Boston房价预测的Python代码:
使用Adaboost实现Iris分类的代码:
```
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.ensemble import AdaBoostClassifier
# 加载数据集
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
# 使用决策树作为基础分类器
base_classifier = DecisionTreeClassifier(max_depth=1)
# 使用Adaboost进行训练
ada_boost = AdaBoostClassifier(base_classifier, n_estimators=50, learning_rate=0.1)
ada_boost.fit(X_train, y_train)
# 对训练集和测试集进行预测
y_train_pred = ada_boost.predict(X_train)
y_test_pred = ada_boost.predict(X_test)
# 计算准确率
train_accuracy = accuracy_score(y_train, y_train_pred)
test_accuracy = accuracy_score(y_test, y_test_pred)
print("训练集准确率:", train_accuracy)
print("测试集准确率:", test_accuracy)
```
使用梯度提升树GBDT实现Boston房价预测的代码:
```
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
# 加载数据集
boston = load_boston()
X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.3, random_state=42)
# 使用梯度提升树进行训练
gbdt = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1)
gbdt.fit(X_train, y_train)
# 对训练集和测试集进行预测
y_train_pred = gbdt.predict(X_train)
y_test_pred = gbdt.predict(X_test)
# 计算均方误差
train_mse = mean_squared_error(y_train, y_train_pred)
test_mse = mean_squared_error(y_test, y_test_pred)
print("训练集均方误差:", train_mse)
print("测试集均方误差:", test_mse)
```
以上就是使用Adaboost实现Iris分类和使用梯度提升树GBDT实现Boston房价预测的Python代码。
阅读全文