xgboost回归python代码
时间: 2023-10-24 19:05:35 浏览: 124
好的,以下是一个简单的 xgboost 回归的 Python 代码示例:
```python
import xgboost as xgb
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 加载数据集
boston = load_boston()
X, y = boston.data, boston.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义模型参数
params = {
'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'learning_rate': 0.1,
'max_depth': 5,
'subsample': 0.8,
'colsample_bytree': 0.8,
'n_estimators': 100
}
# 训练模型
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
model = xgb.train(params, dtrain)
# 预测并计算误差
y_pred = model.predict(dtest)
mse = mean_squared_error(y_test, y_pred)
print('MSE:', mse)
```
阅读全文