xgboost源码回归预测
时间: 2023-09-08 16:15:22 浏览: 101
XGBoost是一种基于决策树的集成学习方法,可用于回归和分类问题。下面是使用XGBoost进行回归预测的示例代码:
1. 加载数据集
```python
import pandas as pd
from sklearn.datasets import load_boston
boston = load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
data['target'] = boston.target
```
2. 划分训练集和测试集
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data[boston.feature_names], data['target'], test_size=0.2)
```
3. 构建XGBoost回归模型
```python
import xgboost as xgb
params = {
'objective': 'reg:squarederror',
'learning_rate': 0.1,
'max_depth': 5,
'n_estimators': 100
}
model = xgb.XGBRegressor(**params)
model.fit(X_train, y_train)
```
4. 进行预测并评估模型性能
```python
from sklearn.metrics import mean_squared_error
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('MSE: {:.2f}'.format(mse))
```
以上就是使用XGBoost进行回归预测的示例代码,其中params字典中定义了模型的参数,包括学习率、最大深度和估计器数量等。可以根据具体需求进行调整。
阅读全文