-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
44 lines (37 loc) · 1.51 KB
/
utils.py
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
# C2_W1 Utilities
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Plot multi-class training points
def plot_mc_data(X, y, class_labels=None, legend=False,size=40):
classes = np.unique(y)
for i in classes:
label = class_labels[i] if class_labels else "class {}".format(i)
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], cmap=plt.cm.Paired,
edgecolor='black', s=size, label=label)
if legend: plt.legend()
#Plot a multi-class categorical decision boundary
# This version handles a non-vector prediction (adds a for-loop over points)
def plot_cat_decision_boundary(X,predict , class_labels=None, legend=False, vector=True):
# create a mesh to points to plot
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 = max(x_max-x_min, y_max-y_min)/200
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
points = np.c_[xx.ravel(), yy.ravel()]
#make predictions for each point in mesh
if vector:
Z = predict(points)
else:
Z = np.zeros((len(points),))
for i in range(len(points)):
Z[i] = predict(points[i].reshape(1,2))
Z = Z.reshape(xx.shape)
#contour plot highlights boundaries between values - classes in this case
plt.figure()
plt.contour(xx, yy, Z, colors='g')
plt.axis('tight')