In [1]:
#支持向量机 根据训练样本的分布搜索所有可能分类器中最佳的那个
from sklearn.datasets import load_iris
digits = load_iris()
In [2]:
digits.data.shape
Out[2]:
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]:
In [7]:
Y_test.shape
Out[7]:
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]:
In [14]:
ypredic = lsvc.predict(Xtest)
In [15]:
#准确性评估
print 'The Accuracy of Linear SVC is',lsvc.score(Xtest, Y_test)
In [16]:
from sklearn.metrics import classification_report
In [17]:
print classification_report(Y_test, ypredic, target_names=digits.target_names.astype(str))
In [ ]: