svm回归预测,数据,见同名同名公众号
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
#加载数据集并归一化
def data_load():
data = pd.read_csv('data.csv') #读取数据
y = data.y #标签
X = data.drop('y', axis=1) #特征
row = data.shape[0] #数据集总行数
# print(row)
num_train = int(row * 0.9) # 训练集比例计算训练集数量
#划分训练集与测试集
x_train=X.iloc[0:num_train,:].values
x_test = X.iloc[num_train:, :].values
y_train = y.iloc[0:num_train].values.reshape(-1,1) #转换为二维
y_test=y.iloc[num_train:].values.reshape(-1,1)#转换为二维
# 归一化标准ss_X,ss_Y
ss_X = StandardScaler().fit(x_train)
ss_Y = StandardScaler().fit(y_train)
#归一化
x_train = ss_X.transform(x_train)
x_test = ss_X.transform(x_test)
y_train = ss_Y.transform(y_train)
y_test = ss_Y.transform(y_test)
return x_train,y_train,x_test,y_test,ss_Y
if __name__=='__main__':
# 加载数据
x_train,y_train,x_test,y_test,ss_Y=data_load()
# 训练SVR模型
model = SVR(kernel='linear') #定义模型
model.fit(x_train,y_train) #训练模型
#预测
pred_data = model.predict(x_test)
#反归一化
y_test = ss_Y.inverse_transform(y_test) #实际值反归一化
pred_test = ss_Y.inverse_transform(pred_data.reshape(-1,1))#预测值反归一化
#设置绘图属性
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False
# 绘图
plt.figure()
plt.plot(y_test, c='k', marker="*",label='实际值')
plt.plot(pred_test, c='r', marker="o",label='预测值')
plt.legend()
plt.xlabel('样本点')
plt.ylabel('功率')
plt.title('测试集对比')
plt.savefig('测试集对比.jpg')
plt.show()
预测对比图: