学习笔记:用不同核函数的支持向量机预测波士顿房价

代码如下:

# -*- coding: utf-8 -*-
"""
Created on Tue May 15 17:23:42 2018

@author: eagle
"""
# =============================================================================
# 不同核函数的支持向量机预测波士顿房价
# =============================================================================

from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
import numpy as np
from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVR

from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error

#加载房价数据
boston = load_boston()      #共506条数据
print(boston.DESCR)
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 average target value is',np.mean(boston.target)) #平均值

#标准化
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))

#不同核函数对比
#线性核函数
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)


#评价
#线性核函数
print('The scroe of linear_svr is:',linear_svr.score(X_test,y_test))
print('The r2-score of linear_svr is:',r2_score(y_test,linear_y_predict))
print('The mean_squared_error of linear_svr is',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(linear_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_y_predict)))

#多项式核函数
print('The scroe of poly_svr is:',poly_svr.score(X_test,y_test))
print('The r2-score of poly_svr is:',r2_score(y_test,poly_y_predict))
print('The mean_squared_error of poly_svr is',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(poly_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_y_predict)))

#径向基核函数
print('The scroe of rbf_svr is:',rbf_svr.score(X_test,y_test))
print('The r2-score of rbf_svr is:',r2_score(y_test,rbf_y_predict))
print('The mean_squared_error of rbf_svr is',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(rbf_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_y_predict)))

采用支持向量机预测波士顿房价,可尝试采用多种不同的核函数,从线性核函数、多项式核函数、径向基核函数的对比性能来看,径向基核函数的效果是最好的。

核函数将线性不可分的低维输入映射到高维可分的新特征空间。



你可能感兴趣的:(机器学习)