Python机器学习入门1.3《使用支持向量机对手写体数字图片分类》

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40883132/article/details/83095983

本次介绍的内容是:支持向量机分类器(Support Vector Classifier)

     决定直线位置的样本并不是所有的训练数据,而是其中的两个空间间隔最小的两个不同类别的数据点,而我们把这种可以用来真正帮助决策最线性分类模型的数据点叫做“支持向量”。

   手写体数据读取代码样例:


#从sklearn.datasets里导入手写体数字加载器
from sklearn.datasets import load_digits

#从通过数据加载器获得手写体数字的数码图像数据并储存在digits变量中
digits=load_digits()

#检查数据规模和特征维度
print(digits.data.shape)

书写体数据分割:


#从sklearn.cross_validation 中 导入 train_test_split
from sklearn.cross_validation import train_test_split
#随机获取75%的数据作为训练样本;其余25%的数据作为测试样本
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.fit_transform(X_test)

#初始化线性假设的支持向量机分类器LinearSVC
lsvc=LinearSVC()

#进行模型训练
lsvc.fit(X_train,y_train)
#利用训练好的模型对测试样本的数字类别进行预测,预测结果储存在标量y_predict中
y_predict=lsvc.predict(X_test)

 支持向量机模型对手写体数码图像识别能力的评估:


#使用模型自带的评估函数进行准确性评测
print('The Accuracy pf Linear SVC is',lsvc.score(X_test,y_test))


#用classification_report对预测结果进行详细分析
from sklearn.metrics import classification_report
print(classification_report(y_test,y_predict,target_names=digits.target_names.astype(str)))

 

特点分析:

支持向量机可以帮助我们再海量甚至高维度的 数据中,筛选对预测任务最为之有效的少数训练样本。节省内存的同时,提高了模型的预测性能。

完整代码参考:

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 16 19:23:54 2018

@author: asus
"""

#从sklearn.datasets里导入手写体数字加载器
from sklearn.datasets import load_digits

#从通过数据加载器获得手写体数字的数码图像数据并储存在digits变量中
digits=load_digits()

#检查数据规模和特征维度
print(digits.data.shape)

#从sklearn.cross_validation 中 导入 train_test_split
from sklearn.cross_validation import train_test_split
#随机获取75%的数据作为训练样本;其余25%的数据作为测试样本
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.fit_transform(X_test)

#初始化线性假设的支持向量机分类器LinearSVC
lsvc=LinearSVC()

#进行模型训练
lsvc.fit(X_train,y_train)
#利用训练好的模型对测试样本的数字类别进行预测,预测结果储存在标量y_predict中
y_predict=lsvc.predict(X_test)

#使用模型自带的评估函数进行准确性评测
print('The Accuracy pf Linear SVC is',lsvc.score(X_test,y_test))

#用classification_report对预测结果进行详细分析
from sklearn.metrics import classification_report
print(classification_report(y_test,y_predict,target_names=digits.target_names.astype(str)))


猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/83095983
今日推荐