码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-02-26 22:24:49      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:数据集   cti   load   ural   test   The   metrics   pre   ica   

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

# 每个图片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()

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

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

标签:数据集   cti   load   ural   test   The   metrics   pre   ica   

原文地址:https://www.cnblogs.com/lyywj170403/p/10440509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!