CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C1 - Neural Networks and Deep Learning/Week 3/Planar data classification with one hidden layer/planar_utils.py
Views: 4798
1
import matplotlib.pyplot as plt
2
import numpy as np
3
import sklearn
4
import sklearn.datasets
5
import sklearn.linear_model
6
7
def plot_decision_boundary(model, X, y):
8
# Set min and max values and give it some padding
9
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
10
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
11
h = 0.01
12
# Generate a grid of points with distance h between them
13
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
14
# Predict the function value for the whole grid
15
Z = model(np.c_[xx.ravel(), yy.ravel()])
16
Z = Z.reshape(xx.shape)
17
# Plot the contour and training examples
18
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
19
plt.ylabel('x2')
20
plt.xlabel('x1')
21
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
22
23
24
def sigmoid(x):
25
"""
26
Compute the sigmoid of x
27
28
Arguments:
29
x -- A scalar or numpy array of any size.
30
31
Return:
32
s -- sigmoid(x)
33
"""
34
s = 1/(1+np.exp(-x))
35
return s
36
37
def load_planar_dataset():
38
np.random.seed(1)
39
m = 400 # number of examples
40
N = int(m/2) # number of points per class
41
D = 2 # dimensionality
42
X = np.zeros((m,D)) # data matrix where each row is a single example
43
Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue)
44
a = 4 # maximum ray of the flower
45
46
for j in range(2):
47
ix = range(N*j,N*(j+1))
48
t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta
49
r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius
50
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
51
Y[ix] = j
52
53
X = X.T
54
Y = Y.T
55
56
return X, Y
57
58
def load_extra_datasets():
59
N = 200
60
noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
61
noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
62
blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
63
gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None)
64
no_structure = np.random.rand(N, 2), np.random.rand(N, 2)
65
66
return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
67