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

In [3]:
digits.data.shape


Out[3]:
(150, 4)

In [4]:
#切分训练集和测试集
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 [5]:
X_train,Xtest,Y_Train,Y_test=train_test_split(digits.data,
                                             digits.target,
                                            test_size=0.25,
                                             random_state=33)

In [6]:
Y_Train.shape


Out[6]:
(112,)

In [7]:
Y_test.shape


Out[7]:
(38,)

In [1]:
#导入标准化模块, 标准化数据
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeRegressor #导入回归树

In [8]:
ss = StandardScaler()

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

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

In [13]:
dtr = DecisionTreeRegressor() #使用默认配置
dtr.fit(X_train, Y_Train)
dtr_y_predic = dtr.predict(Xtest)

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




In [21]:
print 'The Accuracy of DecisionTreeRegressor is',dtr.score(Xtest, Y_test)
print 'The Value of R-squared of DecisionTreeRegressor is',r2_score(Y_test, dtr_y_predic)
print 'The Value of mean_absolute_error of DecisionTreeRegressor is',mean_absolute_error(Y_test, dtr_y_predic)
print 'The Value of  mean_squared_error of DecisionTreeRegressor is',mean_squared_error(Y_test, dtr_y_predic)


The Accuracy of DecisionTreeRegressor is 0.832044198895
The Value of R-squared of DecisionTreeRegressor is 0.832044198895
The Value of mean_absolute_error of DecisionTreeRegressor is 0.105263157895
The Value of  mean_squared_error of DecisionTreeRegressor is 0.105263157895

In [ ]: