Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C1 - Neural Networks and Deep Learning/Week 3/Planar data classification with one hidden layer/planar_utils.py
Views: 4798
import matplotlib.pyplot as plt1import numpy as np2import sklearn3import sklearn.datasets4import sklearn.linear_model56def plot_decision_boundary(model, X, y):7# Set min and max values and give it some padding8x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 19y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 110h = 0.0111# Generate a grid of points with distance h between them12xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))13# Predict the function value for the whole grid14Z = model(np.c_[xx.ravel(), yy.ravel()])15Z = Z.reshape(xx.shape)16# Plot the contour and training examples17plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)18plt.ylabel('x2')19plt.xlabel('x1')20plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)212223def sigmoid(x):24"""25Compute the sigmoid of x2627Arguments:28x -- A scalar or numpy array of any size.2930Return:31s -- sigmoid(x)32"""33s = 1/(1+np.exp(-x))34return s3536def load_planar_dataset():37np.random.seed(1)38m = 400 # number of examples39N = int(m/2) # number of points per class40D = 2 # dimensionality41X = np.zeros((m,D)) # data matrix where each row is a single example42Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue)43a = 4 # maximum ray of the flower4445for j in range(2):46ix = range(N*j,N*(j+1))47t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta48r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius49X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]50Y[ix] = j5152X = X.T53Y = Y.T5455return X, Y5657def load_extra_datasets():58N = 20059noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)60noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)61blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)62gaussian_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)63no_structure = np.random.rand(N, 2), np.random.rand(N, 2)6465return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure6667