回归学习-支持向量机回归

#导入数据
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, test_size=0.25, random_state=33)

#分析回归目标值的差异
print('The max target value is:',np.max(boston.target))
print('The min target value is:',np.min(boston.target))
print('The mean 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)
#y_test = ss_y.transform(y_test)

#训练模型
from sklearn.svm import SVR
#使用线性核函数配置的支持向量机进行回归训练
linear_svr = SVR(kernel='linear')
linear_svr.fit(X_train, y_train)
linear_y_predict = linear_svr.predict(X_test)

#使用多项式核函数配置的支持向量机进行回归训练
poly_svr = SVR(kernel='poly')
poly_svr.fit(X_train, y_train)
poly_y_predict = poly_svr.predict(X_test)

#使用径向基核函数配置的支持向量机进行回归训练
rbf_svr = SVR(kernel='rbf')
rbf_svr.fit(X_train, y_train)
rbf_y_predict = rbf_svr.predict(X_test)

#性能分析
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
print('The value of default measurement of LinearSVR is:',linear_svr.score(X_test, y_test))      #自带的评估模型
print('The value of R-squared of LinearSVR is',r2_score(y_test, linear_y_predict))             #
print('The mean_squared_error of LinearSVR is',mean_squared_error(y_test, linear_y_predict))   #均方误差
print('The mean_absolute_error of LinearSVR is',mean_absolute_error(y_test, linear_y_predict)) #平均绝对误差

print('The value of default measurement of polySVR is:',poly_svr.score(X_test, y_test))
print('The value of R-squared of polySVR is',r2_score(y_test, poly_y_predict))
print('The mean_squared_error of polySVR is',mean_squared_error(y_test, poly_y_predict))
print('The mean_absolute_error of polySVR is',mean_absolute_error(y_test, poly_y_predict))

print('The value of default measurement of rbfSVR is:',rbf_svr.score(X_test, y_test))
print('The value of R-squared of rbfSVR is',r2_score(y_test, rbf_y_predict))
print('The mean_squared_error of rbfSVR is',mean_squared_error(y_test, rbf_y_predict))
print('The mean_absolute_error of rbfSVR is',mean_absolute_error(y_test, rbf_y_predict))

输出:

回归学习-支持向量机回归_第1张图片


你可能感兴趣的:(Python机器学习及实践)