【机器学习 sklearn】手写数字识别 SVM
来源:互联网 发布:我寄人间雪满头 知乎 编辑:程序博客网 时间:2024/04/27 22:11
运行结果:
"D:\Program Files\Python27\python.exe" D:/PycharmProjects/sklearn/SVM.py(1797L, 64L)[[ 0. 0. 5. ..., 0. 0. 0.] [ 0. 0. 0. ..., 10. 0. 0.] [ 0. 0. 0. ..., 16. 9. 0.] ..., [ 0. 0. 1. ..., 6. 0. 0.] [ 0. 0. 2. ..., 12. 0. 0.] [ 0. 0. 10. ..., 12. 1. 0.]][0 1 2 ..., 8 9 8]训练样本个数:1347测试样本个数:450SVM[[35 0 0 0 0 0 0 0 0 0] [ 0 53 0 0 0 0 1 0 0 0] [ 0 0 44 0 0 0 0 0 0 0] [ 0 0 0 43 0 2 0 1 0 0] [ 0 0 0 0 35 0 0 0 0 0] [ 1 0 0 0 0 45 1 0 0 1] [ 1 0 0 0 0 0 50 0 0 0] [ 0 0 0 0 0 0 0 35 0 0] [ 1 2 1 3 1 0 0 0 49 1] [ 0 0 0 0 0 1 0 2 1 40]]The Accuracy of Linear SVC is 0.953333333333 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 44avg / total 0.95 0.95 0.95 450[1 3 7 3 2 4 6 1 4 0 4 7 9 5 2 8 3 6 7 0 6 0 8 3 0 6 2 3 0 9 0 2 0 6 9 1 1 5 8 0 6 1 5 8 9 5 1 6 2 6 6 7 6 7 7 2 7 8 0 7 3 6 3 9 6 6 5 5 4 2 9 3 7 6 5 7 2 8 1 2 2 8 1 1 6 3 5 0 0 1 6 7 6 8 9 7 0 0 9 8 0 8 2 3 6 1 9 9 1 7 3 9 8 8 5 9 5 1 1 7 9 3 3 2 8 1 3 8 6 4 0 0 0 7 1 5 5 1 8 5 1 8 1 6 9 9 4 5 7 5 2 1 2 5 8 7 7 5 1 9 6 9 8 0 6 1 2 1 5 7 8 9 6 8 4 1 0 0 9 8 7 2 8 6 4 8 9 4 2 6 1 8 5 6 7 5 1 9 2 8 3 2 9 4 3 5 5 6 2 4 3 2 6 4 8 5 8 0 8 8 6 3 2 3 0 5 7 1 3 9 3 2 1 6 6 5 1 9 7 2 4 5 2 1 3 1 1 2 1 7 0 1 2 2 1 2 4 9 6 6 3 9 2 8 1 5 5 1 8 6 2 5 6 0 1 4 2 1 8 9 4 3 0 6 8 3 3 2 0 2 0 6 5 6 6 4 6 1 8 3 4 1 3 5 1 4 9 8 7 5 1 1 3 7 8 8 3 7 4 0 7 2 8 7 1 9 4 5 3 5 2 5 1 3 0 5 8 4 7 6 9 9 3 3 4 0 6 4 7 0 6 1 2 3 3 4 5 3 3 5 2 0 9 7 1 5 5 8 4 4 3 6 2 5 1 0 6 1 5 8 4 7 6 4 3 4 0 3 0 1 2 8 0 5 4 5 2 2 9 6 9 8 0 8 8 2 4 6 5 6 4 3 9 8 9 7 1 7 9 4 1 9 9 5 9 8 0 8 2 5 1 4 2 6 3 7 9 3 7 4 3 7 1 8 8 9 5 3 6 6]Process finished with exit code 0
源代码:
#coding:utf-8from __future__ import divisionimport sysreload(sys)sys.setdefaultencoding('utf-8')import timestart_time = time.time()# 从sklearn.datasets里导入手写体数字加载器。from sklearn.datasets import load_digits# 从通过数据加载器获得手写体数字的数码图像数据并储存在digits变量中。digits = load_digits()# 检视数据规模和特征维度。print digits.data.shape###########特征变量(一维)print digits.data##########目标变量(数字类别)print digits.target# 从sklearn.model_selection中导入train_test_split用于数据分割。from sklearn.model_selection 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 "训练样本个数:%s" %y_train.shapeprint "测试样本个数:%s" %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 confusion_matrixlabels1 = list(set(y_predict))conf_mat1 = confusion_matrix(y_test, y_predict, labels=labels1)print "SVM"print conf_mat1# 使用模型自带的评估函数进行准确性测评。print 'The Accuracy of Linear SVC is', lsvc.score(X_test, y_test)# 依然使用sklearn.metrics里面的classification_report模块对预测结果做更加详细的分析。from sklearn.metrics import classification_reportprint classification_report(y_test, y_predict, target_names=digits.target_names.astype(str))#############################################训练模型保存、载入、使用######################## 保存模型from sklearn.externals import joblibjoblib.dump(lsvc , 'c:/lsvc.pkl')#载入保存的模型lsvc2 = joblib.load('c:/lsvc.pkl')##预测y_pred = lsvc2.predict(X_test)print y_pred
阅读全文
0 0
- 【机器学习 sklearn】手写数字识别 SVM
- SVM - 手写数字识别
- SVM手写数字识别
- OpenCV机器学习:SVM分类器实现MNIST手写数字识别
- 机器学习 SVM sklearn
- 机器学习--knn手写数字识别系统
- SVM用于手写数字识别
- pca+svm手写数字识别
- 基于SVM手写数字识别
- SVM学习笔记(二)----手写数字识别
- 4 机器学习实践之手写数字识别- 神经网络识别
- 手写数字识别hebb和SVM
- SVM和Knn实现手写数字识别
- SVM手写数字的识别---python
- 【机器学习】k-近邻算法应用之手写数字识别
- 机器学习xgboost实战—手写数字识别
- 机器学习实践之手写数字识别 - 数据初识
- 关于利用机器学习进行手写数字的的识别
- 关于 QMainwindow设置布局layout失败 的解决方法
- 通过复合塑膜出has-a或“根据某物实现出”
- 为什么面试总是失败收场?
- Vue
- 企业信息架构与分布式系统原则
- 【机器学习 sklearn】手写数字识别 SVM
- 微信安卓客户端逆向分析
- 由无向图建邻接矩阵
- linux命令
- java“多态”的由来?
- CSS3 Gradient渐变
- Windows下 安装Python 模块
- 复杂查询的分页
- style中link标签和@import的区别