1.SVR时间序列预测
SVR可用于时间序列分析,但不是较好的选择。现在一般采用LSTM神经网络来处理时间序列数据
# SVR预测
# 也可用于时间序列分析(ARIMA也可用于时间序列分析)
import numpy as np
from sklearn import svm
import matplotlib.pyplot as plt
if __name__ == "__main__":
# 构造数据
N = 50
np.random.seed(0)
# 排序
x = np.sort(np.random.uniform(0, 6, N), axis=0)
y = 2*np.sin(x) + 0.1*np.random.randn(N)
x = x.reshape(-1, 1)
print('x =\n', x)
print('y =\n', y)
# 高斯核函数
print('SVR - RBF')
svr_rbf = svm.SVR(kernel='rbf', gamma=0.2, C=100)
svr_rbf.fit(x, y)
# 线性核函数
print('SVR - Linear')
svr_linear = svm.SVR(kernel='linear', C=100)
svr_linear.fit(x, y)
# 多项式核函数
print('SVR - Polynomial')
svr_poly = svm.SVR(kernel='poly', degree=3, C=100)
svr_poly.fit(x, y)
print('Fit OK.')
# 思考:系数1.1改成1.5
x_test = np.linspace(x.min(), 1.1*x.max(), 100).reshape(-1, 1)
y_rbf = svr_rbf.predict(x_test)
y_linear = svr_linear.predict(x_test)
y_poly = svr_poly.predict