python--MLP神经网络实现手写数字识别

系统 2010 0
  • 概述

神经网路顾名思义将生物的神经系统中的兴奋与抑制比作计算机中的0和1

知识点:

  1. 神经网络原理
  2. 神经网络中的非线性矫正
  3. 神经网络参数设置
  • 参数设置

重要参数:

activation:隐藏单元进行非线性化的方法,一共4总:identity,logistic,tanh,relu

alpha:正则化参数,默认为0.0001,参数越大算法越简单

hidden_layer_size:设置隐藏层的结点和层数:[10,10]表示2层,每层结点为10       

 

  • 图像分析

            
              import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_wine

from sklearn.model_selection import train_test_split
wine = load_wine()
X = wine.data[:,:2]#只取前2个属性
y = wine.target
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0)

mlp = MLPClassifier(solver = 'lbfgs',hidden_layer_sizes=[100,100],activation='tanh',alpha=1)
mlp.fit(X_train,y_train)

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

cmap_light = ListedColormap(['#FFAAAA','#AAFFAA','#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000','#00FF00','#0000FF'])
                            
x_min, x_max = X[:,0].min() -1,X[:,0].max()+1
y_min, y_max = X[:,1].min() -1,X[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,.02),np.arange(y_min,y_max,.02))
z = mlp.predict(np.c_[xx.ravel(),yy.ravel()])
z = z.reshape(xx.shape)

plt.figure()
plt.pcolormesh(xx,yy,z,cmap=cmap_light)

plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap_bold,edgecolor='k',s=20)
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())

plt.show()

print("训练得分:{:.2f}".format(mlp.score(X_train,y_train)))
print("测试得分:{:.2f}".format(mlp.score(X_test,y_test)))
            
          

通过内置红酒数据集可画出神经网络算法图:

python--MLP神经网络实现手写数字识别_第1张图片

将正则化参数恢复为默认后:

mlp = MLPClassifier(solver = 'lbfgs',hidden_layer_sizes=[100,100],activation='tanh')

python--MLP神经网络实现手写数字识别_第2张图片

可见参数对效果的影响。

 

  • 实例--手写识别

使用内置数据集“load_digits

查看参数:

            
              print(digits.keys())#数据集中的建
print(digits.data[0])#第一个数据
print(digits.target[0])#第一个数据的类型
print(digits.DESCR)#描述
            
          
            
              dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])
[ 0.  0.  5. 13.  9.  1.  0.  0.  0.  0. 13. 15. 10. 15.  5.  0.  0.  3.
 15.  2.  0. 11.  8.  0.  0.  4. 12.  0.  0.  8.  8.  0.  0.  5.  8.  0.
  0.  9.  8.  0.  0.  4. 11.  0.  1. 12.  7.  0.  0.  2. 14.  5. 10. 12.
  0.  0.  0.  0.  6. 13. 10.  0.  0.  0.]
0
.. _digits_dataset:

Optical recognition of handwritten digits dataset
--------------------------------------------------

**Data Set Characteristics:**

    :Number of Instances: 5620
    :Number of Attributes: 64
    :Attribute Information: 8x8 image of integer pixels in the range 0..16.
    :Missing Attribute Values: None
    :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)
    :Date: July; 1998

This is a copy of the test set of the UCI ML hand-written digits datasets
https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits

The data set contains images of hand-written digits: 10 classes where
each class refers to a digit.

Preprocessing programs made available by NIST were used to extract
normalized bitmaps of handwritten digits from a preprinted form. From a
total of 43 people, 30 contributed to the training set and different 13
to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of
4x4 and the number of on pixels are counted in each block. This generates
an input matrix of 8x8 where each element is an integer in the range
0..16. This reduces dimensionality and gives invariance to small
distortions.

For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.
T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.
L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,
1994.

.. topic:: References

  - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their
    Applications to Handwritten Digit Recognition, MSc Thesis, Institute of
    Graduate Studies in Science and Engineering, Bogazici University.
  - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.
  - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.
    Linear dimensionalityreduction using relevance weighted LDA. School of
    Electrical and Electronic Engineering Nanyang Technological University.
    2005.
  - Claudio Gentile. A New Approximate Maximal Margin Classification
    Algorithm. NIPS. 2000.
            
          

通过描述幸喜可以发现图片为8*8的大小

完整代码:

            
              #MNIST数据集
from sklearn.datasets import load_digits
digits = load_digits()
X=digits.data
y=digits.target
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0)

mlp = MLPClassifier(solver = 'lbfgs',hidden_layer_sizes=[100,100],activation='relu',random_state=62)
mlp.fit(X_train,y_train)

print(X_train.shape,y_train.shape,X_test.shape,y_test.shape)
print("训练得分:{:.2f}".format(mlp.score(X_train,y_train)))
print("测试得分:{:.2f}".format(mlp.score(X_test,y_test)))
#导入图像处理工具
from PIL import Image

image = Image.open('1.png').convert('F')
image = image.resize((8,8))
arr = []

for i in range(8):
    for j in range(8):
        pixel = 1.0 - float(image.getpixel((j,i)))/255
        arr.append(pixel)
        
arr1 = np.array(arr).reshape(1,-1)

for i in range(10):
    print('{}的概率为:{}'.format(i,mlp.predict_proba(arr1)[0][i]))
print('结果为:{}'.format(mlp.predict(arr1)[0]))
            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论