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

In [2]:
digits.data.shape


Out[2]:
(150, 4)

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

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 [8]:
#导入标准化模块, 标准化数据
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

In [9]:
ss = StandardScaler()

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

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

In [12]:
lsvc = LinearSVC()

In [13]:
lsvc.fit(X_train, Y_Train)


Out[13]:
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
     verbose=0)

In [14]:
ypredic = lsvc.predict(Xtest)

In [15]:
#准确性评估
print 'The Accuracy of Linear SVC is',lsvc.score(Xtest, Y_test)


The Accuracy of Linear SVC is 0.921052631579

In [16]:
from sklearn.metrics import classification_report

In [17]:
print classification_report(Y_test, ypredic, target_names=digits.target_names.astype(str))


             precision    recall  f1-score   support

     setosa       1.00      1.00      1.00         8
 versicolor       0.83      0.91      0.87        11
  virginica       0.94      0.89      0.92        19

avg / total       0.92      0.92      0.92        38


In [ ]: