用决策树和随机森林分析sklearn中的diabetes数据集代码
时间: 2023-05-16 11:04:56 浏览: 150
决策树和随机森林
以下是使用决策树和随机森林分析sklearn中的diabetes数据集的代码:
```python
from sklearn.datasets import load_diabetes
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 加载数据集
diabetes = load_diabetes()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target, test_size=0.2, random_state=42)
# 使用决策树进行训练和预测
tree_reg = DecisionTreeRegressor(random_state=42)
tree_reg.fit(X_train, y_train)
tree_pred = tree_reg.predict(X_test)
tree_mse = mean_squared_error(y_test, tree_pred)
print("决策树的均方误差为:", tree_mse)
# 使用随机森林进行训练和预测
rf_reg = RandomForestRegressor(n_estimators=100, random_state=42)
rf_reg.fit(X_train, y_train)
rf_pred = rf_reg.predict(X_test)
rf_mse = mean_squared_error(y_test, rf_pred)
print("随机森林的均方误差为:", rf_mse)
```
希望这个代码能够帮到你!
阅读全文