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 [18]:
Y_test.shape


Out[18]:
(38,)

In [25]:
#导入标准化模块, 标准化数据
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor,GradientBoostingRegressor

In [21]:
ss = StandardScaler()

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

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

In [24]:
rfr = RandomForestRegressor()
rfr.fit(X_train, Y_Train)
rfr_y_predic = rfr.predict(Xtest)

In [26]:
etr = ExtraTreesRegressor()
etr.fit(X_train, Y_Train)
etr_y_predic = etr.predict(Xtest)

In [27]:
gbr = GradientBoostingRegressor()
gbr.fit(X_train, Y_Train)
gbr_y_predic = gbr.predict(Xtest)

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

In [29]:
print 'The Accuracy of RandomForestRegressor is',rfr.score(Xtest, Y_test)
print 'The Value of R-squared of RandomForestRegressor is',r2_score(Y_test, rfr_y_predic)
print 'The Value of mean_absolute_error of RandomForestRegressor is',mean_absolute_error(Y_test, rfr_y_predic)
print 'The Value of mean_squared_error of RandomForestRegressor is',mean_squared_error(Y_test, rfr_y_predic)


The Accuracy of RandomForestRegressor is 0.906364640884
The Value of R-squared of RandomForestRegressor is 0.906364640884
The Value of mean_absolute_error of RandomForestRegressor is 0.0815789473684
The Value of mean_squared_error of RandomForestRegressor is 0.0586842105263

In [30]:
print 'The Accuracy of ExtraTreesRegressor is',etr.score(Xtest, Y_test)
print 'The Value of R-squared of ExtraTreesRegressor is',r2_score(Y_test, etr_y_predic)
print 'The Value of mean_absolute_error of ExtraTreesRegressor is',mean_absolute_error(Y_test, etr_y_predic)
print 'The Value of  mean_squared_error of ExtraTreesRegressor is',mean_squared_error(Y_test, etr_y_predic)


The Accuracy of ExtraTreesRegressor is 0.91182320442
The Value of R-squared of ExtraTreesRegressor is 0.91182320442
The Value of mean_absolute_error of ExtraTreesRegressor is 0.1
The Value of  mean_squared_error of ExtraTreesRegressor is 0.0552631578947

In [31]:
print 'The Accuracy of GradientBoostingRegressor is',gbr.score(Xtest, Y_test)
print 'The Value of R-squared of GradientBoostingRegressor is',r2_score(Y_test, gbr_y_predic)
print 'The Value of mean_absolute_error of GradientBoostingRegressor is',mean_absolute_error(Y_test, gbr_y_predic)
print 'The Value of  mean_squared_error of GradientBoostingRegressor is',mean_squared_error(Y_test, gbr_y_predic)


The Accuracy of GradientBoostingRegressor is 0.866909244075
The Value of R-squared of GradientBoostingRegressor is 0.866909244075
The Value of mean_absolute_error of GradientBoostingRegressor is 0.0952183726364
The Value of  mean_squared_error of GradientBoostingRegressor is 0.0834121427367

In [ ]: