forked from jaidevd/nn-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
85 lines (68 loc) · 2.2 KB
/
Copy pathutils.py
File metadata and controls
85 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# JSM product code
#
# (C) Copyright 2017 Juxt SmartMandate Pvt Ltd
# All right reserved.
#
# This file is confidential and NOT open source. Do not distribute.
#
"""
"""
import numpy as np
from sklearn.datasets import make_moons, make_circles
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt
plt.style.use('ggplot')
def backprop_make_classification():
X, Y = make_circles(factor=0.1, noise=0.1)
Y = OneHotEncoder().fit_transform(Y.reshape(-1, 1)).toarray()
return X, Y
def make_classification(for_perceptron=False):
rng = np.random.RandomState(12345)
xx = rng.multivariate_normal([0.5, 0.5], [[0, 0.05], [0.05, 0]], size=(100,))
yy = rng.multivariate_normal([-0.5, -0.5], [[0, 0.05], [0.05, 0]], size=(100,))
X = np.r_[xx, yy]
Y = np.ones((200, 1))
if for_perceptron:
Y[:100, :] = -1
else:
Y[:100, :] = 0
return X, Y
def perceptron_make_moons():
X, y = make_moons(noise=0.04)
X[y == 0, :] += 0.8
y[y == 0] = -1
y = y.reshape(-1, 1)
return X, y
def backprop_make_moons():
X, Y = make_moons(noise=0.01)
Y = OneHotEncoder().fit_transform(Y.reshape(-1, 1)).toarray()
return X, Y
def draw_decision_boundary(weights, X, y, show=False):
"""Show the scatterplot of the data colored by the classes,
draw the decision line based on the weights."""
xx = np.linspace(X[:, 0].min(), X[:, 0].max())
yy = - (weights[0] * xx + weights[2]) / weights[1]
plt.figure(figsize=(6, 4))
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.plot(xx, yy)
if show:
plt.show()
def backprop_decision_boundary(predictor, X, y, show=False):
y = np.argmax(y, axis=1)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1))
Z = predictor(np.c_[xx.ravel(), yy.ravel()])
Z = Z.argmax(axis=1)
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y)
if show:
plt.show()
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))