Ref: Scikit Learn
kernels
"""===================================================================Support Vector Regression (SVR) using linear and non-linear kernels支持向量回归SVR使用线性linear与非线性核polynomial、RBF kernels===================================================================Toy example of 1D regression using linear, polynomial and RBF kernels."""PRint(__doc__) #打印文本脚本说明import numpy as npfrom sklearn.svm import SVRimport matplotlib.pyplot as plt################################################################################ Generate sample data #人工生成数据点X = np.sort(5 * np.random.rand(40, 1), axis=0) # np.random.rand()随机生成[0,1]的随机数,默认是行向量,指定axis=0为列向量y1 = np.sin(X)y = np.sin(X).ravel() #将列向量转化为行向量# ravel说明Ref:# https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html# http://blog.csdn.net/lanchunhui/article/details/50354978# http://old.sebug.net/paper/books/scipydoc/numpy_intro.html################################################################################ Add noise to targets #增加噪音y[::5] += 3 * (0.5 - np.random.rand(8))################################################################################ Fit regression model #拟合回归模型#构建模型,设置模型超参数C,gamma,degreesvr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)svr_lin = SVR(kernel='linear', C=1e3)svr_poly = SVR(kernel='poly', C=1e3, degree=2)#用.fit()方法进行拟合,用.predict()方法进行预测,并返回预测结果y_rbf = svr_rbf.fit(X, y).predict(X)y_lin = svr_lin.fit(X, y).predict(X)y_poly = svr_poly.fit(X, y).predict(X)################################################################################ look at the results #查看结果lw = 2 #设置线宽#绘制人工数据散点图plt.scatter(X, y, color='darkorange', label='data')plt.hold('on') #类似Matlab中的hold on,可在同一个图中绘制后续图形#绘制拟合曲线plt.plot(X, y_rbf, color='navy', lw=lw, label='RBF model') #设置label用于后续legend的显示plt.plot(X, y_lin, color='c', lw=lw, label='Linear model')plt.plot(X, y_poly, color='cornflowerblue', lw=lw, label='Polynomial model')#设置相关显示plt.xlabel('data')plt.ylabel('target')plt.title('Support Vector Regression')plt.legend()plt.show()levels: 对应plt.contour(),显示轮廓线 origin: 对应plt.imshow(),决定图像原点与矩阵原点是否重合,即图像的方向 plt.matshow(A): 绘制矩阵,将矩阵用图像显示
# 绘制矩阵Zplt.matshow(xx)plt.matshow(yy)plt.matshow(Z)plt.show()新闻热点
疑难解答