6.3 神经网络算法(Nerual Networks)应用(下)

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

 

X:                  Y

0 0                 0

0 1                 1

1 0                 1

1 1                 0

Code:

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))

 

2. 手写数字识别:

 

每个图片8x8 

识别数字:0,1,2,3,4,5,6,7,8,9

Code:

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()

 

nn = NeuralNetwork([64,100,10],'logistic')  

X_train, X_test, y_train, y_test = train_test_split(X, y)  

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值