Path: blob/main/C2 - Advanced Learning Algorithms/week1/C2W1A1/utils.py
3588 views
# C2_W1 Utilities1import numpy as np2import matplotlib.pyplot as plt3from sklearn.datasets import make_blobs45def sigmoid(x):6return 1 / (1 + np.exp(-x))78# Plot multi-class training points9def plot_mc_data(X, y, class_labels=None, legend=False,size=40):10classes = np.unique(y)11for i in classes:12label = class_labels[i] if class_labels else "class {}".format(i)13idx = np.where(y == i)14plt.scatter(X[idx, 0], X[idx, 1], cmap=plt.cm.Paired,15edgecolor='black', s=size, label=label)16if legend: plt.legend()171819#Plot a multi-class categorical decision boundary20# This version handles a non-vector prediction (adds a for-loop over points)21def plot_cat_decision_boundary(X,predict , class_labels=None, legend=False, vector=True):2223# create a mesh to points to plot24x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 125y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 126h = max(x_max-x_min, y_max-y_min)/20027xx, yy = np.meshgrid(np.arange(x_min, x_max, h),28np.arange(y_min, y_max, h))29points = np.c_[xx.ravel(), yy.ravel()]3031#make predictions for each point in mesh32if vector:33Z = predict(points)34else:35Z = np.zeros((len(points),))36for i in range(len(points)):37Z[i] = predict(points[i].reshape(1,2))38Z = Z.reshape(xx.shape)3940#contour plot highlights boundaries between values - classes in this case41plt.figure()42plt.contour(xx, yy, Z, colors='g')43plt.axis('tight')4445