# 使用简单数据集体验支持向量回归全流程
时间: 2024-02-29 09:52:33 浏览: 26
好的,我们来体验一下支持向量回归的全流程。
首先,我们需要准备数据集。我们可以使用sklearn自带的波士顿房价数据集。代码如下:
```python
from sklearn.datasets import load_boston
boston = load_boston()
X, y = boston.data, boston.target
```
接着,我们将数据集分为训练集和测试集。代码如下:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
然后,我们需要进行特征缩放。这里我们使用StandardScaler进行标准化处理。代码如下:
```python
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
接下来,我们可以使用支持向量回归进行训练和预测。这里我们使用SVR模型。代码如下:
```python
from sklearn.svm import SVR
svr = SVR(kernel='linear')
svr.fit(X_train_scaled, y_train)
y_pred = svr.predict(X_test_scaled)
```
最后,我们可以使用均方误差评估模型的表现。代码如下:
```python
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test, y_pred)
print('均方误差:', mse)
```
这样就完成了支持向量回归的全流程。你可以将代码复制到Jupyter Notebook中进行测试。
相关推荐
![](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)