计算机视觉(CV)sklearn之分类算法与手写数字识别
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算机视觉(CV)sklearn之分类算法与手写数字识别相关的知识,希望对你有一定的参考价值。
【计算机视觉(CV)】sklearn之分类算法与手写数字识别
(文章目录)
前言
(一)、任务描述
- 数据介绍
- 数据读取与存储形式
- sklearn分类模型
- 代码实现与结果
(二)、环境配置
本实践代码运行的环境配置如下:Python版本为3.7,PaddlePaddle版本为2.0.0,操作平台为AI Studio。大部分深度学习项目都要经过以下几个过程:数据准备、模型配置、模型训练、模型评估。
import paddle
import numpy as np
import matplotlib.pyplot as plt
print(paddle.__version__)
# cpu/gpu环境选择,在 paddle.set_device() 输入对应运行设备。
# device = paddle.set_device(gpu)
一、数据集介绍
- Training set images: train-images-idx3-ubyte.gz (9.9 MB, 解压后 47 MB, 包含 60,000 个样本)
- Training set labels: train-labels-idx1-ubyte.gz (29 KB, 解压后 60 KB, 包含 60,000 个标签)
- Test set images: t10k-images-idx3-ubyte.gz (1.6 MB, 解压后 7.8 MB, 包含 10,000 个样本)
- Test set labels: t10k-labels-idx1-ubyte.gz (5KB, 解压后 10 KB, 包含 10,000 个标签)
二、数据读取与存储形式
!unzip data/data7869/mnist.zip
输出结果如下图1所示:
!gzip -dfq mnist/train-labels-idx1-ubyte.gz
!gzip -dfq mnist/t10k-labels-idx1-ubyte.gz
!gzip -dfq mnist/train-images-idx3-ubyte.gz
!gzip -dfq mnist/t10k-images-idx3-ubyte.gz
(一)、导入相关包
首先我们引入本案例需要的所有模块
import struct,os
import numpy as np
from array import array as pyarray
from numpy import append, array, int8, uint8, zeros
import matplotlib.pyplot as plt
%matplotlib inline
(二)、参数配置
def load_mnist(image_file, label_file, path="mnist"):
digits=np.arange(10)
fname_image = os.path.join(path, image_file)
fname_label = os.path.join(path, label_file)
flbl = open(fname_label, rb)
magic_nr, size = struct.unpack(">II", flbl.read(8))
lbl = pyarray("b", flbl.read())
flbl.close()
fimg = open(fname_image, rb)
magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
img = pyarray("B", fimg.read())
fimg.close()
ind = [ k for k in range(size) if lbl[k] in digits ]
N = len(ind)
images = zeros((N, rows*cols), dtype=uint8)
labels = zeros((N, 1), dtype=int8)
for i in range(len(ind)):
images[i] = array(img[ ind[i]*rows*cols : (ind[i]+1)*rows*cols ]).reshape((1, rows*cols))
labels[i] = lbl[ind[i]]
return images, labels
train_image, train_label = load_mnist("train-images-idx3-ubyte", "train-labels-idx1-ubyte")
test_image, test_label = load_mnist("t10k-images-idx3-ubyte", "t10k-labels-idx1-ubyte")
(三)、数据展示
import matplotlib.pyplot as plt
def show_image(imgdata,imgtarget,show_column, show_row):
#注意这里的show_column*show_row==len(imgdata)
for index,(im,it) in enumerate(list(zip(imgdata,imgtarget))):
xx = im.reshape(28,28)
plt.subplots_adjust(left=1, bottom=None, right=3, top=2, wspace=None, hspace=None)
plt.subplot(show_row, show_column, index+1)
plt.axis(off)
plt.imshow(xx , cmap=gray,interpolation=nearest)
plt.title(label:%i % it)
# 显示训练集前50数字
show_image(train_image[:50], train_label[:50], 10,5)
输出结果如下图2所示:
三、sklearn 分类模型
本文使用的sklearn分类模型有:
- K-Nearest Neighbor Classification
- Naive Bayes
- Decision Tree
- Random Forest
- Logistic Regression
- Support Vector Machine
https://blog.csdn.net/u013909139/article/details/69740089
http://cuijiahua.com/blog/2017/11/ml_8_svm_1.html
http://cuijiahua.com/blog/2017/11/ml_9_svm_2.html
数据归一化
https://blog.csdn.net/program_developer/article/details/78637711
此处采用min-max标准化将数据进行归一化操作。
image = [im/255.0 for im in image]
train_image=[im/255.0 for im in train_image]
test_image=[im/255.0 for im in test_image]
四、sklearn模型分类
(一)、K-Nearest Neighbor Classification
print(len(train_image))
print(len(train_label))
输出结果如下图3所示:
from sklearn.metrics import accuracy_score,classification_report
from sklearn.neighbors import KNeighborsClassifier
knc = KNeighborsClassifier(n_neighbors=10)
knc.fit(train_image,train_label.ravel())
输出结果如下图4所示:
predict = knc.predict(test_image)
print("accuracy_score: %.4lf" % accuracy_score(predict,test_label))
输出结果如下图5所示:
print("Classification report for classifier %s:\\n%s\\n" % (knc, classification_report(test_label, predict)))
输出结果如下图6所示:
(二)、Naive Bayes
from sklearn.naive_bayes import MultinomialNB
mnb = MultinomialNB()
mnb.fit(train_image,train_label)
predict = mnb.predict(test_image)
print("accuracy_score: %.4lf" % accuracy_score(predict,test_label))
print("Classification report for classifier %s:\\n%s\\n" % (mnb, classification_report(test_label, predict)))
输出结果如下图7所示:
(三)、Decision Tree
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
dtc.fit(train_image,train_label)
predict = dtc.predict(test_image)
print("accuracy_score: %.4lf" % accuracy_score(predict,test_label))
print("Classification report for classifier %s:\\n%s\\n" % (dtc, classification_report(test_label, predict)))
输出结果如下图8所示:
(四)、Random Forest
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()
rfc.fit(train_image,train_label)
predict = rfc.predict(test_image)
print("accuracy_score: %.4lf" % accuracy_score(predict,test_label))
print("Classification report for classifier %s:\\n%s\\n" % (rfc, classification_report(test_label, predict)))
(五)、Logistic Regression
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(train_image,train_label)
predict = lr.predict(test_image)
print("accuracy_score: %.4lf" % accuracy_score(predict,test_label))
print("Classification report for classifier %s:\\n%s\\n" % (lr, classification_report(test_label, predict)))
输出结果如下图9所示:
(六)、Support Vector Machine
from sklearn.svm import SVC
svc = SVC()
svc.fit(train_image,train_label)
predict = svc.predict(test_image)
print("accuracy_score: %.4lf" % accuracy_score(predict,test_label))
print("Classification report for classifier %s:\\n%s\\n" % (svc, classification_report(test_label, predict)))
各分类模型准确度比较:
<table><thead><tr><th>模型</th><th>准确度</th></tr></thead><tbody><tr><td>K-Nearest Neighbor Classification</td><td>0.9665</td></tr><tr><td>Naive Bayes</td><td>0.8375</td></tr><tr><td>Decision Tree</td><td>0.8758</td></tr><tr><td>Random Forest</td><td>0.9486</td></tr><tr><td>Logistic Regression</td><td>0.9202</td></tr><tr><td>Support Vector Machine</td><td>0.9769</td></tr></tbody></table>
我们可以将预测结果展示出来:
import matplotlib.pyplot as plt
def show_image(imgtestdata, imgtesttarget, show_column, show_row):
#注意这里的show_column*show_row==len(imgtestdata)
for index,(im,it) in enumerate(list(zip(imgtestdata, imgtesttarget))):
xx = im.reshape(28,28)
plt.subplots_adjust(left=1, bottom=None, right=3, top=4, wspace=None, hspace=None)
plt.subplot(show_row, show_column, index+1)
plt.axis(off)
plt.imshow(xx , cmap=gray,interpolation=nearest)
plt.title(predict:%i % it)
show_image(test_image[:100], predict, 10, 10)
部分输出结果如下图10所示:
总结
本系列文章内容为根据清华社出版的《自然语言处理实践》所作的相关笔记和感悟,其中代码均为基于百度飞桨开发,若有任何侵权和不妥之处,请私信于我,定积极配合处理,看到必回!!!
最后,引用本次活动的一句话,来作为文章的结语~( ̄▽ ̄~)~:
【**学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。**】
以上是关于计算机视觉(CV)sklearn之分类算法与手写数字识别的主要内容,如果未能解决你的问题,请参考以下文章
自然语言处理(nlp)比计算机视觉(cv)发展缓慢,而且更难!
《计算机视觉和图像处理简介 - 中英双语版》:基于PyTorch Softmax 进行 MNIST 手写数字分类Digit Classification with Softmax