1.简单非线性关系数据集测试(XOR)
X: Y
0 0 0
0 1 1
1 0 1
1 1 0
Code:
#!/usr/bin/env python
#-*-coding:utf-8-*-
#神经网络测试的例子
#简单非线性关系数据集测试(XOR)异或的运算
from NeuralNetwork import NeuralNetwork
import numpy as npnn=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.0022接近于0
实例2:手写数字识别
每个图片8*8
识别数字:0 1 2 3 4 5 6 7 8 9
code
#!/usr/bin/env python
#-*-coding:utf-8-*-
#手写数字的识别 神经网络算法,利用写好的神经网络算法测试
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_splitdigits=load_digits()
X=digits.data
y=digits.target
#特征值 0 1之间
X-=X.min()
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)
#转化为01,
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))
结果解释:
对角线上的值代表预测对的值,39代表0预测对了39次
第一行第四个数5个1代表预测错了4预测成了0