使用sklearnpython库中进行岭回归模型来预测并包含模型评估和可视化代码
时间: 2024-10-23 08:17:40 浏览: 9
线性回归模型预测波士顿房价并绘图
5星 · 资源好评率100%
在Python中,可以使用`sklearn`库中的`Ridge`类来进行岭回归建模。以下是一个完整的示例,包括模型训练、预测以及评估:
首先,导入所需的库:
```python
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_boston
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, accuracy_score, explained_variance_score
```
接着,加载数据集(这里以波士顿房价数据为例):
```python
boston = load_boston()
X = boston.data
y = boston.target
```
对数据进行预处理(归一化),这有助于模型性能:
```python
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
```
划分训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
```
创建并训练岭回归模型:
```python
ridge = Ridge(alpha=1.0) # alpha参数控制正则化程度,默认为1.0
ridge.fit(X_train, y_train)
```
进行预测:
```python
y_pred_ridge = ridge.predict(X_test)
```
评估模型:
```python
# 混淆矩阵
conf_mat = confusion_matrix(y_test, y_pred_ridge.round())
# 准确率
accuracy = accuracy_score(y_test, y_pred_ridge.round())
# 偏差 Deviance
explained_var = explained_variance_score(y_test, y_pred_ridge)
print(f"Confusion Matrix:\n{conf_mat}")
print(f"Accuracy: {accuracy}")
print(f"Explained Variance Score: {explained_var}")
```
对于模型系数的可视化,由于`sklearn`本身不直接提供可视化功能,你可以通过其他库如`pandas.DataFrame`展示系数,但通常会结合`matplotlib`进行绘制折线图或条形图。不过,为了简洁,这里并未包括系数可视化的代码。
阅读全文