赞
踩
本文是个人学习笔记,内容主要基于支持向量机(回归)SVR对boston数据集学习回归模型和利用模型预测。
SVR同SVC一样,是从训练数据中选取一部分更加有效地支持向量,根据这些训练样本的值通过回归分析预测目标。
在SVM里,核函数是一种非常有用的特征映射技巧,径向基核函数对特征可以进行非线性映射。
from sklearn.datasets import load_boston
boston=load_boston()
print(boston.DESCR) #打印数据描述
from sklearn.cross_validation import train_test_split
import numpy as np
X=boston.data
y=boston.target
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33,test_size=0.25)
print('The max target value is',np.max(boston.target))
print('The min target value is',np.min(boston.target))
print('The average target value is',np.mean(boston.target))
from sklearn.preprocessing import StandardScaler
ss_X=StandardScaler()
ss_y=StandardScaler()
X_train=ss_X.fit_transform(X_train)
X_test=ss_X.transform(X_test)
y_train=ss_y.fit_transform(y_train.reshape(-1, 1))
y_test=ss_y.transform(y_test.reshape(-1, 1))
from sklearn.svm import SVR
linear_svr=SVR(kernel='linear') #线性核函数初始化的SVR
linear_svr.fit(X_train,y_train)
linear_svr_y_predict=linear_svr.predict(X_test)
poly_svr=SVR(kernel='poly') #多项式核函数初始化的SVR
poly_svr.fit(X_train,y_train)
poly_svr_y_predict=poly_svr.predict(X_test)
rbf_svr=SVR(kernel='rbf') #径向基核函数初始化的SVR
rbf_svr.fit(X_train,y_train)
rbf_svr_y_predict=rbf_svr.predict(X_test)
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error print('R-squared value of linear SVR is',linear_svr.score(X_test,y_test)) print('The mean squared error of linear SVR is',mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(linear_svr_y_predict))) print('The mean absolute error of linear SVR is',mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(linear_svr_y_predict))) print(' ') print('R-squared value of Poly SVR is',poly_svr.score(X_test,y_test)) print('The mean squared error of Poly SVR is',mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(poly_svr_y_predict))) print('The mean absolute error of Poly SVR is',mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(poly_svr_y_predict))) print(' ') print('R-squared value of RBF SVR is',rbf_svr.score(X_test,y_test)) print('The mean squared error of RBF SVR is',mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rbf_svr_y_predict))) print('The mean absolute error of RBF SVR is',mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rbf_svr_y_predict)))
从上述测评结果可见,不同配置下的模型在相同测试集上,存在非常大的性能差异;并且在使用了径向基(Radial basis function)核函数对特征进行非线性映射之后,支持向量机展现了最佳的回归性能。
欢迎关注公众号:瑞行AI,欢迎交流AI算法、数据分析、leetcode刷题等技术,提供技术方案咨询和就业指导服务!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。