• 机器学习-神经网络算法应用(二)


    1. 简单非线性关系数据集测试(XOR):

     
    X:                  Y
    0 0                 0
    0 1                 1
    1 0                 1
    1 1                 0
     
    # -*- coding:utf-8 -*-
    from NeuralNetwork import NeuralNetwork
    import numpy as np
    
    nn = NeuralNetwork([2, 2, 1], 'tanh')
    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    y = np.array([0, 1, 1, 0])
    nn.fit(X, y)
    for i in [[0, 0], [0, 1], [1, 0], [1, 1]]:
        print(i, nn.predict(i))
    
    结果:
    [0, 0] [-0.01209026]
    [0, 1] [ 0.99815739]
    [1, 0] [ 0.99815649]
    [1, 1] [-0.01949152]
     
    2. 手写数字识别:
     
    每个图片8x8 
    识别数字:0,1,2,3,4,5,6,7,8,9
    查看数据集:
    # -*- coding:utf-8 -*-
    from sklearn.datasets import load_digits
    import pylab as  pl
    
    digits = load_digits()
    print(digits.data.shape)
    
    pl.gray()
    pl.matshow(digits.images[0])
    pl.show()
    

     结果:(1797, 64) 1797个图片实例,每个实例有8x8=64个特征向量(像素点)

    # -*- coding:utf-8 -*-

    # 每个图片8x8 识别数字:0,1,2,3,4,5,6,7,8,9

    import numpy as np
    from sklearn.datasets import load_digits
    from sklearn.metrics import confusion_matrix, classification_report
    from sklearn.preprocessing import LabelBinarizer
    from NeuralNetwork import NeuralNetwork
    from sklearn.cross_validation import train_test_split

    #加载数据
    digits = load_digits()
    x = digits.data #特征向量
    y = digits.target #类标签
    x -= x.min() # normalize the values to bring them into the range 0-1
    x /= x.max() #所有x减去他的最小值,再除以他的最大值

    nn = NeuralNetwork([64, 100, 10], 'logistic')
    x_train, x_test, y_train, y_test = train_test_split(x, y)
    #转化为0 1
    labels_train = LabelBinarizer().fit_transform(y_train)
    labels_test = LabelBinarizer().fit_transform(y_test)

    print("start fitting")
    nn.fit(x_train, labels_train, epochs = 3000)
    predictions = []
    for i in range(x_test.shape[0]):
    o = nn.predict(x_test[i])
    predictions.append(np.argmax(o))
    print(confusion_matrix(y_test, predictions))
    print(classification_report(y_test, predictions))
     
     结果:
    start fitting
    [[48  0  0  0  0  0  0  0  0  0]
     [ 0 37  1  0  0  0  0  0  0  4]
     [ 0  1 44  1  0  0  0  0  0  0]
     [ 0  0  0 44  0  1  0  1  0  0]
     [ 1  1  0  0 39  0  0  1  1  0]
     [ 0  0  0  0  0 49  1  0  0  0]
     [ 0  1  0  0  0  0 43  0  0  0]
     [ 0  0  0  0  0  0  0 33  1  0]
     [ 0  3  0  1  0  4  1  0 35  1]
     [ 0  0  0  6  0  2  0  4  0 40]]
                 precision    recall  f1-score   support
    
              0       0.98      1.00      0.99        48
              1       0.86      0.88      0.87        42
              2       0.98      0.96      0.97        46
              3       0.85      0.96      0.90        46
              4       1.00      0.91      0.95        43
              5       0.88      0.98      0.92        50
              6       0.96      0.98      0.97        44
              7       0.85      0.97      0.90        34
              8       0.95      0.78      0.85        45
              9       0.89      0.77      0.82        52
    
    avg / total       0.92      0.92      0.91       450
    

      

    # -*- coding:utf-8 -*-
    from sklearn.datasets import load_digits
    import pylab as pl

    digits = load_digits()
    print(digits.data.shape)

    pl.gray()
    pl.matshow(digits.images[0])
    pl.show()
  • 相关阅读:
    电赛菜鸟营培训(五)——OLED屏幕的使用
    网站构建学习笔记(0)——基本概念了解及资源学习(copy自w3school)
    AppInventor学习笔记(五)——瓢虫快跑应用学习
    电赛菜鸟营培训(四)——STM32F103CB之ADC转换
    电赛菜鸟营培训(三)——STM32F103CB之串口通信
    [I2C]pca9555(i2c-gpio扩展)应用层测试代码
    [shell]简单的shell提示和参数脚本
    [Busybox]Busybox制作文件系统
    [boostrap]debian下为arm创建debian和emdebian文件系统
    [debootstrap]制作基于arm平台的debian文件系统
  • 原文地址:https://www.cnblogs.com/lyywj170403/p/10440509.html
Copyright © 2020-2023  润新知