python 写SVR
时间: 2023-06-28 10:12:30 浏览: 94
下面是使用Python实现SVR的基本步骤:
1.导入必要的库:numpy、pandas和sklearn
```python
import numpy as np
import pandas as pd
from sklearn.svm import SVR
```
2.读取数据集,将特征和目标分别存储到X和y中
```python
data = pd.read_csv('data.csv')
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
```
3.标准化数据集
```python
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y.reshape(-1, 1))
```
4.拟合SVR模型
```python
regressor = SVR(kernel='rbf')
regressor.fit(X, y)
```
5.进行预测
```python
y_pred = regressor.predict(sc_X.transform([[6.5]]))
y_pred = sc_y.inverse_transform(y_pred)
```
完整代码如下:
```python
import numpy as np
import pandas as pd
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
# 读取数据集
data = pd.read_csv('data.csv')
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 标准化数据集
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y.reshape(-1, 1))
# 拟合SVR模型
regressor = SVR(kernel='rbf')
regressor.fit(X, y)
# 进行预测
y_pred = regressor.predict(sc_X.transform([[6.5]]))
y_pred = sc_y.inverse_transform(y_pred)
print(y_pred)
```
阅读全文