svm实现sciki-learn内部手写体分类

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 13 10:25:38 2018

@author: fengjuan
"""
#从sklearn.datasets导入数字加载器
from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
digits=load_digits()
#从通过数据加载器获得的手写体数码图像数据存储在digits变量中
print(digits.data.shape)
#将数据分割,25%作为测试集,75%作为训练集
X_train,X_test,y_train,y_test=train_test_split(digits.data,digits.target,
                                               test_size=0.25,random_state=33)
print(y_train.shape)
print(y_test.shape)
#sklearn.preprocessing中导入标准化模块
from sklearn.preprocessing import StandardScaler
#从sklearn.svm导入基于线性假设的支持向量机分类器LinearSVC
from sklearn.svm import LinearSVC
#对特征数据进行标准化
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
#初始化基于线性假设的支持向量机分类器LinearSVC
lsvc=LinearSVC()
#模型训练
lsvc.fit(X_train,y_train)
#用训练好的模型进行预测,并将预测结果存储在变量y_predict中
y_predict=lsvc.predict(X_test)
#识别能力的评估
from sklearn.metrics import classification_report
print('Accuracy of Linear SVC is:',lsvc.score(X_test,y_test))
print(classification_report(y_test,y_predict,target_names=digits.target_names.astype(str)))
#结果'''Accuracy of Linear SVC is: 0.9533333333333334
'''
             precision    recall  f1-score   support

          0       0.92      1.00      0.96        35
          1       0.96      0.98      0.97        54
          2       0.98      1.00      0.99        44
          3       0.93      0.93      0.93        46
          4       0.97      1.00      0.99        35
          5       0.94      0.94      0.94        48
          6       0.96      0.98      0.97        51
          7       0.92      1.00      0.96        35
          8       0.98      0.84      0.91        58
          9       0.95      0.91      0.93        44

avg / total       0.95      0.95      0.95       450'''

猜你喜欢

转载自blog.csdn.net/qq_40516413/article/details/83037490