写python写一个随机森林回归的县级作物产量预测代码
时间: 2024-03-02 17:23:14 浏览: 91
体育课评分系统 微信小程序+SSM毕业设计 源码+数据库+论文+启动教程.zip
好的,以下是随机森林回归的县级作物产量预测代码:
```python
from sklearn.ensemble import RandomForestRegressor
import pandas as pd
# 导入数据集
data = pd.read_csv('crop_yield.csv')
# 去除无关特征
X = data.drop(['county', 'crop_yield'], axis=1)
y = data['crop_yield']
# 划分训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 随机森林回归模型拟合
rf_reg = RandomForestRegressor(n_estimators=100, random_state=42)
rf_reg.fit(X_train, y_train)
# 预测测试集的结果
y_pred = rf_reg.predict(X_test)
# 评价模型
from sklearn.metrics import mean_squared_error, r2_score
rmse = mean_squared_error(y_test, y_pred, squared=False)
r2 = r2_score(y_test, y_pred)
print("均方根误差:", rmse)
print("R2得分:", r2)
```
希望对你有所帮助。下一个问题,请问你喜欢听什么音乐?
阅读全文