# Test: binary classification for colored points
import numpy as np
import matplotlib.pyplot as plt
from nn_model import *
from predict import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
# Generate training data
np.random.seed(1)
m = 400 # number of samples
N = int(m / 2) # number of points per class
D = 2 # dimension
X = np.zeros((m, D)) # data matrix where each row is a single example
Y = np.zeros((m ,1), dtype = 'uint8') # labels vector (0 for red, 1 for blue)
a = 4 # maximum radius of the flower
for j in range(2):
ix = range(N * j, N * (j + 1))
theta = np.linspace(j * 3.12, (j + 1) * 3.12, N) + np.random.randn(N) * 0.2
radius = a * np.sin(4 * theta) + np.random.randn(N) * 0.2
X[ix] = np.c_[radius * np.sin(theta), radius * np.cos(theta)]
Y[ix] = j
X = X.T
Y = Y.T
# Model training
parameters = nn_model(X, Y, nh = 4, num_iterations = 10000, print_cost = True)
# Accuracy
predictions = predict(X, parameters)
print('Training accuracy = %.2f ' % float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100) + '%')
# Binary classification
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
h = 0.01 # Meshgrid interval
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Generate a grid of points with interval h
Z = predict(np.c_[xx.ravel(), yy.ravel()].T, parameters)
Z = Z.reshape(xx.shape)
plt.figure(2)
plt.contourf(xx, yy, Z, cmap = plt.cm.Spectral)
plt.xlabel('x1')
plt.ylabel('x2')
plt.scatter(X[0, :], X[1, :], c = Y.flatten(), s = 40, cmap = plt.cm.Spectral)
plt.title("Decision Boundary for hidden layer size " + str(4))
# Tuning hidden layer size
hidden_layer_sizes = [1, 3, 4, 5, 6, 20]
plt.figure(3)
for i, nh in enumerate(hidden_layer_sizes):
plt.subplot(3, 2, i + 1)
plt.title('Hidden layer of size %d' % nh)
parameters = nn_model(X, Y, nh, num_iterations = 5000)
Z = predict(np.c_[xx.ravel(), yy.ravel()].T, parameters)
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.xlabel('x1')
plt.ylabel('x2')
plt.scatter(X[0, :], X[1, :], c=Y.flatten(), s=40, cmap=plt.cm.Spectral)
predictions = predict(X, parameters)
accuracy = float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100)
print('Training accuracy for {} hidden units: {} % '.format(nh, accuracy))
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1、资源内容:基于Python实现浅层神经网络案例(源码).rar 2、适用人群:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业或毕业设计,作为“参考资料”使用。 3、解压说明:本资源需要电脑端使用WinRAR、7zip等解压工具进行解压,没有解压工具的自行百度下载即可。 4、免责声明:本资源作为“参考资料”而不是“定制需求”不一定能够满足所有人的需求,需要有一定的基础能够看懂代码,能够自行调试代码并解决报错,能够自行添加功能修改代码。由于作者大厂工作较忙,不提供答疑服务,如不存在资源缺失问题概不负责,谢谢理解。
资源推荐
资源详情
资源评论
收起资源包目录
基于Python实现浅层神经网络案例(源码).rar (10个子文件)
基于Python实现浅层神经网络案例(源码)
forward.py 1KB
Planar data classification with one hidden layer v4.ipynb 384KB
compute_cost.py 795B
backward.py 2KB
predict.py 1KB
nn_model.py 2KB
initialize.py 1KB
update_parameters.py 2KB
test.py 3KB
sigmoid.py 262B
共 10 条
- 1
资源评论
Matlab仿真实验室
- 粉丝: 2w+
- 资源: 2180
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功