Path: blob/main/C3 - Unsupervised Learning, Recommenders, Reinforcement Learning/week1/C3W1A/C3W1A2/utils.py
3567 views
import numpy as np1import matplotlib.pyplot as plt23def load_data():4X = np.load("data/X_part1.npy")5X_val = np.load("data/X_val_part1.npy")6y_val = np.load("data/y_val_part1.npy")7return X, X_val, y_val89def load_data_multi():10X = np.load("data/X_part2.npy")11X_val = np.load("data/X_val_part2.npy")12y_val = np.load("data/y_val_part2.npy")13return X, X_val, y_val141516def multivariate_gaussian(X, mu, var):17"""18Computes the probability19density function of the examples X under the multivariate gaussian20distribution with parameters mu and var. If var is a matrix, it is21treated as the covariance matrix. If var is a vector, it is treated22as the var values of the variances in each dimension (a diagonal23covariance matrix24"""2526k = len(mu)2728if var.ndim == 1:29var = np.diag(var)3031X = X - mu32p = (2* np.pi)**(-k/2) * np.linalg.det(var)**(-0.5) * \33np.exp(-0.5 * np.sum(np.matmul(X, np.linalg.pinv(var)) * X, axis=1))3435return p3637def visualize_fit(X, mu, var):38"""39This visualization shows you the40probability density function of the Gaussian distribution. Each example41has a location (x1, x2) that depends on its feature values.42"""4344X1, X2 = np.meshgrid(np.arange(0, 35.5, 0.5), np.arange(0, 35.5, 0.5))45Z = multivariate_gaussian(np.stack([X1.ravel(), X2.ravel()], axis=1), mu, var)46Z = Z.reshape(X1.shape)4748plt.plot(X[:, 0], X[:, 1], 'bx')4950if np.sum(np.isinf(Z)) == 0:51plt.contour(X1, X2, Z, levels=10**(np.arange(-20., 1, 3)), linewidths=1)5253# Set the title54plt.title("The Gaussian contours of the distribution fit to the dataset")55# Set the y-axis label56plt.ylabel('Throughput (mb/s)')57# Set the x-axis label58plt.xlabel('Latency (ms)')5960