利用SVM回归模型预测练习,数据集是sklearn集成的美国波士顿地区房价。kernel=‘rbf’,指用径向基核函数配置的支持向量机进行回归训练。上码:
#-*- coding:utf-8 -*-
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 valueis:',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)
y_test=ss_y.transform(y_test)
from sklearn.svm import SVR
rbf_svr=SVR(kernel='rbf')
rbf_svr.fit(X_train,y_train)
rbf_svr_y_predict=rbf_svr.predict(X_test)
from sklearn.metrics importr2_score,mean_absolute_error,mean_squared_error
print('R-squared value of RBF SVRis:',r2_score(y_test,rbf_svr_y_predict))
print('The mean square error of RBF SVRis:',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(rbf_svr_y_predict)))
print('The mean absolute error of RBF SVRis:',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(rbf_svr_y_predict)))