请写一段python代码,使用sklearn,用含rbf核的核岭回归模型训练波士顿房价数据集
时间: 2024-01-24 21:17:34 浏览: 41
以下是使用sklearn库中的核岭回归模型训练波士顿房价数据集的Python代码:
```python
from sklearn.datasets import load_boston
from sklearn.kernel_ridge import KernelRidge
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 加载数据集
boston = load_boston()
X = boston.data
y = boston.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 创建核岭回归模型
kr = KernelRidge(alpha=0.1, kernel='rbf')
# 训练模型
kr.fit(X_train, y_train)
# 预测测试集结果
y_pred = kr.predict(X_test)
# 评估模型
rmse = mean_squared_error(y_test, y_pred, squared=False)
print("Root Mean Squared Error:", rmse)
```
在上面的代码中,我们首先使用sklearn库中的load_boston函数加载波士顿房价数据集。然后,我们使用train_test_split函数将数据集划分为训练集和测试集。接下来,我们创建一个核岭回归模型,并使用fit方法训练模型。最后,我们使用predict方法预测测试集结果,并使用mean_squared_error函数计算模型的均方根误差(RMSE)来评估模型的表现。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)