In [1]:
#支持向量机 根据训练样本的分布搜索所有可能分类器中最佳的那个
from sklearn.datasets import load_iris
digits = load_iris()

In [2]:
digits.data.shape


Out[2]:
(150, 4)

In [3]:
#切分训练集和测试集
from sklearn.cross_validation import train_test_split


/Users/wizardholy/soft/dunas/lib/python2.7/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

In [4]:
X_train,Xtest,Y_Train,Y_test=train_test_split(digits.data,
                                             digits.target,
                                            test_size=0.25,
                                             random_state=33)

In [5]:
Y_Train.shape


Out[5]:
(112,)

In [6]:
Y_test.shape


Out[6]:
(38,)

In [7]:
#导入标准化模块, 标准化数据
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR  #导入支持向量积回归模型

In [8]:
ss = StandardScaler()

In [9]:
X_train = ss.fit_transform(X_train)

In [10]:
Xtest = ss.transform(Xtest)

In [11]:
lsvr = SVR(kernel = 'linear') #使用线性核函数配置
lsvr.fit(X_train, Y_Train)
lsvr_y_predic = lsvr.predict(Xtest)

In [12]:
plsvr = SVR(kernel = 'poly') #使用多项式核函数配置
plsvr.fit(X_train, Y_Train)
plsvr_y_predic = plsvr.predict(Xtest)

In [13]:
rbfsvr = SVR(kernel = 'rbf') #使用径向基核函数配置
rbfsvr.fit(X_train, Y_Train)
rbfsvr_y_predic = rbfsvr.predict(Xtest)

In [14]:
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error

In [15]:
print 'The Accuracy of linear SVR is',lsvr.score(Xtest, Y_test)
print 'The Value of R-squared of linear SVR is',r2_score(Y_test, lsvr_y_predic)
print 'The Value of mean_absolute_error of linear SVR is',mean_absolute_error(Y_test, lsvr_y_predic)
print 'The Value of mean_squared_error of linear SVR is',mean_squared_error(Y_test, lsvr_y_predic)


The Accuracy of linear SVR is 0.899791215427
The Value of R-squared of linear SVR is 0.899791215427
The Value of mean_absolute_error of linear SVR is 0.185934519571
The Value of mean_squared_error of linear SVR is 0.0628039820215

In [16]:
print 'The Accuracy of poly SVR is',plsvr.score(Xtest, Y_test)
print 'The Value of R-squared of poly SVR is',r2_score(Y_test, plsvr_y_predic)
print 'The Value of mean_absolute_error of poly SVR is',mean_absolute_error(Y_test, plsvr_y_predic)
print 'The Value of  mean_squared_error of poly SVR is',mean_squared_error(Y_test, plsvr_y_predic)


The Accuracy of poly SVR is 0.775192120891
The Value of R-squared of poly SVR is 0.775192120891
The Value of mean_absolute_error of poly SVR is 0.260578994473
The Value of  mean_squared_error of poly SVR is 0.14089413476

In [17]:
print 'The Accuracy of rbf SVR is',rbfsvr.score(Xtest, Y_test)
print 'The Value of R-squared of rbf SVR is',r2_score(Y_test, rbfsvr_y_predic)
print 'The Value of mean_absolute_error of rbf SVR is',mean_absolute_error(Y_test, rbfsvr_y_predic)
print 'The Value of  mean_squared_error of rbf SVR is',mean_squared_error(Y_test, rbfsvr_y_predic)


The Accuracy of rbf SVR is 0.928893307557
The Value of R-squared of rbf SVR is 0.928893307557
The Value of mean_absolute_error of rbf SVR is 0.148929627444
The Value of  mean_squared_error of rbf SVR is 0.0445647899312