写xgboost回归预测模型代码
时间: 2023-10-19 20:05:44 浏览: 89
好的,以下是一个简单的xgboost回归预测模型代码的示例:
```python
import xgboost as xgb
import pandas as pd
from sklearn.metrics import mean_squared_error
# 读取训练数据和测试数据
train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')
# 划分特征和标签
train_X, train_y = train_data.iloc[:, :-1], train_data.iloc[:, -1]
test_X, test_y = test_data.iloc[:, :-1], test_data.iloc[:, -1]
# 定义模型参数
params = {
'booster': 'gbtree',
'objective': 'reg:squarederror',
'max_depth': 5,
'learning_rate': 0.1,
'n_estimators': 100
}
# 训练模型
model = xgb.XGBRegressor(**params)
model.fit(train_X, train_y)
# 预测并计算均方误差
pred_y = model.predict(test_X)
mse = mean_squared_error(test_y, pred_y)
print('均方误差:%.2f' % mse)
```
希望能对您有所帮助!下一个问题,请问您想问些什么呢?
阅读全文