博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 数字识别 SVM
阅读量:4216 次
发布时间:2019-05-26

本文共 2051 字,大约阅读时间需要 6 分钟。

利用python 内置的digits 数据进行 数字识别

# The data that we are interested in is made of 8x8 images of digits, let's# have a look at the first 4 images, stored in the `images` attribute of the# dataset.  If we were working from image files, we could load them using# matplotlib.pyplot.imread.  Note that each image must have the same size. For these# images, we know which digit they represent: it is given in the 'target' of# the dataset.from sklearn import datasets,svm,metricsimport numpy as npimport matplotlib.pyplot as pltdigits = datasets.load_digits()images_and_labels = list(zip(digits.images,digits.target))#print(images_and_labels[0])#print(images_and_labels[0:4])#显示训练集的前4个结果for index,(image,label) in enumerate(images_and_labels[:4]):    plt.subplot(2,4,index+1)    plt.axis('off')    plt.imshow(image,cmap=plt.cm.gray_r,interpolation='nearest')    plt.title('Training :%i' %label)#样本数n_samples = len(digits.images)#print(digits.images.shape)data = digits.images.reshape((n_samples,-1)) #和 reshape(n_samples,64)效果一样 可以用下面的这条验证#print(np.all(digits.images.reshape((1797,-1))==digits.data)) #trueclassifier = svm.SVC(gamma=0.001)#对前一半样本进行训练,构建模型classifier.fit(data[:n_samples//2],digits.target[:n_samples//2])#对后半部分数据进行验证,期望的预测结果expected = digits.target[n_samples//2:]#真实的预测结果predicted = classifier.predict(data[n_samples//2:])#预测结果与真实结果进行对比,得出预测详细信息(正确率等)print("Classification report for classifier %s:\n%s\n"      % (classifier, metrics.classification_report(expected, predicted)))print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))#print(predicted) 所有的预测结果#将测试数据用zip构建城dict 进行图像与预测结果的对应images_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))#对结果进行显示for index, (image, prediction) in enumerate(images_and_predictions[:4]): #只是画出了前四个 预测的结果    plt.subplot(2, 4, index + 5) #2*4的图 第index+5部分    plt.axis('off')#不显示坐标信息    #显示图片(灰色)    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')    #在图片上方显示预测结果,方便直观看出正确性    plt.title('Prediction: %i' % prediction)plt.show()

上4个图是训练的时候画的,下面4个是预测的(前4张图)
你可能感兴趣的文章
【屌丝程序的口才逆袭演讲稿50篇】第五篇:不要给自己找任何借口【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第七篇:请留意我们身边的风景 【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第八篇:坚持的力量 【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第九篇:春节那些事-过年回家不需要理由【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十一篇:马云乌镇40分钟演讲实录【张振华.Jack】
查看>>
Java并发编程从入门到精通 张振华.Jack --我的书
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十二篇:世界上最快的捷径【张振华.Jack】
查看>>
Android中Java代码和XML布局效率问题
查看>>
android TextView属性大全(转)
查看>>
Conclusion for Resource Management
查看>>
Conclusion for Constructors,Destructors,and Assignment Operators
查看>>
Conclusion for Accustoming Yourself to C++
查看>>
面试题1:赋值运算函数(offer)
查看>>
Mark : MessagePack简介及使用
查看>>
Mark : hive文件存储格式
查看>>
mark : hadoop 四种压缩格式
查看>>
All Things OpenTSDB
查看>>
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>