├── Certificates ├── - ├── Machine Learning.pdf ├── Advanced Learning Algorithms.pdf ├── Supervised Machine Learning- Regression and Classification.pdf └── Unsupervised Learning, Recommenders, Reinforcement Learning.pdf ├── Supervised Machine Learning: Regression and Classification ├── Readme.md ├── W1 │ ├── C1_W1_Lab02_Course_Preview_Soln.ipynb │ ├── C1_W1_Lab01_Python_Jupyter_Soln.ipynb │ └── C1_W1_Lab04_Cost_function_Soln.py └── W3 │ ├── C1_W3_Lab08_Overfitting_Soln.ipynb │ ├── C1_W3_Lab01_Classification_Soln.ipynb │ ├── C1_W3_Lab03_Decision_Boundary_Soln.ipynb │ ├── C1_W3_Lab05_Cost_Function_Soln.ipynb │ └── C1_W3_Lab02_Sigmoid_function_Soln.ipynb ├── Unsupervised Learning, Recommenders, Reinforcement Learning ├── W1 │ ├── 1 │ ├── ex7_X.npy │ ├── X_part1.npy │ ├── X_part2.npy │ ├── figure1.png │ ├── figure2.png │ ├── figure3.png │ ├── bird_small.png │ ├── figure 1.png │ ├── figure 2.png │ ├── figure 3.png │ ├── X_val_part1.npy │ ├── X_val_part2.npy │ ├── y_val_part1.npy │ ├── y_val_part2.npy │ ├── utils.py │ └── public_tests.py ├── W2 │ ├── 1 │ ├── content_item_train_header.txt │ ├── content_user_train_header.txt │ ├── content_user_to_genre.pickle │ ├── recsys_utils.py │ ├── public_tests.py │ └── small_movies_b.csv └── W3 │ ├── lunar_lander_model.h5 │ ├── public_tests.py │ └── utils.py ├── data ├── ex7_X.npy ├── X_part1.npy ├── X_part2.npy ├── X_val_part1.npy ├── X_val_part2.npy ├── y_val_part1.npy ├── y_val_part2.npy ├── lunar_lander_model.h5 ├── content_user_to_genre.pickle ├── content_item_train_header.txt ├── content_user_train_header.txt ├── ex1data2.txt ├── ex1data1.txt └── small_movies_b.csv ├── src ├── lab_utils_multiclass.py ├── lab_utils_softmax.py ├── public_tests.py ├── lab_neurons_utils.py ├── lab_utils_relu.py ├── deeplearning.mplstyle ├── utils.py ├── lab_coffee_utils.py ├── autils.py ├── lab_utils_multiclass_TF.py ├── plt_logistic_loss.py ├── public_tests_a1.py ├── plt_one_addpt_onclick.py ├── lab_utils_common.py └── recsysNN_utils.py ├── Advanced Learning Algorithms ├── W4 │ └── public_tests.py └── W2 │ └── C2_W2_Relu.ipynb └── README.md /Certificates/-: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/1: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/1: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /data/ex7_X.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/ex7_X.npy -------------------------------------------------------------------------------- /data/X_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/X_part1.npy -------------------------------------------------------------------------------- /data/X_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/X_part2.npy -------------------------------------------------------------------------------- /data/X_val_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/X_val_part1.npy -------------------------------------------------------------------------------- /data/X_val_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/X_val_part2.npy -------------------------------------------------------------------------------- /data/y_val_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/y_val_part1.npy -------------------------------------------------------------------------------- /data/y_val_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/y_val_part2.npy -------------------------------------------------------------------------------- /data/lunar_lander_model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/lunar_lander_model.h5 -------------------------------------------------------------------------------- /Certificates/Machine Learning.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Certificates/Machine Learning.pdf -------------------------------------------------------------------------------- /data/content_user_to_genre.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/data/content_user_to_genre.pickle -------------------------------------------------------------------------------- /data/content_item_train_header.txt: -------------------------------------------------------------------------------- 1 | movie id,year,ave rating,Action,Adventure,Animation,Children,Comedy,Crime,Documentary,Drama,Fantasy,Horror,Mystery,Romance,Sci-Fi,Thriller 2 | -------------------------------------------------------------------------------- /Certificates/Advanced Learning Algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Certificates/Advanced Learning Algorithms.pdf -------------------------------------------------------------------------------- /data/content_user_train_header.txt: -------------------------------------------------------------------------------- 1 | user id,rating count,rating ave,Action,Adventure,Animation,Children,Comedy,Crime,Documentary,Drama,Fantasy,Horror,Mystery,Romance,Sci-Fi,Thriller 2 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/ex7_X.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/ex7_X.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_part1.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_part2.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure1.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure2.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure3.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/content_item_train_header.txt: -------------------------------------------------------------------------------- 1 | movie id,year,ave rating,Action,Adventure,Animation,Children,Comedy,Crime,Documentary,Drama,Fantasy,Horror,Mystery,Romance,Sci-Fi,Thriller 2 | -------------------------------------------------------------------------------- /Certificates/Supervised Machine Learning- Regression and Classification.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Certificates/Supervised Machine Learning- Regression and Classification.pdf -------------------------------------------------------------------------------- /Certificates/Unsupervised Learning, Recommenders, Reinforcement Learning.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Certificates/Unsupervised Learning, Recommenders, Reinforcement Learning.pdf -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/bird_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/bird_small.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure 1.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure 2.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/figure 3.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/content_user_train_header.txt: -------------------------------------------------------------------------------- 1 | user id,rating count,rating ave,Action,Adventure,Animation,Children,Comedy,Crime,Documentary,Drama,Fantasy,Horror,Mystery,Romance,Sci-Fi,Thriller 2 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_val_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_val_part1.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_val_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/X_val_part2.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/y_val_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/y_val_part1.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/y_val_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W1/y_val_part2.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W3/lunar_lander_model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W3/lunar_lander_model.h5 -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/content_user_to_genre.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quocviethere/Coursera-Machine-Learning-Specialization/main/Unsupervised Learning, Recommenders, Reinforcement Learning/W2/content_user_to_genre.pickle -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W1/C1_W1_Lab02_Course_Preview_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Ungraded Lab - Examples of Material that will be covered in this course\n", 8 | "Work in Progress" 9 | ] 10 | } 11 | ], 12 | "metadata": { 13 | "kernelspec": { 14 | "display_name": "Python 3", 15 | "language": "python", 16 | "name": "python3" 17 | }, 18 | "language_info": { 19 | "codemirror_mode": { 20 | "name": "ipython", 21 | "version": 3 22 | }, 23 | "file_extension": ".py", 24 | "mimetype": "text/x-python", 25 | "name": "python", 26 | "nbconvert_exporter": "python", 27 | "pygments_lexer": "ipython3", 28 | "version": "3.8.10" 29 | } 30 | }, 31 | "nbformat": 4, 32 | "nbformat_minor": 5 33 | } 34 | -------------------------------------------------------------------------------- /data/ex1data2.txt: -------------------------------------------------------------------------------- 1 | 2104,3,399900 2 | 1600,3,329900 3 | 2400,3,369000 4 | 1416,2,232000 5 | 3000,4,539900 6 | 1985,4,299900 7 | 1534,3,314900 8 | 1427,3,198999 9 | 1380,3,212000 10 | 1494,3,242500 11 | 1940,4,239999 12 | 2000,3,347000 13 | 1890,3,329999 14 | 4478,5,699900 15 | 1268,3,259900 16 | 2300,4,449900 17 | 1320,2,299900 18 | 1236,3,199900 19 | 2609,4,499998 20 | 3031,4,599000 21 | 1767,3,252900 22 | 1888,2,255000 23 | 1604,3,242900 24 | 1962,4,259900 25 | 3890,3,573900 26 | 1100,3,249900 27 | 1458,3,464500 28 | 2526,3,469000 29 | 2200,3,475000 30 | 2637,3,299900 31 | 1839,2,349900 32 | 1000,1,169900 33 | 2040,4,314900 34 | 3137,3,579900 35 | 1811,4,285900 36 | 1437,3,249900 37 | 1239,3,229900 38 | 2132,4,345000 39 | 4215,4,549000 40 | 2162,4,287000 41 | 1664,2,368500 42 | 2238,3,329900 43 | 2567,4,314000 44 | 1200,3,299000 45 | 852,2,179900 46 | 1852,4,299900 47 | 1203,3,239500 48 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | def load_data(): 5 | X = np.load("data/ex7_X.npy") 6 | return X 7 | 8 | def draw_line(p1, p2, style="-k", linewidth=1): 9 | plt.plot([p1[0], p2[0]], [p1[1], p2[1]], style, linewidth=linewidth) 10 | 11 | def plot_data_points(X, idx): 12 | # plots data points in X, coloring them so that those with the same 13 | # index assignments in idx have the same color 14 | plt.scatter(X[:, 0], X[:, 1], c=idx) 15 | 16 | def plot_progress_kMeans(X, centroids, previous_centroids, idx, K, i): 17 | # Plot the examples 18 | plot_data_points(X, idx) 19 | 20 | # Plot the centroids as black 'x's 21 | plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', c='k', linewidths=3) 22 | 23 | # Plot history of the centroids with lines 24 | for j in range(centroids.shape[0]): 25 | draw_line(centroids[j, :], previous_centroids[j, :]) 26 | 27 | plt.title("Iteration number %d" %i) -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/recsys_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | from numpy import loadtxt 4 | 5 | def normalizeRatings(Y, R): 6 | """ 7 | Preprocess data by subtracting mean rating for every movie (every row). 8 | Only include real ratings R(i,j)=1. 9 | [Ynorm, Ymean] = normalizeRatings(Y, R) normalized Y so that each movie 10 | has a rating of 0 on average. Unrated moves then have a mean rating (0) 11 | Returns the mean rating in Ymean. 12 | """ 13 | Ymean = (np.sum(Y*R,axis=1)/(np.sum(R, axis=1)+1e-12)).reshape(-1,1) 14 | Ynorm = Y - np.multiply(Ymean, R) 15 | return(Ynorm, Ymean) 16 | 17 | def load_precalc_params_small(): 18 | 19 | file = open('./data/small_movies_X.csv', 'rb') 20 | X = loadtxt(file, delimiter = ",") 21 | 22 | file = open('./data/small_movies_W.csv', 'rb') 23 | W = loadtxt(file,delimiter = ",") 24 | 25 | file = open('./data/small_movies_b.csv', 'rb') 26 | b = loadtxt(file,delimiter = ",") 27 | b = b.reshape(1,-1) 28 | num_movies, num_features = X.shape 29 | num_users,_ = W.shape 30 | return(X, W, b, num_movies, num_features, num_users) 31 | 32 | def load_ratings_small(): 33 | file = open('./data/small_movies_Y.csv', 'rb') 34 | Y = loadtxt(file,delimiter = ",") 35 | 36 | file = open('./data/small_movies_R.csv', 'rb') 37 | R = loadtxt(file,delimiter = ",") 38 | return(Y,R) 39 | 40 | def load_Movie_List_pd(): 41 | """ returns df with and index of movies in the order they are in in the Y matrix """ 42 | df = pd.read_csv('./data/small_movie_list.csv', header=0, index_col=0, delimiter=',', quotechar='"') 43 | mlist = df["title"].to_list() 44 | return(mlist, df) 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /data/ex1data1.txt: -------------------------------------------------------------------------------- 1 | 6.1101,17.592 2 | 5.5277,9.1302 3 | 8.5186,13.662 4 | 7.0032,11.854 5 | 5.8598,6.8233 6 | 8.3829,11.886 7 | 7.4764,4.3483 8 | 8.5781,12 9 | 6.4862,6.5987 10 | 5.0546,3.8166 11 | 5.7107,3.2522 12 | 14.164,15.505 13 | 5.734,3.1551 14 | 8.4084,7.2258 15 | 5.6407,0.71618 16 | 5.3794,3.5129 17 | 6.3654,5.3048 18 | 5.1301,0.56077 19 | 6.4296,3.6518 20 | 7.0708,5.3893 21 | 6.1891,3.1386 22 | 20.27,21.767 23 | 5.4901,4.263 24 | 6.3261,5.1875 25 | 5.5649,3.0825 26 | 18.945,22.638 27 | 12.828,13.501 28 | 10.957,7.0467 29 | 13.176,14.692 30 | 22.203,24.147 31 | 5.2524,-1.22 32 | 6.5894,5.9966 33 | 9.2482,12.134 34 | 5.8918,1.8495 35 | 8.2111,6.5426 36 | 7.9334,4.5623 37 | 8.0959,4.1164 38 | 5.6063,3.3928 39 | 12.836,10.117 40 | 6.3534,5.4974 41 | 5.4069,0.55657 42 | 6.8825,3.9115 43 | 11.708,5.3854 44 | 5.7737,2.4406 45 | 7.8247,6.7318 46 | 7.0931,1.0463 47 | 5.0702,5.1337 48 | 5.8014,1.844 49 | 11.7,8.0043 50 | 5.5416,1.0179 51 | 7.5402,6.7504 52 | 5.3077,1.8396 53 | 7.4239,4.2885 54 | 7.6031,4.9981 55 | 6.3328,1.4233 56 | 6.3589,-1.4211 57 | 6.2742,2.4756 58 | 5.6397,4.6042 59 | 9.3102,3.9624 60 | 9.4536,5.4141 61 | 8.8254,5.1694 62 | 5.1793,-0.74279 63 | 21.279,17.929 64 | 14.908,12.054 65 | 18.959,17.054 66 | 7.2182,4.8852 67 | 8.2951,5.7442 68 | 10.236,7.7754 69 | 5.4994,1.0173 70 | 20.341,20.992 71 | 10.136,6.6799 72 | 7.3345,4.0259 73 | 6.0062,1.2784 74 | 7.2259,3.3411 75 | 5.0269,-2.6807 76 | 6.5479,0.29678 77 | 7.5386,3.8845 78 | 5.0365,5.7014 79 | 10.274,6.7526 80 | 5.1077,2.0576 81 | 5.7292,0.47953 82 | 5.1884,0.20421 83 | 6.3557,0.67861 84 | 9.7687,7.5435 85 | 6.5159,5.3436 86 | 8.5172,4.2415 87 | 9.1802,6.7981 88 | 6.002,0.92695 89 | 5.5204,0.152 90 | 5.0594,2.8214 91 | 5.7077,1.8451 92 | 7.6366,4.2959 93 | 5.8707,7.2029 94 | 5.3054,1.9869 95 | 8.2934,0.14454 96 | 13.394,9.0551 97 | 5.4369,0.61705 98 | -------------------------------------------------------------------------------- /src/lab_utils_multiclass.py: -------------------------------------------------------------------------------- 1 | # C2_W1 Utilities 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from sklearn.datasets import make_blobs 5 | 6 | def sigmoid(x): 7 | return 1 / (1 + np.exp(-x)) 8 | 9 | # Plot multi-class training points 10 | def plot_mc_data(X, y, class_labels=None, legend=False,size=40): 11 | classes = np.unique(y) 12 | for i in classes: 13 | label = class_labels[i] if class_labels else "class {}".format(i) 14 | idx = np.where(y == i) 15 | plt.scatter(X[idx, 0], X[idx, 1], cmap=plt.cm.Paired, 16 | edgecolor='black', s=size, label=label) 17 | if legend: plt.legend() 18 | 19 | 20 | #Plot a multi-class categorical decision boundary 21 | # This version handles a non-vector prediction (adds a for-loop over points) 22 | def plot_cat_decision_boundary(X,predict , class_labels=None, legend=False, vector=True): 23 | 24 | # create a mesh to points to plot 25 | x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 26 | y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 27 | h = max(x_max-x_min, y_max-y_min)/200 28 | xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 29 | np.arange(y_min, y_max, h)) 30 | points = np.c_[xx.ravel(), yy.ravel()] 31 | print("points", points.shape) 32 | print("xx.shape", xx.shape) 33 | 34 | #make predictions for each point in mesh 35 | if vector: 36 | Z = predict(points) 37 | else: 38 | Z = np.zeros((len(points),)) 39 | for i in range(len(points)): 40 | Z[i] = predict(points[i].reshape(1,2)) 41 | Z = Z.reshape(xx.shape) 42 | 43 | #contour plot highlights boundaries between values - classes in this case 44 | plt.figure() 45 | plt.contour(xx, yy, Z, colors='g') 46 | plt.axis('tight') -------------------------------------------------------------------------------- /src/lab_utils_softmax.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | plt.style.use('./deeplearning.mplstyle') 4 | import tensorflow as tf 5 | from IPython.display import display, Markdown, Latex 6 | from matplotlib.widgets import Slider 7 | from lab_utils_common import dlc 8 | 9 | 10 | def plt_softmax(my_softmax): 11 | fig, ax = plt.subplots(1,2,figsize=(8,4)) 12 | plt.subplots_adjust(bottom=0.35) 13 | 14 | axz0 = fig.add_axes([0.15, 0.10, 0.30, 0.03]) # [left, bottom, width, height] 15 | axz1 = fig.add_axes([0.15, 0.15, 0.30, 0.03]) 16 | axz2 = fig.add_axes([0.15, 0.20, 0.30, 0.03]) 17 | axz3 = fig.add_axes([0.15, 0.25, 0.30, 0.03]) 18 | 19 | z3 = Slider(axz3, 'z3', 0.1, 10.0, valinit=4, valstep=0.1) 20 | z2 = Slider(axz2, 'z2', 0.1, 10.0, valinit=3, valstep=0.1) 21 | z1 = Slider(axz1, 'z1', 0.1, 10.0, valinit=2, valstep=0.1) 22 | z0 = Slider(axz0, 'z0', 0.1, 10.0, valinit=1, valstep=0.1) 23 | 24 | z = np.array(['z0','z1','z2','z3']) 25 | bar = ax[0].barh(z, height=0.6, width=[z0.val,z1.val,z2.val,z3.val], left=None, align='center') 26 | bars = bar.get_children() 27 | ax[0].set_xlim([0,10]) 28 | ax[0].set_title("z input to softmax") 29 | 30 | a = my_softmax(np.array([z0.val,z1.val,z2.val,z3.val])) 31 | anames = np.array(['a0','a1','a2','a3']) 32 | sbar = ax[1].barh(anames, height=0.6, width=a, left=None, align='center',color=dlc["dldarkred"]) 33 | sbars = sbar.get_children() 34 | ax[1].set_xlim([0,1]) 35 | ax[1].set_title("softmax(z)") 36 | 37 | def update(val): 38 | bars[0].set_width(z0.val) 39 | bars[1].set_width(z1.val) 40 | bars[2].set_width(z2.val) 41 | bars[3].set_width(z3.val) 42 | a = my_softmax(np.array([z0.val,z1.val,z2.val,z3.val])) 43 | sbars[0].set_width(a[0]) 44 | sbars[1].set_width(a[1]) 45 | sbars[2].set_width(a[2]) 46 | sbars[3].set_width(a[3]) 47 | 48 | fig.canvas.draw_idle() 49 | 50 | z0.on_changed(update) 51 | z1.on_changed(update) 52 | z2.on_changed(update) 53 | z3.on_changed(update) 54 | 55 | plt.show() 56 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W1/public_tests.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def compute_centroids_test(target): 4 | # With 3 centroids 5 | X = np.array([[-1, -1], [-1.5, -1.5], [-1.5, 1], 6 | [-1, 1.5], [2.5, 1.5], [-1.1, -1.7], [-1.6, 1.2]]) 7 | idx = np.array([1, 1, 1, 0, 0, 0, 2]) 8 | K = 3 9 | centroids = target(X, idx, K) 10 | expected_centroids = np.array([[0.13333333, 0.43333333], 11 | [-1.33333333, -0.5 ], 12 | [-1.6, 1.2 ]]) 13 | 14 | assert type(centroids) == np.ndarray, "Wrong type" 15 | assert centroids.shape == (K, X.shape[1]), f"Wrong shape. Expected: {(len(X),)} got: {idx.shape}" 16 | assert np.allclose(centroids, expected_centroids), f"Wrong values. Expected: {expected_centroids}, got: {centroids}" 17 | 18 | X = np.array([[2, 2.5], [2.5, 2.5], [-1.5, -1.5], 19 | [2, 2], [-1.5, -1], [-1, -1]]) 20 | idx = np.array([0, 0, 1, 0, 1, 1]) 21 | K = 2 22 | centroids = target(X, idx, K) 23 | expected_centroids = np.array([[[ 2.16666667, 2.33333333], 24 | [-1.33333333, -1.16666667]]]) 25 | 26 | assert type(centroids) == np.ndarray, "Wrong type" 27 | assert centroids.shape == (K, X.shape[1]), f"Wrong shape. Expected: {(len(X),)} got: {idx.shape}" 28 | assert np.allclose(centroids, expected_centroids), f"Wrong values. Expected: {expected_centroids}, got: {centroids}" 29 | 30 | print("\033[92mAll tests passed!") 31 | 32 | def find_closest_centroids_test(target): 33 | # With 2 centroids 34 | X = np.array([[-1, -1], [-1.5, -1.5], [-1.5, -1], 35 | [2, 2],[2.5, 2.5],[2, 2.5]]) 36 | initial_centroids = np.array([[-1, -1], [2, 2]]) 37 | idx = target(X, initial_centroids) 38 | 39 | assert type(idx) == np.ndarray, "Wrong type" 40 | assert idx.shape == (len(X),), f"Wrong shape. Expected: {(len(X),)} got: {idx.shape}" 41 | assert np.allclose(idx, [0, 0, 0, 1, 1, 1]), "Wrong values" 42 | 43 | # With 3 centroids 44 | X = np.array([[-1, -1], [-1.5, -1.5], [-1.5, 1], 45 | [-1, 1.5], [2.5, 1.5], [2, 2]]) 46 | initial_centroids = np.array([[2.5, 2], [-1, -1], [-1.5, 1.]]) 47 | idx = target(X, initial_centroids) 48 | 49 | assert type(idx) == np.ndarray, "Wrong type" 50 | assert idx.shape == (len(X),), f"Wrong shape. Expected: {(len(X),)} got: {idx.shape}" 51 | assert np.allclose(idx, [1, 1, 2, 2, 0, 0]), f"Wrong values. Expected {[2, 2, 0, 0, 1, 1]}, got: {idx}" 52 | 53 | # With 3 centroids 54 | X = np.array([[-1, -1], [-1.5, -1.5], [-1.5, 1], 55 | [-1, 1.5], [2.5, 1.5], [-1.1, -1.7], [-1.6, 1.2]]) 56 | initial_centroids = np.array([[2.5, 2], [-1, -1], [-1.5, 1.]]) 57 | idx = target(X, initial_centroids) 58 | 59 | assert type(idx) == np.ndarray, "Wrong type" 60 | assert idx.shape == (len(X),), f"Wrong shape. Expected: {(len(X),)} got: {idx.shape}" 61 | assert np.allclose(idx, [1, 1, 2, 2, 0, 1, 2]), f"Wrong values. Expected {[2, 2, 0, 0, 1, 1]}, got: {idx}" 62 | 63 | print("\033[92mAll tests passed!") -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/public_tests.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def test_cofi_cost_func(target): 4 | num_users_r = 4 5 | num_movies_r = 5 6 | num_features_r = 3 7 | 8 | X_r = np.ones((num_movies_r, num_features_r)) 9 | W_r = np.ones((num_users_r, num_features_r)) 10 | b_r = np.zeros((1, num_users_r)) 11 | Y_r = np.zeros((num_movies_r, num_users_r)) 12 | R_r = np.zeros((num_movies_r, num_users_r)) 13 | 14 | J = target(X_r, W_r, b_r, Y_r, R_r, 2); 15 | assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiplied the regulartization term by lambda_?" 16 | assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term" 17 | 18 | 19 | X_r = np.ones((num_movies_r, num_features_r)) 20 | W_r = np.ones((num_users_r, num_features_r)) 21 | b_r = np.ones((1, num_users_r)) 22 | Y_r = np.ones((num_movies_r, num_users_r)) 23 | R_r = np.ones((num_movies_r, num_users_r)) 24 | 25 | # Evaluate cost function 26 | J = target(X_r, W_r, b_r, Y_r, R_r, 0); 27 | 28 | assert np.isclose(J, 90), f"Wrong value. Expected {90}, got {J}. Check the term without the regularization" 29 | 30 | 31 | X_r = np.ones((num_movies_r, num_features_r)) 32 | W_r = np.ones((num_users_r, num_features_r)) 33 | b_r = np.ones((1, num_users_r)) 34 | Y_r = np.zeros((num_movies_r, num_users_r)) 35 | R_r = np.ones((num_movies_r, num_users_r)) 36 | 37 | # Evaluate cost function 38 | J = target(X_r, W_r, b_r, Y_r, R_r, 0); 39 | 40 | assert np.isclose(J, 160), f"Wrong value. Expected {160}, got {J}. Check the term without the regularization" 41 | 42 | X_r = np.ones((num_movies_r, num_features_r)) 43 | W_r = np.ones((num_users_r, num_features_r)) 44 | b_r = np.ones((1, num_users_r)) 45 | Y_r = np.ones((num_movies_r, num_users_r)) 46 | R_r = np.ones((num_movies_r, num_users_r)) 47 | 48 | # Evaluate cost function 49 | J = target(X_r, W_r, b_r, Y_r, R_r, 1); 50 | 51 | assert np.isclose(J, 103.5), f"Wrong value. Expected {103.5}, got {J}. Check the term without the regularization" 52 | 53 | num_users_r = 3 54 | num_movies_r = 4 55 | num_features_r = 4 56 | 57 | #np.random.seed(247) 58 | X_r = np.array([[0.36618032, 0.9075415, 0.8310605, 0.08590986], 59 | [0.62634721, 0.38234325, 0.85624346, 0.55183039], 60 | [0.77458727, 0.35704147, 0.31003294, 0.20100006], 61 | [0.34420469, 0.46103436, 0.88638208, 0.36175401]])#np.random.rand(num_movies_r, num_features_r) 62 | W_r = np.array([[0.04786854, 0.61504665, 0.06633146, 0.38298908], 63 | [0.16515965, 0.22320207, 0.89826005, 0.14373251], 64 | [0.1274051 , 0.22757303, 0.96865613, 0.70741111]])#np.random.rand(num_users_r, num_features_r) 65 | b_r = np.array([[0.14246472, 0.30110933, 0.56141144]])#np.random.rand(1, num_users_r) 66 | Y_r = np.array([[0.20651685, 0.60767914, 0.86344527], 67 | [0.82665019, 0.00944765, 0.4376798 ], 68 | [0.81623732, 0.26776794, 0.03757507], 69 | [0.37232161, 0.19890823, 0.13026598]])#np.random.rand(num_movies_r, num_users_r) 70 | R_r = np.array([[1, 0, 1], [1, 0, 0], [1, 0, 0], [0, 1, 0]])#(np.random.rand(num_movies_r, num_users_r) > 0.4) * 1 71 | 72 | # Evaluate cost function 73 | J = target(X_r, W_r, b_r, Y_r, R_r, 3); 74 | 75 | assert np.isclose(J, 13.621929978531858, atol=1e-8), f"Wrong value. Expected {13.621929978531858}, got {J}." 76 | 77 | print('\033[92mAll tests passed!') 78 | 79 | -------------------------------------------------------------------------------- /src/public_tests.py: -------------------------------------------------------------------------------- 1 | from tensorflow.keras.activations import relu, linear 2 | from tensorflow.keras.layers import Dense 3 | from tensorflow.keras.optimizers import Adam 4 | 5 | import numpy as np 6 | 7 | def test_network(target): 8 | num_actions = 4 9 | state_size = 8 10 | i = 0 11 | assert len(target.layers) == 3, f"Wrong number of layers. Expected 3 but got {len(target.layers)}" 12 | assert target.input.shape.as_list() == [None, state_size], \ 13 | f"Wrong input shape. Expected [None, 400] but got {target.input.shape.as_list()}" 14 | expected = [[Dense, [None, 64], relu], 15 | [Dense, [None, 64], relu], 16 | [Dense, [None, num_actions], linear]] 17 | 18 | for layer in target.layers: 19 | assert type(layer) == expected[i][0], \ 20 | f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}" 21 | assert layer.output.shape.as_list() == expected[i][1], \ 22 | f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}" 23 | assert layer.activation == expected[i][2], \ 24 | f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}" 25 | i = i + 1 26 | 27 | print("\033[92mAll tests passed!") 28 | 29 | def test_optimizer(target, ALPHA): 30 | assert type(target) == Adam, f"Wrong optimizer. Expected: {Adam}, got: {target}" 31 | assert np.isclose(target.learning_rate.numpy(), ALPHA), f"Wrong alpha. Expected: {ALPHA}, got: {target.learning_rate.numpy()}" 32 | print("\033[92mAll tests passed!") 33 | 34 | 35 | def test_compute_loss(target): 36 | num_actions = 4 37 | def target_q_network_random(inputs): 38 | return np.float32(np.random.rand(inputs.shape[0],num_actions)) 39 | 40 | def q_network_random(inputs): 41 | return np.float32(np.random.rand(inputs.shape[0],num_actions)) 42 | 43 | def target_q_network_ones(inputs): 44 | return np.float32(np.ones((inputs.shape[0], num_actions))) 45 | 46 | def q_network_ones(inputs): 47 | return np.float32(np.ones((inputs.shape[0], num_actions))) 48 | 49 | np.random.seed(1) 50 | states = np.float32(np.random.rand(64, 8)) 51 | actions = np.float32(np.floor(np.random.uniform(0, 1, (64, )) * 4)) 52 | rewards = np.float32(np.random.rand(64, )) 53 | next_states = np.float32(np.random.rand(64, 8)) 54 | done_vals = np.float32((np.random.uniform(0, 1, size=(64,)) > 0.96) * 1) 55 | 56 | loss = target((states, actions, rewards, next_states, done_vals), 0.995, q_network_random, target_q_network_random) 57 | 58 | 59 | assert np.isclose(loss, 0.6991737), f"Wrong value. Expected {0.6991737}, got {loss}" 60 | 61 | # Test when episode terminates 62 | done_vals = np.float32(np.ones((64,))) 63 | loss = target((states, actions, rewards, next_states, done_vals), 0.995, q_network_ones, target_q_network_ones) 64 | assert np.isclose(loss, 0.343270182), f"Wrong value. Expected {0.343270182}, got {loss}" 65 | 66 | # Test MSE with parameters A = B 67 | done_vals = np.float32((np.random.uniform(0, 1, size=(64,)) > 0.96) * 1) 68 | rewards = np.float32(np.ones((64, ))) 69 | loss = target((states, actions, rewards, next_states, done_vals), 0, q_network_ones, target_q_network_ones) 70 | assert np.isclose(loss, 0), f"Wrong value. Expected {0}, got {loss}" 71 | 72 | # Test MSE with parameters A = 0 and B = 1 73 | done_vals = np.float32((np.random.uniform(0, 1, size=(64,)) > 0.96) * 1) 74 | rewards = np.float32(np.zeros((64, ))) 75 | loss = target((states, actions, rewards, next_states, done_vals), 0, q_network_ones, target_q_network_ones) 76 | assert np.isclose(loss, 1), f"Wrong value. Expected {1}, got {loss}" 77 | 78 | print("\033[92mAll tests passed!") 79 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W3/public_tests.py: -------------------------------------------------------------------------------- 1 | from tensorflow.keras.activations import relu, linear 2 | from tensorflow.keras.layers import Dense 3 | from tensorflow.keras.optimizers import Adam 4 | 5 | import numpy as np 6 | 7 | def test_network(target): 8 | num_actions = 4 9 | state_size = 8 10 | i = 0 11 | assert len(target.layers) == 3, f"Wrong number of layers. Expected 3 but got {len(target.layers)}" 12 | assert target.input.shape.as_list() == [None, state_size], \ 13 | f"Wrong input shape. Expected [None, 400] but got {target.input.shape.as_list()}" 14 | expected = [[Dense, [None, 64], relu], 15 | [Dense, [None, 64], relu], 16 | [Dense, [None, num_actions], linear]] 17 | 18 | for layer in target.layers: 19 | assert type(layer) == expected[i][0], \ 20 | f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}" 21 | assert layer.output.shape.as_list() == expected[i][1], \ 22 | f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}" 23 | assert layer.activation == expected[i][2], \ 24 | f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}" 25 | i = i + 1 26 | 27 | print("\033[92mAll tests passed!") 28 | 29 | def test_optimizer(target, ALPHA): 30 | assert type(target) == Adam, f"Wrong optimizer. Expected: {Adam}, got: {target}" 31 | assert np.isclose(target.learning_rate.numpy(), ALPHA), f"Wrong alpha. Expected: {ALPHA}, got: {target.learning_rate.numpy()}" 32 | print("\033[92mAll tests passed!") 33 | 34 | 35 | def test_compute_loss(target): 36 | num_actions = 4 37 | def target_q_network_random(inputs): 38 | return np.float32(np.random.rand(inputs.shape[0],num_actions)) 39 | 40 | def q_network_random(inputs): 41 | return np.float32(np.random.rand(inputs.shape[0],num_actions)) 42 | 43 | def target_q_network_ones(inputs): 44 | return np.float32(np.ones((inputs.shape[0], num_actions))) 45 | 46 | def q_network_ones(inputs): 47 | return np.float32(np.ones((inputs.shape[0], num_actions))) 48 | 49 | np.random.seed(1) 50 | states = np.float32(np.random.rand(64, 8)) 51 | actions = np.float32(np.floor(np.random.uniform(0, 1, (64, )) * 4)) 52 | rewards = np.float32(np.random.rand(64, )) 53 | next_states = np.float32(np.random.rand(64, 8)) 54 | done_vals = np.float32((np.random.uniform(0, 1, size=(64,)) > 0.96) * 1) 55 | 56 | loss = target((states, actions, rewards, next_states, done_vals), 0.995, q_network_random, target_q_network_random) 57 | 58 | 59 | assert np.isclose(loss, 0.6991737), f"Wrong value. Expected {0.6991737}, got {loss}" 60 | 61 | # Test when episode terminates 62 | done_vals = np.float32(np.ones((64,))) 63 | loss = target((states, actions, rewards, next_states, done_vals), 0.995, q_network_ones, target_q_network_ones) 64 | assert np.isclose(loss, 0.343270182), f"Wrong value. Expected {0.343270182}, got {loss}" 65 | 66 | # Test MSE with parameters A = B 67 | done_vals = np.float32((np.random.uniform(0, 1, size=(64,)) > 0.96) * 1) 68 | rewards = np.float32(np.ones((64, ))) 69 | loss = target((states, actions, rewards, next_states, done_vals), 0, q_network_ones, target_q_network_ones) 70 | assert np.isclose(loss, 0), f"Wrong value. Expected {0}, got {loss}" 71 | 72 | # Test MSE with parameters A = 0 and B = 1 73 | done_vals = np.float32((np.random.uniform(0, 1, size=(64,)) > 0.96) * 1) 74 | rewards = np.float32(np.zeros((64, ))) 75 | loss = target((states, actions, rewards, next_states, done_vals), 0, q_network_ones, target_q_network_ones) 76 | assert np.isclose(loss, 1), f"Wrong value. Expected {1}, got {loss}" 77 | 78 | print("\033[92mAll tests passed!") 79 | -------------------------------------------------------------------------------- /src/lab_neurons_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | plt.style.use('./deeplearning.mplstyle') 4 | from matplotlib import cm 5 | import matplotlib.colors as colors 6 | from lab_utils_common import dlc 7 | 8 | def plt_prob_1d(ax,fwb): 9 | """ plots a decision boundary but include shading to indicate the probability """ 10 | #setup useful ranges and common linspaces 11 | x_space = np.linspace(0, 5 , 50) 12 | y_space = np.linspace(0, 1 , 50) 13 | 14 | # get probability for x range, extend to y 15 | z = np.zeros((len(x_space),len(y_space))) 16 | for i in range(len(x_space)): 17 | x = np.array([[x_space[i]]]) 18 | z[:,i] = fwb(x) 19 | 20 | cmap = plt.get_cmap('Blues') 21 | new_cmap = truncate_colormap(cmap, 0.0, 0.5) 22 | pcm = ax.pcolormesh(x_space, y_space, z, 23 | norm=cm.colors.Normalize(vmin=0, vmax=1), 24 | cmap=new_cmap, shading='nearest', alpha = 0.9) 25 | ax.figure.colorbar(pcm, ax=ax) 26 | 27 | def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100): 28 | """ truncates color map """ 29 | new_cmap = colors.LinearSegmentedColormap.from_list( 30 | 'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval), 31 | cmap(np.linspace(minval, maxval, n))) 32 | return new_cmap 33 | 34 | 35 | def sigmoidnp(z): 36 | """ 37 | Compute the sigmoid of z 38 | 39 | Parameters 40 | ---------- 41 | z : array_like 42 | A scalar or numpy array of any size. 43 | 44 | Returns 45 | ------- 46 | g : array_like 47 | sigmoid(z) 48 | """ 49 | z = np.clip( z, -500, 500 ) # protect against overflow 50 | g = 1.0/(1.0+np.exp(-z)) 51 | 52 | return g 53 | 54 | def plt_linear(X_train, Y_train, prediction_tf, prediction_np): 55 | fig, ax = plt.subplots(1,2, figsize=(16,4)) 56 | ax[0].scatter(X_train, Y_train, marker='x', c='r', label="Data Points") 57 | ax[0].plot(X_train, prediction_tf, c=dlc['dlblue'], label="model output") 58 | ax[0].text(1.6,350,r"y=$200 x + 100$", fontsize='xx-large', color=dlc['dlmagenta']) 59 | ax[0].legend(fontsize='xx-large') 60 | ax[0].set_ylabel('Price (in 1000s of dollars)', fontsize='xx-large') 61 | ax[0].set_xlabel('Size (1000 sqft)', fontsize='xx-large') 62 | ax[0].set_title("Tensorflow prediction",fontsize='xx-large') 63 | 64 | ax[1].scatter(X_train, Y_train, marker='x', c='r', label="Data Points") 65 | ax[1].plot(X_train, prediction_np, c=dlc['dlblue'], label="model output") 66 | ax[1].text(1.6,350,r"y=$200 x + 100$", fontsize='xx-large', color=dlc['dlmagenta']) 67 | ax[1].legend(fontsize='xx-large') 68 | ax[1].set_ylabel('Price (in 1000s of dollars)', fontsize='xx-large') 69 | ax[1].set_xlabel('Size (1000 sqft)', fontsize='xx-large') 70 | ax[1].set_title("Numpy prediction",fontsize='xx-large') 71 | plt.show() 72 | 73 | 74 | def plt_logistic(X_train, Y_train, model, set_w, set_b, pos, neg): 75 | fig,ax = plt.subplots(1,2,figsize=(16,4)) 76 | 77 | layerf= lambda x : model.predict(x) 78 | plt_prob_1d(ax[0], layerf) 79 | 80 | ax[0].scatter(X_train[pos], Y_train[pos], marker='x', s=80, c = 'red', label="y=1") 81 | ax[0].scatter(X_train[neg], Y_train[neg], marker='o', s=100, label="y=0", facecolors='none', 82 | edgecolors=dlc["dlblue"],lw=3) 83 | 84 | ax[0].set_ylim(-0.08,1.1) 85 | ax[0].set_xlim(-0.5,5.5) 86 | ax[0].set_ylabel('y', fontsize=16) 87 | ax[0].set_xlabel('x', fontsize=16) 88 | ax[0].set_title('Tensorflow Model', fontsize=20) 89 | ax[0].legend(fontsize=16) 90 | 91 | layerf= lambda x : sigmoidnp(np.dot(set_w,x.reshape(1,1)) + set_b) 92 | plt_prob_1d(ax[1], layerf) 93 | 94 | ax[1].scatter(X_train[pos], Y_train[pos], marker='x', s=80, c = 'red', label="y=1") 95 | ax[1].scatter(X_train[neg], Y_train[neg], marker='o', s=100, label="y=0", facecolors='none', 96 | edgecolors=dlc["dlblue"],lw=3) 97 | 98 | ax[1].set_ylim(-0.08,1.1) 99 | ax[1].set_xlim(-0.5,5.5) 100 | ax[1].set_ylabel('y', fontsize=16) 101 | ax[1].set_xlabel('x', fontsize=16) 102 | ax[1].set_title('Numpy Model', fontsize=20) 103 | ax[1].legend(fontsize=16) 104 | plt.show() 105 | -------------------------------------------------------------------------------- /src/lab_utils_relu.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from matplotlib.gridspec import GridSpec 4 | plt.style.use('./deeplearning.mplstyle') 5 | from matplotlib.widgets import Slider 6 | from lab_utils_common import dlc 7 | 8 | def widgvis(fig): 9 | fig.canvas.toolbar_visible = False 10 | fig.canvas.header_visible = False 11 | fig.canvas.footer_visible = False 12 | 13 | 14 | def plt_base(ax): 15 | X = np.linspace(0, 3, 3*100) 16 | y = np.r_[ -2*X[0:100]+2, 1*X[100:200]-3+2, 3*X[200:300]-7+2 ] 17 | w00 = -2 18 | b00 = 2 19 | w01 = 0 # 1 20 | b01 = 0 # -1 21 | w02 = 0 # 2 22 | b02 = 0 # -4 23 | ax[0].plot(X, y, color = dlc["dlblue"], label="target") 24 | arts = [] 25 | arts.extend( plt_yhat(ax[0], X, w00, b00, w01, b01, w02, b02) ) 26 | _ = plt_unit(ax[1], X, w00, b00) #Fixed 27 | arts.extend( plt_unit(ax[2], X, w01, b01) ) 28 | arts.extend( plt_unit(ax[3], X, w02, b02) ) 29 | return(X, arts) 30 | 31 | def plt_yhat(ax, X, w00, b00, w01, b01, w02, b02): 32 | yhat = np.maximum(0, np.dot(w00, X) + b00) + \ 33 | np.maximum(0, np.dot(w01, X) + b01) + \ 34 | np.maximum(0, np.dot(w02, X) + b02) 35 | lp = ax.plot(X, yhat, lw=2, color = dlc["dlorange"], label="a2") 36 | return(lp) 37 | 38 | def plt_unit(ax, X, w, b): 39 | z = np.dot(w,X) + b 40 | yhat = np.maximum(0,z) 41 | lpa = ax.plot(X, z, dlc["dlblue"], label="z") 42 | lpb = ax.plot(X, yhat, dlc["dlmagenta"], lw=1, label="a") 43 | return([lpa[0], lpb[0]]) 44 | 45 | # if output is need for debug, put this in a cell and call ahead of time. Output will be below that cell. 46 | #from ipywidgets import Output #this line stays here 47 | #output = Output() #this line stays here 48 | #display(output) #this line goes in notebook 49 | 50 | def plt_relu_ex(): 51 | artists = [] 52 | 53 | fig = plt.figure() 54 | fig.suptitle("Explore Non-Linear Activation") 55 | 56 | gs = GridSpec(3, 2, width_ratios=[2, 1], height_ratios=[1, 1, 1]) 57 | ax1 = fig.add_subplot(gs[0:2,0]) 58 | ax2 = fig.add_subplot(gs[0,1]) 59 | ax3 = fig.add_subplot(gs[1,1]) 60 | ax4 = fig.add_subplot(gs[2,1]) 61 | ax = [ax1,ax2,ax3,ax4] 62 | 63 | widgvis(fig) 64 | #plt.subplots_adjust(bottom=0.35) 65 | 66 | axb2 = fig.add_axes([0.15, 0.10, 0.30, 0.03]) # [left, bottom, width, height] 67 | axw2 = fig.add_axes([0.15, 0.15, 0.30, 0.03]) 68 | axb1 = fig.add_axes([0.15, 0.20, 0.30, 0.03]) 69 | axw1 = fig.add_axes([0.15, 0.25, 0.30, 0.03]) 70 | 71 | sw1 = Slider(axw1, 'w1', -4.0, 4.0, valinit=0, valstep=0.1) 72 | sb1 = Slider(axb1, 'b1', -4.0, 4.0, valinit=0, valstep=0.1) 73 | sw2 = Slider(axw2, 'w2', -4.0, 4.0, valinit=0, valstep=0.1) 74 | sb2 = Slider(axb2, 'b2', -4.0, 4.0, valinit=0, valstep=0.1) 75 | 76 | X,lp = plt_base(ax) 77 | artists.extend( lp ) 78 | 79 | #@output.capture() 80 | def update(val): 81 | #print("-----------") 82 | #print(f"len artists {len(artists)}", artists) 83 | for i in range(len(artists)): 84 | artist = artists[i] 85 | #print("artist:", artist) 86 | artist.remove() 87 | artists.clear() 88 | #print(artists) 89 | w00 = -2 90 | b00 = 2 91 | w01 = sw1.val # 1 92 | b01 = sb1.val # -1 93 | w02 = sw2.val # 2 94 | b02 = sb2.val # -4 95 | artists.extend(plt_yhat(ax[0], X, w00, b00, w01, b01, w02, b02)) 96 | artists.extend(plt_unit(ax[2], X, w01, b01) ) 97 | artists.extend(plt_unit(ax[3], X, w02, b02) ) 98 | #fig.canvas.draw_idle() 99 | 100 | sw1.on_changed(update) 101 | sb1.on_changed(update) 102 | sw2.on_changed(update) 103 | sb2.on_changed(update) 104 | 105 | ax[0].set_title(" Match Target ") 106 | ax[0].legend() 107 | ax[0].set_xlabel("x") 108 | ax[1].set_title("Unit 0 (fixed) ") 109 | ax[1].legend() 110 | ax[2].set_title("Unit 1") 111 | ax[2].legend() 112 | ax[3].set_title("Unit 2") 113 | ax[3].legend() 114 | plt.tight_layout() 115 | 116 | plt.show() 117 | return([sw1,sw2,sb1,sb2,artists]) # returned to keep a live reference to sliders 118 | 119 | -------------------------------------------------------------------------------- /data/small_movies_b.csv: -------------------------------------------------------------------------------- 1 | 0.23609531,0.35653394,0.12819964,0.3155024,0.23636407,-0.22917777,-0.14401537,0.075727165,-0.37003124,-0.021354377,-0.01885897,-0.016493738,0.069341004,-0.4371599,-0.24480343,0.29720962,-0.3823989,0.37802482,0.44692165,-0.024649143,-0.499193,-0.4379481,-0.27089447,-0.23521835,0.35863072,-0.33172983,-0.319295,0.33025116,0.06182003,0.1360473,-0.16637295,-0.07102251,0.05386877,-0.34516948,-0.07712108,-0.43622762,0.34539694,-0.008741677,0.3759488,0.2825104,-0.14146823,-0.4804005,-0.40585357,0.2456305,-0.49492687,0.10903448,0.1771251,0.056036294,0.1348601,-0.25175112,0.11369991,0.48754352,0.24302799,0.20360255,0.066055596,0.13253677,0.09305513,0.2777846,0.023716927,0.10299754,-0.43873072,0.3180564,0.11554575,-0.45647788,-0.1157915,-0.20613712,-0.3222394,0.0670712,0.1316781,-0.04797685,0.16459042,0.265431,-0.44394213,-0.11467117,0.42816055,0.11962229,-0.18784195,0.23876792,0.14463365,-0.054173946,-0.084650755,-0.022121489,-0.016997159,-0.478563,0.19013703,0.14574134,-0.36160725,0.12130946,0.28853047,-0.029776871,-0.06522834,-0.4273659,-0.075282395,-0.15572357,0.011092424,0.3204555,0.24933082,0.11092675,0.099162936,0.066720545,-0.24027056,-0.15710282,-0.17818177,-0.36245525,-0.113695264,0.24532175,0.15722138,0.031137764,-0.27763367,0.16367197,-0.28439033,0.46277177,0.29739505,0.27828717,0.019126952,0.11723554,-0.22481555,-0.30280328,0.34909534,-0.24122453,-0.16898203,-0.14903909,0.26694208,0.42356133,0.34913808,-0.03734243,0.10106343,-0.17388654,-0.49831563,0.07820094,-0.15342546,-0.36590004,0.1406846,-0.47188687,0.17444587,-0.095620334,-0.35815257,0.35699356,-0.2095989,0.027381063,-0.38910145,0.11574459,-0.14782763,-0.39063984,0.4066319,-0.32036752,-0.1109702,0.39456856,0.37689888,0.39321297,0.3459376,0.006568253,-0.11964077,0.49896598,-0.24059612,0.10720742,0.22555989,-0.28566742,-0.3495056,-0.4036088,-0.14572406,0.36242604,-0.27781355,-0.35440212,0.02017504,-0.014872491,-0.45937228,-0.46679926,-0.24539918,0.250834,0.48240495,-0.12271291,-0.18530989,0.4877388,-0.42409772,-0.17774671,-0.23274487,-0.20638466,0.10630572,-0.23506796,0.41716546,-0.48773384,0.34115142,-0.19853532,0.13483697,0.48814183,0.365838,0.167292,0.26261747,0.048885167,-0.053166866,-0.09033108,0.004328966,-0.07519597,-0.0074519515,-0.18805629,0.08991516,-0.074141085,0.011850238,0.0032178164,-0.13788843,-0.15182257,0.23794007,-0.44536138,-0.4325565,0.04964775,-0.39575815,0.13207555,0.46450144,0.48699808,0.0982306,-0.24818414,-0.43989497,-0.17733443,0.44720662,-0.4877941,0.32816106,0.1520955,-0.36473483,0.41389132,0.38561857,0.3391484,0.24316305,-0.34090698,0.18459034,0.22107542,0.46749824,0.24527848,-0.015088022,-0.19061911,0.39714372,0.17129338,-0.4565462,0.29424638,0.112273395,0.029050827,0.24344242,0.07746142,0.09415382,0.32288378,-0.12956685,-0.4886583,-0.28763622,-0.13127446,0.06799567,0.19962704,0.30296177,0.36935085,-0.09190476,0.26250762,0.40036052,-0.2743777,-0.49375767,-0.069624245,-0.29795706,0.051351905,0.025731027,0.3185677,0.1474306,0.29947352,0.07307166,0.19167346,-0.103982925,0.18090385,0.30783385,0.13168758,-0.28889263,-0.2867632,-0.14057803,-0.4823867,0.031248987,-0.3843631,-0.15378278,-0.2224276,0.117133915,-0.17432636,-0.4389227,-0.1263656,-0.19403148,0.07353908,-0.4833243,0.4543454,0.32979268,0.020353973,0.43283933,0.32527155,0.121165454,-0.19067067,-0.31272715,0.2764269,-0.1997847,-0.49005002,-0.13874072,-0.24505162,0.4553625,0.4179442,-0.042735636,-0.08790296,0.041137338,0.41393095,0.15213323,-0.09905428,0.13102758,0.12146121,0.1455949,-0.4549449,0.3422228,0.19875216,0.33838552,-0.31526864,0.15791327,-0.048859477,0.38249975,-0.2259838,0.4335224,0.091159284,0.1873228,-0.4408388,0.13642609,0.2911085,-0.4142604,0.36956507,0.17642796,-0.05092746,0.4400813,0.2210195,0.19256228,0.40831757,0.2089485,-0.2952068,-0.46408284,-0.024006605,0.09706116,0.30689007,-0.20083755,-0.065697074,-0.007901192,0.33029783,-0.04697472,-0.301004,-0.03436923,-0.18052393,0.205028,0.17713094,-0.2915638,0.13628459,0.18889505,-0.10857934,-0.105745375,-0.30238467,0.27625644,0.36994237,0.3764261,0.2511189,-0.30040634,0.17628974,-0.41685903,-0.109700084,-0.18728197,0.28934503,0.49773997,0.08808029,-0.110871136,0.03448701,-0.27818608,0.34542423,-0.37529272,-0.2853775,-0.26411527,0.41159993,-0.1375072,0.4988646,0.19680327,0.4818679,0.15024763,0.12502313,0.25538224,0.3805648,0.27491546,0.15169483,-0.30213797,0.11057246,0.37918144,-0.09437716,0.13442796,-0.019827485,-0.43407947,0.26273787,0.22879243,0.26459223,0.4303277,0.25444216,0.4908933,0.41282868,-0.097350895,-0.39770204,-0.2710728,0.3187216,0.49455476,-0.33418065,-0.22715044,-0.11189991,-0.37946326,0.17442083,0.1970753,0.3409006,0.27558124,-0.20686197,0.3802954,0.0137351155,-0.1563006,0.48745012,-0.032333493,0.11390352,-0.40698427,-0.21574324,0.27934504,0.05980569,0.3466866,0.07286084,0.35323143,-0.2696011,0.29613405,0.09769893,0.24693,0.26872206,-0.41006708,-0.04926592,-0.44144148,-0.41583318,0.15290171,0.068596244,0.10710317,-0.17657506,-0.47332853,-0.36171782,0.08478701,0.42955917,-0.40508574,0.45839047,0.4126075,-0.36223966,-0.3201397 2 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W2/small_movies_b.csv: -------------------------------------------------------------------------------- 1 | 0.23609531,0.35653394,0.12819964,0.3155024,0.23636407,-0.22917777,-0.14401537,0.075727165,-0.37003124,-0.021354377,-0.01885897,-0.016493738,0.069341004,-0.4371599,-0.24480343,0.29720962,-0.3823989,0.37802482,0.44692165,-0.024649143,-0.499193,-0.4379481,-0.27089447,-0.23521835,0.35863072,-0.33172983,-0.319295,0.33025116,0.06182003,0.1360473,-0.16637295,-0.07102251,0.05386877,-0.34516948,-0.07712108,-0.43622762,0.34539694,-0.008741677,0.3759488,0.2825104,-0.14146823,-0.4804005,-0.40585357,0.2456305,-0.49492687,0.10903448,0.1771251,0.056036294,0.1348601,-0.25175112,0.11369991,0.48754352,0.24302799,0.20360255,0.066055596,0.13253677,0.09305513,0.2777846,0.023716927,0.10299754,-0.43873072,0.3180564,0.11554575,-0.45647788,-0.1157915,-0.20613712,-0.3222394,0.0670712,0.1316781,-0.04797685,0.16459042,0.265431,-0.44394213,-0.11467117,0.42816055,0.11962229,-0.18784195,0.23876792,0.14463365,-0.054173946,-0.084650755,-0.022121489,-0.016997159,-0.478563,0.19013703,0.14574134,-0.36160725,0.12130946,0.28853047,-0.029776871,-0.06522834,-0.4273659,-0.075282395,-0.15572357,0.011092424,0.3204555,0.24933082,0.11092675,0.099162936,0.066720545,-0.24027056,-0.15710282,-0.17818177,-0.36245525,-0.113695264,0.24532175,0.15722138,0.031137764,-0.27763367,0.16367197,-0.28439033,0.46277177,0.29739505,0.27828717,0.019126952,0.11723554,-0.22481555,-0.30280328,0.34909534,-0.24122453,-0.16898203,-0.14903909,0.26694208,0.42356133,0.34913808,-0.03734243,0.10106343,-0.17388654,-0.49831563,0.07820094,-0.15342546,-0.36590004,0.1406846,-0.47188687,0.17444587,-0.095620334,-0.35815257,0.35699356,-0.2095989,0.027381063,-0.38910145,0.11574459,-0.14782763,-0.39063984,0.4066319,-0.32036752,-0.1109702,0.39456856,0.37689888,0.39321297,0.3459376,0.006568253,-0.11964077,0.49896598,-0.24059612,0.10720742,0.22555989,-0.28566742,-0.3495056,-0.4036088,-0.14572406,0.36242604,-0.27781355,-0.35440212,0.02017504,-0.014872491,-0.45937228,-0.46679926,-0.24539918,0.250834,0.48240495,-0.12271291,-0.18530989,0.4877388,-0.42409772,-0.17774671,-0.23274487,-0.20638466,0.10630572,-0.23506796,0.41716546,-0.48773384,0.34115142,-0.19853532,0.13483697,0.48814183,0.365838,0.167292,0.26261747,0.048885167,-0.053166866,-0.09033108,0.004328966,-0.07519597,-0.0074519515,-0.18805629,0.08991516,-0.074141085,0.011850238,0.0032178164,-0.13788843,-0.15182257,0.23794007,-0.44536138,-0.4325565,0.04964775,-0.39575815,0.13207555,0.46450144,0.48699808,0.0982306,-0.24818414,-0.43989497,-0.17733443,0.44720662,-0.4877941,0.32816106,0.1520955,-0.36473483,0.41389132,0.38561857,0.3391484,0.24316305,-0.34090698,0.18459034,0.22107542,0.46749824,0.24527848,-0.015088022,-0.19061911,0.39714372,0.17129338,-0.4565462,0.29424638,0.112273395,0.029050827,0.24344242,0.07746142,0.09415382,0.32288378,-0.12956685,-0.4886583,-0.28763622,-0.13127446,0.06799567,0.19962704,0.30296177,0.36935085,-0.09190476,0.26250762,0.40036052,-0.2743777,-0.49375767,-0.069624245,-0.29795706,0.051351905,0.025731027,0.3185677,0.1474306,0.29947352,0.07307166,0.19167346,-0.103982925,0.18090385,0.30783385,0.13168758,-0.28889263,-0.2867632,-0.14057803,-0.4823867,0.031248987,-0.3843631,-0.15378278,-0.2224276,0.117133915,-0.17432636,-0.4389227,-0.1263656,-0.19403148,0.07353908,-0.4833243,0.4543454,0.32979268,0.020353973,0.43283933,0.32527155,0.121165454,-0.19067067,-0.31272715,0.2764269,-0.1997847,-0.49005002,-0.13874072,-0.24505162,0.4553625,0.4179442,-0.042735636,-0.08790296,0.041137338,0.41393095,0.15213323,-0.09905428,0.13102758,0.12146121,0.1455949,-0.4549449,0.3422228,0.19875216,0.33838552,-0.31526864,0.15791327,-0.048859477,0.38249975,-0.2259838,0.4335224,0.091159284,0.1873228,-0.4408388,0.13642609,0.2911085,-0.4142604,0.36956507,0.17642796,-0.05092746,0.4400813,0.2210195,0.19256228,0.40831757,0.2089485,-0.2952068,-0.46408284,-0.024006605,0.09706116,0.30689007,-0.20083755,-0.065697074,-0.007901192,0.33029783,-0.04697472,-0.301004,-0.03436923,-0.18052393,0.205028,0.17713094,-0.2915638,0.13628459,0.18889505,-0.10857934,-0.105745375,-0.30238467,0.27625644,0.36994237,0.3764261,0.2511189,-0.30040634,0.17628974,-0.41685903,-0.109700084,-0.18728197,0.28934503,0.49773997,0.08808029,-0.110871136,0.03448701,-0.27818608,0.34542423,-0.37529272,-0.2853775,-0.26411527,0.41159993,-0.1375072,0.4988646,0.19680327,0.4818679,0.15024763,0.12502313,0.25538224,0.3805648,0.27491546,0.15169483,-0.30213797,0.11057246,0.37918144,-0.09437716,0.13442796,-0.019827485,-0.43407947,0.26273787,0.22879243,0.26459223,0.4303277,0.25444216,0.4908933,0.41282868,-0.097350895,-0.39770204,-0.2710728,0.3187216,0.49455476,-0.33418065,-0.22715044,-0.11189991,-0.37946326,0.17442083,0.1970753,0.3409006,0.27558124,-0.20686197,0.3802954,0.0137351155,-0.1563006,0.48745012,-0.032333493,0.11390352,-0.40698427,-0.21574324,0.27934504,0.05980569,0.3466866,0.07286084,0.35323143,-0.2696011,0.29613405,0.09769893,0.24693,0.26872206,-0.41006708,-0.04926592,-0.44144148,-0.41583318,0.15290171,0.068596244,0.10710317,-0.17657506,-0.47332853,-0.36171782,0.08478701,0.42955917,-0.40508574,0.45839047,0.4126075,-0.36223966,-0.3201397 2 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W1/C1_W1_Lab01_Python_Jupyter_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Optional Lab: Brief Introduction to Python and Jupyter Notebooks\n", 8 | "Welcome to the first optional lab! \n", 9 | "Optional labs are available to:\n", 10 | "- provide information - like this notebook\n", 11 | "- reinforce lecture material with hands-on examples\n", 12 | "- provide working examples of routines used in the graded labs" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "## Goals\n", 20 | "In this lab, you will:\n", 21 | "- Get a brief introduction to Jupyter notebooks\n", 22 | "- Take a tour of Jupyter notebooks\n", 23 | "- Learn the difference between markdown cells and code cells\n", 24 | "- Practice some basic python\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "The easiest way to become familiar with Jupyter notebooks is to take the tour available above in the Help menu:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "
\n", 39 | "
missing
\n", 40 | "
" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "Jupyter notebooks have two types of cells that are used in this course. Cells such as this which contain documentation called `Markdown Cells`. The name is derived from the simple formatting language used in the cells. You will not be required to produce markdown cells. Its useful to understand the `cell pulldown` shown in graphic below. Occasionally, a cell will end up in the wrong mode and you may need to restore it to the right state:" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "
\n", 55 | " missing\n", 56 | "
" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "The other type of cell is the `code cell` where you will write your code:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 1, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "This is code cell\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "#This is a 'Code' Cell\n", 81 | "print(\"This is code cell\")" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Python\n", 89 | "You can write your code in the code cells. \n", 90 | "To run the code, select the cell and either\n", 91 | "- hold the shift-key down and hit 'enter' or 'return'\n", 92 | "- click the 'run' arrow above\n", 93 | "
\n", 94 | " \n", 95 | "
\n", 96 | "\n", 97 | " " 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "### Print statement\n", 105 | "Print statements will generally use the python f-string style. \n", 106 | "Try creating your own print in the following cell. \n", 107 | "Try both methods of running the cell." 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 2, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "f strings allow you to embed variables right in the strings!\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "# print statements\n", 125 | "variable = \"right in the strings!\"\n", 126 | "print(f\"f strings allow you to embed variables {variable}\")" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "# Congratulations!\n", 134 | "You now know how to find your way around a Jupyter Notebook." 135 | ] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 3", 141 | "language": "python", 142 | "name": "python3" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 3 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython3", 154 | "version": "3.7.6" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 5 159 | } 160 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W3/C1_W3_Lab08_Overfitting_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Ungraded Lab: Overfitting \n", 8 | "\n", 9 | "\n", 10 | "\n", 11 | "" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Goals\n", 19 | "In this lab, you will explore:\n", 20 | "- the situations where overfitting can occur\n", 21 | "- some of the solutions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "%matplotlib widget\n", 31 | "import matplotlib.pyplot as plt\n", 32 | "from ipywidgets import Output\n", 33 | "from plt_overfit import overfit_example, output\n", 34 | "plt.style.use('./deeplearning.mplstyle')" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "# Overfitting\n", 42 | "The week's lecture described situations where overfitting can arise. Run the cell below to generate a plot that will allow you to explore overfitting. There are further instructions below the cell." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "application/vnd.jupyter.widget-view+json": { 53 | "model_id": "717ecf5ed2384b6db9b4cf9a983508f3", 54 | "version_major": 2, 55 | "version_minor": 0 56 | }, 57 | "text/plain": [ 58 | "Output()" 59 | ] 60 | }, 61 | "metadata": {}, 62 | "output_type": "display_data" 63 | }, 64 | { 65 | "data": { 66 | "application/vnd.jupyter.widget-view+json": { 67 | "model_id": "a6108a7e5a5f4f489e30f3ea9fb0f7d7", 68 | "version_major": 2, 69 | "version_minor": 0 70 | }, 71 | "text/plain": [ 72 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 73 | ] 74 | }, 75 | "metadata": {}, 76 | "output_type": "display_data" 77 | } 78 | ], 79 | "source": [ 80 | "plt.close(\"all\")\n", 81 | "display(output)\n", 82 | "ofit = overfit_example(False)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "In the plot above you can:\n", 90 | "- switch between Regression and Categorization examples\n", 91 | "- add data\n", 92 | "- select the degree of the model\n", 93 | "- fit the model to the data \n", 94 | "\n", 95 | "Here are some things you should try:\n", 96 | "- Fit the data with degree = 1; Note 'underfitting'.\n", 97 | "- Fit the data with degree = 6; Note 'overfitting'\n", 98 | "- tune degree to get the 'best fit'\n", 99 | "- add data:\n", 100 | " - extreme examples can increase overfitting (assuming they are outliers).\n", 101 | " - nominal examples can reduce overfitting\n", 102 | "- switch between `Regression` and `Categorical` to try both examples.\n", 103 | "\n", 104 | "To reset the plot, re-run the cell. Click slowly to allow the plot to update before receiving the next click.\n", 105 | "\n", 106 | "Notes on implementations:\n", 107 | "- the 'ideal' curves represent the generator model to which noise was added to achieve the data set\n", 108 | "- 'fit' does not use pure gradient descent to improve speed. These methods can be used on smaller data sets. " 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "## Congratulations!\n", 116 | "You have developed some intuition about the causes and solutions to overfitting. In the next lab, you will explore a commonly used solution, Regularization." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [] 125 | } 126 | ], 127 | "metadata": { 128 | "kernelspec": { 129 | "display_name": "Python 3", 130 | "language": "python", 131 | "name": "python3" 132 | }, 133 | "language_info": { 134 | "codemirror_mode": { 135 | "name": "ipython", 136 | "version": 3 137 | }, 138 | "file_extension": ".py", 139 | "mimetype": "text/x-python", 140 | "name": "python", 141 | "nbconvert_exporter": "python", 142 | "pygments_lexer": "ipython3", 143 | "version": "3.7.6" 144 | } 145 | }, 146 | "nbformat": 4, 147 | "nbformat_minor": 5 148 | } 149 | -------------------------------------------------------------------------------- /src/deeplearning.mplstyle: -------------------------------------------------------------------------------- 1 | # see https://matplotlib.org/stable/tutorials/introductory/customizing.html 2 | lines.linewidth: 4 3 | lines.solid_capstyle: butt 4 | 5 | legend.fancybox: true 6 | 7 | # Verdana" for non-math text, 8 | # Cambria Math 9 | 10 | #Blue (Crayon-Aqua) 0096FF 11 | #Dark Red C00000 12 | #Orange (Apple Orange) FF9300 13 | #Black 000000 14 | #Magenta FF40FF 15 | #Purple 7030A0 16 | 17 | axes.prop_cycle: cycler('color', ['0096FF', 'FF9300', 'FF40FF', '7030A0', 'C00000']) 18 | #axes.facecolor: f0f0f0 # grey 19 | axes.facecolor: ffffff # white 20 | axes.labelsize: large 21 | axes.axisbelow: true 22 | axes.grid: False 23 | axes.edgecolor: f0f0f0 24 | axes.linewidth: 3.0 25 | axes.titlesize: x-large 26 | 27 | patch.edgecolor: f0f0f0 28 | patch.linewidth: 0.5 29 | 30 | svg.fonttype: path 31 | 32 | grid.linestyle: - 33 | grid.linewidth: 1.0 34 | grid.color: cbcbcb 35 | 36 | xtick.major.size: 0 37 | xtick.minor.size: 0 38 | ytick.major.size: 0 39 | ytick.minor.size: 0 40 | 41 | savefig.edgecolor: f0f0f0 42 | savefig.facecolor: f0f0f0 43 | 44 | #figure.subplot.left: 0.08 45 | #figure.subplot.right: 0.95 46 | #figure.subplot.bottom: 0.07 47 | 48 | #figure.facecolor: f0f0f0 # grey 49 | figure.facecolor: ffffff # white 50 | 51 | ## *************************************************************************** 52 | ## * FONT * 53 | ## *************************************************************************** 54 | ## The font properties used by `text.Text`. 55 | ## See https://matplotlib.org/api/font_manager_api.html for more information 56 | ## on font properties. The 6 font properties used for font matching are 57 | ## given below with their default values. 58 | ## 59 | ## The font.family property can take either a concrete font name (not supported 60 | ## when rendering text with usetex), or one of the following five generic 61 | ## values: 62 | ## - 'serif' (e.g., Times), 63 | ## - 'sans-serif' (e.g., Helvetica), 64 | ## - 'cursive' (e.g., Zapf-Chancery), 65 | ## - 'fantasy' (e.g., Western), and 66 | ## - 'monospace' (e.g., Courier). 67 | ## Each of these values has a corresponding default list of font names 68 | ## (font.serif, etc.); the first available font in the list is used. Note that 69 | ## for font.serif, font.sans-serif, and font.monospace, the first element of 70 | ## the list (a DejaVu font) will always be used because DejaVu is shipped with 71 | ## Matplotlib and is thus guaranteed to be available; the other entries are 72 | ## left as examples of other possible values. 73 | ## 74 | ## The font.style property has three values: normal (or roman), italic 75 | ## or oblique. The oblique style will be used for italic, if it is not 76 | ## present. 77 | ## 78 | ## The font.variant property has two values: normal or small-caps. For 79 | ## TrueType fonts, which are scalable fonts, small-caps is equivalent 80 | ## to using a font size of 'smaller', or about 83%% of the current font 81 | ## size. 82 | ## 83 | ## The font.weight property has effectively 13 values: normal, bold, 84 | ## bolder, lighter, 100, 200, 300, ..., 900. Normal is the same as 85 | ## 400, and bold is 700. bolder and lighter are relative values with 86 | ## respect to the current weight. 87 | ## 88 | ## The font.stretch property has 11 values: ultra-condensed, 89 | ## extra-condensed, condensed, semi-condensed, normal, semi-expanded, 90 | ## expanded, extra-expanded, ultra-expanded, wider, and narrower. This 91 | ## property is not currently implemented. 92 | ## 93 | ## The font.size property is the default font size for text, given in points. 94 | ## 10 pt is the standard value. 95 | ## 96 | ## Note that font.size controls default text sizes. To configure 97 | ## special text sizes tick labels, axes, labels, title, etc., see the rc 98 | ## settings for axes and ticks. Special text sizes can be defined 99 | ## relative to font.size, using the following values: xx-small, x-small, 100 | ## small, medium, large, x-large, xx-large, larger, or smaller 101 | 102 | 103 | font.family: sans-serif 104 | font.style: normal 105 | font.variant: normal 106 | font.weight: normal 107 | font.stretch: normal 108 | font.size: 8.0 109 | 110 | font.serif: DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif 111 | font.sans-serif: Verdana, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif 112 | font.cursive: Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursive 113 | font.fantasy: Chicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasy 114 | font.monospace: DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace 115 | 116 | 117 | ## *************************************************************************** 118 | ## * TEXT * 119 | ## *************************************************************************** 120 | ## The text properties used by `text.Text`. 121 | ## See https://matplotlib.org/api/artist_api.html#module-matplotlib.text 122 | ## for more information on text properties 123 | #text.color: black 124 | 125 | -------------------------------------------------------------------------------- /src/utils.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import random 3 | from itertools import zip_longest 4 | 5 | import imageio 6 | import IPython 7 | import matplotlib.pyplot as plt 8 | import matplotlib.ticker as mticker 9 | import numpy as np 10 | import pandas as pd 11 | import tensorflow as tf 12 | from statsmodels.iolib.table import SimpleTable 13 | 14 | 15 | SEED = 0 # seed for pseudo-random number generator 16 | MINIBATCH_SIZE = 64 # mini-batch size 17 | TAU = 1e-3 # soft update parameter 18 | E_DECAY = 0.995 # ε decay rate for ε-greedy policy 19 | E_MIN = 0.01 # minimum ε value for ε-greedy policy 20 | 21 | 22 | random.seed(SEED) 23 | 24 | 25 | def get_experiences(memory_buffer): 26 | experiences = random.sample(memory_buffer, k=MINIBATCH_SIZE) 27 | states = tf.convert_to_tensor(np.array([e.state for e in experiences if e is not None]),dtype=tf.float32) 28 | actions = tf.convert_to_tensor(np.array([e.action for e in experiences if e is not None]), dtype=tf.float32) 29 | rewards = tf.convert_to_tensor(np.array([e.reward for e in experiences if e is not None]), dtype=tf.float32) 30 | next_states = tf.convert_to_tensor(np.array([e.next_state for e in experiences if e is not None]),dtype=tf.float32) 31 | done_vals = tf.convert_to_tensor(np.array([e.done for e in experiences if e is not None]).astype(np.uint8), 32 | dtype=tf.float32) 33 | return (states, actions, rewards, next_states, done_vals) 34 | 35 | 36 | def check_update_conditions(t, num_steps_upd, memory_buffer): 37 | if (t + 1) % num_steps_upd == 0 and len(memory_buffer) > MINIBATCH_SIZE: 38 | return True 39 | else: 40 | return False 41 | 42 | 43 | def get_new_eps(epsilon): 44 | return max(E_MIN, E_DECAY*epsilon) 45 | 46 | 47 | def get_action(q_values, epsilon=0): 48 | if random.random() > epsilon: 49 | return np.argmax(q_values.numpy()[0]) 50 | else: 51 | return random.choice(np.arange(4)) 52 | 53 | 54 | def update_target_network(q_network, target_q_network): 55 | for target_weights, q_net_weights in zip(target_q_network.weights, q_network.weights): 56 | target_weights.assign(TAU * q_net_weights + (1.0 - TAU) * target_weights) 57 | 58 | 59 | def plot_history(reward_history, rolling_window=20, lower_limit=None, 60 | upper_limit=None, plot_rw=True, plot_rm=True): 61 | 62 | if lower_limit is None or upper_limit is None: 63 | rh = reward_history 64 | xs = [x for x in range(len(reward_history))] 65 | else: 66 | rh = reward_history[lower_limit:upper_limit] 67 | xs = [x for x in range(lower_limit,upper_limit)] 68 | 69 | df = pd.DataFrame(rh) 70 | rollingMean = df.rolling(rolling_window).mean() 71 | 72 | plt.figure(figsize=(10,7), facecolor='white') 73 | 74 | if plot_rw: 75 | plt.plot(xs, rh, linewidth=1, color='cyan') 76 | if plot_rm: 77 | plt.plot(xs, rollingMean, linewidth=2, color='magenta') 78 | 79 | text_color = 'black' 80 | 81 | ax = plt.gca() 82 | ax.set_facecolor('black') 83 | plt.grid() 84 | # plt.title("Total Point History", color=text_color, fontsize=40) 85 | plt.xlabel('Episode', color=text_color, fontsize=30) 86 | plt.ylabel('Total Points', color=text_color, fontsize=30) 87 | yNumFmt = mticker.StrMethodFormatter('{x:,}') 88 | ax.yaxis.set_major_formatter(yNumFmt) 89 | ax.tick_params(axis='x', colors=text_color) 90 | ax.tick_params(axis='y', colors=text_color) 91 | plt.show() 92 | 93 | 94 | def display_table(initial_state, action, next_state, reward, done): 95 | 96 | action_labels = ["Do nothing", "Fire right engine", "Fire main engine", "Fire left engine"] 97 | 98 | # Do not use column headers 99 | column_headers = None 100 | 101 | with np.printoptions(formatter={'float': '{:.3f}'.format}): 102 | table_info = [("Initial State:", [f"{initial_state}"]), 103 | ("Action:", [f"{action_labels[action]}"]), 104 | ("Next State:", [f"{next_state}"]), 105 | ("Reward Received:", [f"{reward:.3f}"]), 106 | ("Episode Terminated:", [f"{done}"])] 107 | 108 | # Generate table 109 | row_labels, data = zip_longest(*table_info) 110 | table = SimpleTable(data, column_headers, row_labels) 111 | 112 | return table 113 | 114 | 115 | def embed_mp4(filename): 116 | """Embeds an mp4 file in the notebook.""" 117 | video = open(filename,'rb').read() 118 | b64 = base64.b64encode(video) 119 | tag = ''' 120 | '''.format(b64.decode()) 124 | return IPython.display.HTML(tag) 125 | 126 | 127 | def create_video(filename, env, q_network, fps=30): 128 | with imageio.get_writer(filename, fps=fps) as video: 129 | done = False 130 | state = env.reset() 131 | frame = env.render(mode="rgb_array") 132 | video.append_data(frame) 133 | while not done: 134 | state = np.expand_dims(state, axis=0) 135 | q_values = q_network(state) 136 | action = np.argmax(q_values.numpy()[0]) 137 | state, _, done, _ = env.step(action) 138 | frame = env.render(mode="rgb_array") 139 | video.append_data(frame) -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/W3/utils.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import random 3 | from itertools import zip_longest 4 | 5 | import imageio 6 | import IPython 7 | import matplotlib.pyplot as plt 8 | import matplotlib.ticker as mticker 9 | import numpy as np 10 | import pandas as pd 11 | import tensorflow as tf 12 | from statsmodels.iolib.table import SimpleTable 13 | 14 | 15 | SEED = 0 # seed for pseudo-random number generator 16 | MINIBATCH_SIZE = 64 # mini-batch size 17 | TAU = 1e-3 # soft update parameter 18 | E_DECAY = 0.995 # ε decay rate for ε-greedy policy 19 | E_MIN = 0.01 # minimum ε value for ε-greedy policy 20 | 21 | 22 | random.seed(SEED) 23 | 24 | 25 | def get_experiences(memory_buffer): 26 | experiences = random.sample(memory_buffer, k=MINIBATCH_SIZE) 27 | states = tf.convert_to_tensor(np.array([e.state for e in experiences if e is not None]),dtype=tf.float32) 28 | actions = tf.convert_to_tensor(np.array([e.action for e in experiences if e is not None]), dtype=tf.float32) 29 | rewards = tf.convert_to_tensor(np.array([e.reward for e in experiences if e is not None]), dtype=tf.float32) 30 | next_states = tf.convert_to_tensor(np.array([e.next_state for e in experiences if e is not None]),dtype=tf.float32) 31 | done_vals = tf.convert_to_tensor(np.array([e.done for e in experiences if e is not None]).astype(np.uint8), 32 | dtype=tf.float32) 33 | return (states, actions, rewards, next_states, done_vals) 34 | 35 | 36 | def check_update_conditions(t, num_steps_upd, memory_buffer): 37 | if (t + 1) % num_steps_upd == 0 and len(memory_buffer) > MINIBATCH_SIZE: 38 | return True 39 | else: 40 | return False 41 | 42 | 43 | def get_new_eps(epsilon): 44 | return max(E_MIN, E_DECAY*epsilon) 45 | 46 | 47 | def get_action(q_values, epsilon=0): 48 | if random.random() > epsilon: 49 | return np.argmax(q_values.numpy()[0]) 50 | else: 51 | return random.choice(np.arange(4)) 52 | 53 | 54 | def update_target_network(q_network, target_q_network): 55 | for target_weights, q_net_weights in zip(target_q_network.weights, q_network.weights): 56 | target_weights.assign(TAU * q_net_weights + (1.0 - TAU) * target_weights) 57 | 58 | 59 | def plot_history(reward_history, rolling_window=20, lower_limit=None, 60 | upper_limit=None, plot_rw=True, plot_rm=True): 61 | 62 | if lower_limit is None or upper_limit is None: 63 | rh = reward_history 64 | xs = [x for x in range(len(reward_history))] 65 | else: 66 | rh = reward_history[lower_limit:upper_limit] 67 | xs = [x for x in range(lower_limit,upper_limit)] 68 | 69 | df = pd.DataFrame(rh) 70 | rollingMean = df.rolling(rolling_window).mean() 71 | 72 | plt.figure(figsize=(10,7), facecolor='white') 73 | 74 | if plot_rw: 75 | plt.plot(xs, rh, linewidth=1, color='cyan') 76 | if plot_rm: 77 | plt.plot(xs, rollingMean, linewidth=2, color='magenta') 78 | 79 | text_color = 'black' 80 | 81 | ax = plt.gca() 82 | ax.set_facecolor('black') 83 | plt.grid() 84 | # plt.title("Total Point History", color=text_color, fontsize=40) 85 | plt.xlabel('Episode', color=text_color, fontsize=30) 86 | plt.ylabel('Total Points', color=text_color, fontsize=30) 87 | yNumFmt = mticker.StrMethodFormatter('{x:,}') 88 | ax.yaxis.set_major_formatter(yNumFmt) 89 | ax.tick_params(axis='x', colors=text_color) 90 | ax.tick_params(axis='y', colors=text_color) 91 | plt.show() 92 | 93 | 94 | def display_table(initial_state, action, next_state, reward, done): 95 | 96 | action_labels = ["Do nothing", "Fire right engine", "Fire main engine", "Fire left engine"] 97 | 98 | # Do not use column headers 99 | column_headers = None 100 | 101 | with np.printoptions(formatter={'float': '{:.3f}'.format}): 102 | table_info = [("Initial State:", [f"{initial_state}"]), 103 | ("Action:", [f"{action_labels[action]}"]), 104 | ("Next State:", [f"{next_state}"]), 105 | ("Reward Received:", [f"{reward:.3f}"]), 106 | ("Episode Terminated:", [f"{done}"])] 107 | 108 | # Generate table 109 | row_labels, data = zip_longest(*table_info) 110 | table = SimpleTable(data, column_headers, row_labels) 111 | 112 | return table 113 | 114 | 115 | def embed_mp4(filename): 116 | """Embeds an mp4 file in the notebook.""" 117 | video = open(filename,'rb').read() 118 | b64 = base64.b64encode(video) 119 | tag = ''' 120 | '''.format(b64.decode()) 124 | return IPython.display.HTML(tag) 125 | 126 | 127 | def create_video(filename, env, q_network, fps=30): 128 | with imageio.get_writer(filename, fps=fps) as video: 129 | done = False 130 | state = env.reset() 131 | frame = env.render(mode="rgb_array") 132 | video.append_data(frame) 133 | while not done: 134 | state = np.expand_dims(state, axis=0) 135 | q_values = q_network(state) 136 | action = np.argmax(q_values.numpy()[0]) 137 | state, _, done, _ = env.step(action) 138 | frame = env.render(mode="rgb_array") 139 | video.append_data(frame) -------------------------------------------------------------------------------- /src/lab_coffee_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | plt.style.use('./deeplearning.mplstyle') 4 | import tensorflow as tf 5 | from tensorflow.keras.activations import sigmoid 6 | from matplotlib import cm 7 | import matplotlib.colors as colors 8 | from lab_utils_common import dlc 9 | 10 | def load_coffee_data(): 11 | """ Creates a coffee roasting data set. 12 | roasting duration: 12-15 minutes is best 13 | temperature range: 175-260C is best 14 | """ 15 | rng = np.random.default_rng(2) 16 | X = rng.random(400).reshape(-1,2) 17 | X[:,1] = X[:,1] * 4 + 11.5 # 12-15 min is best 18 | X[:,0] = X[:,0] * (285-150) + 150 # 350-500 F (175-260 C) is best 19 | Y = np.zeros(len(X)) 20 | 21 | i=0 22 | for t,d in X: 23 | y = -3/(260-175)*t + 21 24 | if (t > 175 and t < 260 and d > 12 and d < 15 and d<=y ): 25 | Y[i] = 1 26 | else: 27 | Y[i] = 0 28 | i += 1 29 | 30 | return (X, Y.reshape(-1,1)) 31 | 32 | def plt_roast(X,Y): 33 | Y = Y.reshape(-1,) 34 | colormap = np.array(['r', 'b']) 35 | fig, ax = plt.subplots(1,1,) 36 | ax.scatter(X[Y==1,0],X[Y==1,1], s=70, marker='x', c='red', label="Good Roast" ) 37 | ax.scatter(X[Y==0,0],X[Y==0,1], s=100, marker='o', facecolors='none', 38 | edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast") 39 | tr = np.linspace(175,260,50) 40 | ax.plot(tr, (-3/85) * tr + 21, color=dlc["dlpurple"],linewidth=1) 41 | ax.axhline(y=12,color=dlc["dlpurple"],linewidth=1) 42 | ax.axvline(x=175,color=dlc["dlpurple"],linewidth=1) 43 | ax.set_title(f"Coffee Roasting", size=16) 44 | ax.set_xlabel("Temperature \n(Celsius)",size=12) 45 | ax.set_ylabel("Duration \n(minutes)",size=12) 46 | ax.legend(loc='upper right') 47 | plt.show() 48 | 49 | def plt_prob(ax,fwb): 50 | """ plots a decision boundary but include shading to indicate the probability """ 51 | #setup useful ranges and common linspaces 52 | x0_space = np.linspace(150, 285 , 40) 53 | x1_space = np.linspace(11.5, 15.5 , 40) 54 | 55 | # get probability for x0,x1 ranges 56 | tmp_x0,tmp_x1 = np.meshgrid(x0_space,x1_space) 57 | z = np.zeros_like(tmp_x0) 58 | for i in range(tmp_x0.shape[0]): 59 | for j in range(tmp_x1.shape[1]): 60 | x = np.array([[tmp_x0[i,j],tmp_x1[i,j]]]) 61 | z[i,j] = fwb(x) 62 | 63 | 64 | cmap = plt.get_cmap('Blues') 65 | new_cmap = truncate_colormap(cmap, 0.0, 0.5) 66 | pcm = ax.pcolormesh(tmp_x0, tmp_x1, z, 67 | norm=cm.colors.Normalize(vmin=0, vmax=1), 68 | cmap=new_cmap, shading='nearest', alpha = 0.9) 69 | ax.figure.colorbar(pcm, ax=ax) 70 | 71 | def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100): 72 | """ truncates color map """ 73 | new_cmap = colors.LinearSegmentedColormap.from_list( 74 | 'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval), 75 | cmap(np.linspace(minval, maxval, n))) 76 | return new_cmap 77 | 78 | def plt_layer(X,Y,W1,b1,norm_l): 79 | Y = Y.reshape(-1,) 80 | fig,ax = plt.subplots(1,W1.shape[1], figsize=(16,4)) 81 | for i in range(W1.shape[1]): 82 | layerf= lambda x : sigmoid(np.dot(norm_l(x),W1[:,i]) + b1[i]) 83 | plt_prob(ax[i], layerf) 84 | ax[i].scatter(X[Y==1,0],X[Y==1,1], s=70, marker='x', c='red', label="Good Roast" ) 85 | ax[i].scatter(X[Y==0,0],X[Y==0,1], s=100, marker='o', facecolors='none', 86 | edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast") 87 | tr = np.linspace(175,260,50) 88 | ax[i].plot(tr, (-3/85) * tr + 21, color=dlc["dlpurple"],linewidth=2) 89 | ax[i].axhline(y= 12, color=dlc["dlpurple"], linewidth=2) 90 | ax[i].axvline(x=175, color=dlc["dlpurple"], linewidth=2) 91 | ax[i].set_title(f"Layer 1, unit {i}") 92 | ax[i].set_xlabel("Temperature \n(Celsius)",size=12) 93 | ax[0].set_ylabel("Duration \n(minutes)",size=12) 94 | plt.show() 95 | 96 | def plt_network(X,Y,netf): 97 | fig, ax = plt.subplots(1,2,figsize=(16,4)) 98 | Y = Y.reshape(-1,) 99 | plt_prob(ax[0], netf) 100 | ax[0].scatter(X[Y==1,0],X[Y==1,1], s=70, marker='x', c='red', label="Good Roast" ) 101 | ax[0].scatter(X[Y==0,0],X[Y==0,1], s=100, marker='o', facecolors='none', 102 | edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast") 103 | ax[0].plot(X[:,0], (-3/85) * X[:,0] + 21, color=dlc["dlpurple"],linewidth=1) 104 | ax[0].axhline(y= 12, color=dlc["dlpurple"], linewidth=1) 105 | ax[0].axvline(x=175, color=dlc["dlpurple"], linewidth=1) 106 | ax[0].set_xlabel("Temperature \n(Celsius)",size=12) 107 | ax[0].set_ylabel("Duration \n(minutes)",size=12) 108 | ax[0].legend(loc='upper right') 109 | ax[0].set_title(f"network probability") 110 | 111 | ax[1].plot(X[:,0], (-3/85) * X[:,0] + 21, color=dlc["dlpurple"],linewidth=1) 112 | ax[1].axhline(y= 12, color=dlc["dlpurple"], linewidth=1) 113 | ax[1].axvline(x=175, color=dlc["dlpurple"], linewidth=1) 114 | fwb = netf(X) 115 | yhat = (fwb > 0.5).astype(int) 116 | ax[1].scatter(X[yhat[:,0]==1,0],X[yhat[:,0]==1,1], s=70, marker='x', c='orange', label="Predicted Good Roast" ) 117 | ax[1].scatter(X[yhat[:,0]==0,0],X[yhat[:,0]==0,1], s=100, marker='o', facecolors='none', 118 | edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast") 119 | ax[1].set_title(f"network decision") 120 | ax[1].set_xlabel("Temperature \n(Celsius)",size=12) 121 | ax[1].set_ylabel("Duration \n(minutes)",size=12) 122 | ax[1].legend(loc='upper right') 123 | 124 | 125 | def plt_output_unit(W,b): 126 | """ plots a single unit function with 3 inputs """ 127 | steps = 10 128 | fig = plt.figure() 129 | ax = fig.add_subplot(projection='3d') 130 | x_ = np.linspace(0., 1., steps) 131 | y_ = np.linspace(0., 1., steps) 132 | z_ = np.linspace(0., 1., steps) 133 | x, y, z = np.meshgrid(x_, y_, z_, indexing='ij') 134 | d = np.zeros((steps,steps,steps)) 135 | cmap = plt.get_cmap('Blues') 136 | for i in range(steps): 137 | for j in range(steps): 138 | for k in range(steps): 139 | v = np.array([x[i,j,k],y[i,j,k],z[i,j,k]]) 140 | d[i,j,k] = tf.keras.activations.sigmoid(np.dot(v,W[:,0])+b).numpy() 141 | pcm = ax.scatter(x, y, z, c=d, cmap=cmap, alpha = 1 ) 142 | ax.set_xlabel("unit 0"); 143 | ax.set_ylabel("unit 1"); 144 | ax.set_zlabel("unit 2"); 145 | ax.view_init(30, -120) 146 | ax.figure.colorbar(pcm, ax=ax) 147 | ax.set_title(f"Layer 2, output unit") 148 | 149 | plt.show() -------------------------------------------------------------------------------- /Advanced Learning Algorithms/W4/public_tests.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def compute_entropy_test(target): 4 | y = np.array([1] * 10) 5 | result = target(y) 6 | 7 | assert result == 0, "Entropy must be 0 with array of ones" 8 | 9 | y = np.array([0] * 10) 10 | result = target(y) 11 | 12 | assert result == 0, "Entropy must be 0 with array of zeros" 13 | 14 | y = np.array([0] * 12 + [1] * 12) 15 | result = target(y) 16 | 17 | assert result == 1, "Entropy must be 1 with same ammount of ones and zeros" 18 | 19 | y = np.array([1, 0, 1, 0, 1, 1, 1, 0, 1]) 20 | assert np.isclose(target(y), 0.918295, atol=1e-6), "Wrong value. Something between 0 and 1" 21 | assert np.isclose(target(-y + 1), target(y), atol=1e-6), "Wrong value" 22 | 23 | print("\033[92m All tests passed.") 24 | 25 | def split_dataset_test(target): 26 | X = np.array([[1, 0], 27 | [1, 0], 28 | [1, 1], 29 | [0, 0], 30 | [0, 1]]) 31 | X_t = np.array([[0, 1, 0, 1, 0]]) 32 | X = np.concatenate((X, X_t.T), axis=1) 33 | 34 | left, right = target(X, list(range(5)), 2) 35 | expected = {'left': np.array([1, 3]), 36 | 'right': np.array([0, 2, 4])} 37 | 38 | assert type(left) == list, f"Wrong type for left. Expected: list got: {type(left)}" 39 | assert type(right) == list, f"Wrong type for right. Expected: list got: {type(right)}" 40 | 41 | assert type(left[0]) == int, f"Wrong type for elements in the left list. Expected: int got: {type(left[0])}" 42 | assert type(right[0]) == int, f"Wrong type for elements in the right list. Expected: number got: {type(right[0])}" 43 | 44 | assert len(left) == 2, f"left must have 2 elements but got: {len(left)}" 45 | assert len(right) == 3, f"right must have 3 elements but got: {len(right)}" 46 | 47 | assert np.allclose(right, expected['right']), f"Wrong value for right. Expected: { expected['right']} \ngot: {right}" 48 | assert np.allclose(left, expected['left']), f"Wrong value for left. Expected: { expected['left']} \ngot: {left}" 49 | 50 | X = np.array([[0, 1], 51 | [1, 1], 52 | [1, 1], 53 | [0, 0], 54 | [1, 0]]) 55 | X_t = np.array([[0, 1, 0, 1, 0]]) 56 | X = np.concatenate((X_t.T, X), axis=1) 57 | 58 | left, right = target(X, list(range(5)), 0) 59 | expected = {'left': np.array([1, 3]), 60 | 'right': np.array([0, 2, 4])} 61 | 62 | assert np.allclose(right, expected['right']) and np.allclose(left, expected['left']), f"Wrong value when target is at index 0." 63 | 64 | X = (np.random.rand(11, 3) > 0.5) * 1 # Just random binary numbers 65 | X_t = np.array([[0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0]]) 66 | X = np.concatenate((X, X_t.T), axis=1) 67 | 68 | left, right = target(X, [1, 2, 3, 6, 7, 9, 10], 3) 69 | expected = {'left': np.array([1, 3, 6]), 70 | 'right': np.array([2, 7, 9, 10])} 71 | 72 | assert np.allclose(right, expected['right']) and np.allclose(left, expected['left']), f"Wrong value when target is at index 0. \nExpected: {expected} \ngot: \{left:{left}, 'right': {right}\}" 73 | 74 | 75 | print("\033[92m All tests passed.") 76 | 77 | def compute_information_gain_test(target): 78 | X = np.array([[1, 0], 79 | [1, 0], 80 | [1, 0], 81 | [0, 0], 82 | [0, 1]]) 83 | 84 | y = np.array([[0, 0, 0, 0, 0]]).T 85 | node_indexes = list(range(5)) 86 | 87 | result1 = target(X, y, node_indexes, 0) 88 | result2 = target(X, y, node_indexes, 0) 89 | 90 | assert result1 == 0 and result2 == 0, f"Information gain must be 0 when target variable is pure. Got {result1} and {result2}" 91 | 92 | y = np.array([[0, 1, 0, 1, 0]]).T 93 | node_indexes = list(range(5)) 94 | 95 | result = target(X, y, node_indexes, 0) 96 | assert np.isclose(result, 0.019973, atol=1e-6), f"Wrong information gain. Expected {0.019973} got: {result}" 97 | 98 | result = target(X, y, node_indexes, 1) 99 | assert np.isclose(result, 0.170951, atol=1e-6), f"Wrong information gain. Expected {0.170951} got: {result}" 100 | 101 | node_indexes = list(range(4)) 102 | result = target(X, y, node_indexes, 0) 103 | assert np.isclose(result, 0.311278, atol=1e-6), f"Wrong information gain. Expected {0.311278} got: {result}" 104 | 105 | result = target(X, y, node_indexes, 1) 106 | assert np.isclose(result, 0, atol=1e-6), f"Wrong information gain. Expected {0.0} got: {result}" 107 | 108 | print("\033[92m All tests passed.") 109 | 110 | def get_best_split_test(target): 111 | X = np.array([[1, 0], 112 | [1, 0], 113 | [1, 0], 114 | [0, 0], 115 | [0, 1]]) 116 | 117 | y = np.array([[0, 0, 0, 0, 0]]).T 118 | node_indexes = list(range(5)) 119 | 120 | result = target(X, y, node_indexes) 121 | 122 | assert result == -1, f"When the target variable is pure, there is no best split to do. Expected -1, got {result}" 123 | 124 | y = X[:,0] 125 | result = target(X, y, node_indexes) 126 | assert result == 0, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 0, got {result}" 127 | y = X[:,1] 128 | result = target(X, y, node_indexes) 129 | assert result == 1, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 1, got {result}" 130 | 131 | y = 1 - X[:,0] 132 | result = target(X, y, node_indexes) 133 | assert result == 0, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 0, got {result}" 134 | 135 | y = np.array([[0, 1, 0, 1, 0]]).T 136 | result = target(X, y, node_indexes) 137 | assert result == 1, f"Wrong result. Expected 1, got {result}" 138 | 139 | y = np.array([[0, 1, 0, 1, 0]]).T 140 | node_indexes = [2, 3, 4] 141 | result = target(X, y, node_indexes) 142 | assert result == 0, f"Wrong result. Expected 0, got {result}" 143 | 144 | n_samples = 100 145 | X0 = np.array([[1] * n_samples]) 146 | X1 = np.array([[0] * n_samples]) 147 | X2 = (np.random.rand(1, 100) > 0.5) * 1 148 | X3 = np.array([[1] * int(n_samples / 2) + [0] * int(n_samples / 2)]) 149 | 150 | y = X2.T 151 | node_indexes = list(range(20, 80)) 152 | X = np.array([X0, X1, X2, X3]).T.reshape(n_samples, 4) 153 | result = target(X, y, node_indexes) 154 | 155 | assert result == 2, f"Wrong result. Expected 2, got {result}" 156 | 157 | y = X0.T 158 | result = target(X, y, node_indexes) 159 | assert result == -1, f"When the target variable is pure, there is no best split to do. Expected -1, got {result}" 160 | print("\033[92m All tests passed.") -------------------------------------------------------------------------------- /src/autils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import tensorflow as tf 4 | from tensorflow.keras.models import Sequential 5 | from tensorflow.keras.layers import Dense 6 | from tensorflow.keras.activations import linear, relu, sigmoid 7 | 8 | dlc = dict(dlblue = '#0096ff', dlorange = '#FF9300', dldarkred='#C00000', dlmagenta='#FF40FF', dlpurple='#7030A0', dldarkblue = '#0D5BDC', dlmedblue='#4285F4') 9 | dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0'; dldarkblue = '#0D5BDC'; dlmedblue='#4285F4' 10 | dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple] 11 | plt.style.use('./deeplearning.mplstyle') 12 | 13 | 14 | def load_data(): 15 | X = np.load("data/X.npy") 16 | y = np.load("data/y.npy") 17 | return X, y 18 | 19 | def plt_act_trio(): 20 | X = np.linspace(-5,5,100) 21 | fig,ax = plt.subplots(1,3, figsize=(6,2)) 22 | widgvis(fig) 23 | ax[0].plot(X,tf.keras.activations.linear(X)) 24 | ax[0].axvline(0, lw=0.3, c="black") 25 | ax[0].axhline(0, lw=0.3, c="black") 26 | ax[0].set_title("Linear") 27 | ax[1].plot(X,tf.keras.activations.sigmoid(X)) 28 | ax[1].axvline(0, lw=0.3, c="black") 29 | ax[1].axhline(0, lw=0.3, c="black") 30 | ax[1].set_title("Sigmoid") 31 | ax[2].plot(X,tf.keras.activations.relu(X)) 32 | ax[2].axhline(0, lw=0.3, c="black") 33 | ax[2].axvline(0, lw=0.3, c="black") 34 | ax[2].set_title("ReLu") 35 | fig.suptitle("Common Activation Functions", fontsize=14) 36 | fig.tight_layout(pad=0.2) 37 | plt.show() 38 | 39 | def widgvis(fig): 40 | fig.canvas.toolbar_visible = False 41 | fig.canvas.header_visible = False 42 | fig.canvas.footer_visible = False 43 | 44 | def plt_ex1(): 45 | X = np.linspace(0,2*np.pi, 100) 46 | y = np.cos(X)+1 47 | y[50:100]=0 48 | fig,ax = plt.subplots(1,1, figsize=(2,2)) 49 | widgvis(fig) 50 | ax.set_title("Target") 51 | ax.set_xlabel("x") 52 | ax.set_ylabel("y") 53 | ax.plot(X,y) 54 | fig.tight_layout(pad=0.1) 55 | plt.show() 56 | return(X,y) 57 | 58 | def plt_ex2(): 59 | X = np.linspace(0,2*np.pi, 100) 60 | y = np.cos(X)+1 61 | y[0:49]=0 62 | fig,ax = plt.subplots(1,1, figsize=(2,2)) 63 | widgvis(fig) 64 | ax.set_title("Target") 65 | ax.set_xlabel("x") 66 | ax.set_ylabel("y") 67 | ax.plot(X,y) 68 | fig.tight_layout(pad=0.1) 69 | plt.show() 70 | return(X,y) 71 | 72 | def gen_data(): 73 | X = np.linspace(0,2*np.pi, 100) 74 | y = np.cos(X)+1 75 | X=X.reshape(-1,1) 76 | return(X,y) 77 | 78 | def plt_dual(X,y,yhat): 79 | fig,ax = plt.subplots(1,2, figsize=(4,2)) 80 | widgvis(fig) 81 | ax[0].set_title("Target") 82 | ax[0].set_xlabel("x") 83 | ax[0].set_ylabel("y") 84 | ax[0].plot(X,y) 85 | ax[1].set_title("Prediction") 86 | ax[1].set_xlabel("x") 87 | ax[1].set_ylabel("y") 88 | ax[1].plot(X,y) 89 | ax[1].plot(X,yhat) 90 | fig.tight_layout(pad=0.1) 91 | plt.show() 92 | 93 | def plt_act1(X,y,z,a): 94 | fig,ax = plt.subplots(1,3, figsize=(6,2.5)) 95 | widgvis(fig) 96 | ax[0].plot(X,y,label="target") 97 | ax[0].axvline(0, lw=0.3, c="black") 98 | ax[0].axhline(0, lw=0.3, c="black") 99 | ax[0].set_title("y - target") 100 | ax[1].plot(X,y, label="target") 101 | ax[1].plot(X,z, c=dlc["dldarkred"],label="z") 102 | ax[1].axvline(0, lw=0.3, c="black") 103 | ax[1].axhline(0, lw=0.3, c="black") 104 | ax[1].set_title(r"$z = w \cdot x+b$") 105 | ax[1].legend(loc="upper center") 106 | ax[2].plot(X,y, label="target") 107 | ax[2].plot(X,a, c=dlc["dldarkred"],label="ReLu(z)") 108 | ax[2].axhline(0, lw=0.3, c="black") 109 | ax[2].axvline(0, lw=0.3, c="black") 110 | ax[2].set_title("max(0,z)") 111 | ax[2].legend() 112 | fig.suptitle("Role of Non-Linear Activation", fontsize=12) 113 | fig.tight_layout(pad=0.22) 114 | return(ax) 115 | 116 | 117 | def plt_add_notation(ax): 118 | ax[1].annotate(text = "matches\n here", xy =(1.5,1.0), 119 | xytext = (0.1,-1.5), fontsize=9, 120 | arrowprops=dict(facecolor=dlc["dlpurple"],width=2, headwidth=8)) 121 | ax[1].annotate(text = "but not\n here", xy =(5,-2.5), 122 | xytext = (1,-3), fontsize=9, 123 | arrowprops=dict(facecolor=dlc["dlpurple"],width=2, headwidth=8)) 124 | ax[2].annotate(text = "ReLu\n 'off'", xy =(2.6,0), 125 | xytext = (0.1,0.1), fontsize=9, 126 | arrowprops=dict(facecolor=dlc["dlpurple"],width=2, headwidth=8)) 127 | 128 | def compile_fit(model,X,y): 129 | model.compile( 130 | loss=tf.keras.losses.MeanSquaredError(), 131 | optimizer=tf.keras.optimizers.Adam(0.01), 132 | ) 133 | 134 | model.fit( 135 | X,y, 136 | epochs=100, 137 | verbose = 0 138 | ) 139 | l1=model.get_layer("l1") 140 | l2=model.get_layer("l2") 141 | w1,b1 = l1.get_weights() 142 | w2,b2 = l2.get_weights() 143 | return(w1,b1,w2,b2) 144 | 145 | def plt_model(X,y,yhat_pre, yhat_post): 146 | fig,ax = plt.subplots(1,3, figsize=(8,2)) 147 | widgvis(fig) 148 | ax[0].set_title("Target") 149 | ax[0].set_xlabel("x") 150 | ax[0].set_ylabel("y") 151 | ax[0].plot(X,y) 152 | ax[1].set_title("Prediction, pre-training") 153 | ax[1].set_xlabel("x") 154 | ax[1].set_ylabel("y") 155 | ax[1].plot(X,y) 156 | ax[1].plot(X,yhat_pre) 157 | ax[2].set_title("Prediction, post-training") 158 | ax[2].set_xlabel("x") 159 | ax[2].set_ylabel("y") 160 | ax[2].plot(X,y) 161 | ax[2].plot(X,yhat_post) 162 | fig.tight_layout(pad=0.1) 163 | plt.show() 164 | 165 | def display_errors(model,X,y): 166 | f = model.predict(X) 167 | yhat = np.argmax(f, axis=1) 168 | doo = yhat != y[:,0] 169 | idxs = np.where(yhat != y[:,0])[0] 170 | if len(idxs) == 0: 171 | print("no errors found") 172 | else: 173 | cnt = min(8, len(idxs)) 174 | fig, ax = plt.subplots(1,cnt, figsize=(5,1.2)) 175 | fig.tight_layout(pad=0.13,rect=[0, 0.03, 1, 0.80]) #[left, bottom, right, top] 176 | widgvis(fig) 177 | 178 | for i in range(cnt): 179 | j = idxs[i] 180 | X_reshaped = X[j].reshape((20,20)).T 181 | 182 | # Display the image 183 | ax[i].imshow(X_reshaped, cmap='gray') 184 | 185 | # Predict using the Neural Network 186 | prediction = model.predict(X[j].reshape(1,400)) 187 | prediction_p = tf.nn.softmax(prediction) 188 | yhat = np.argmax(prediction_p) 189 | 190 | # Display the label above the image 191 | ax[i].set_title(f"{y[j,0]},{yhat}",fontsize=10) 192 | ax[i].set_axis_off() 193 | fig.suptitle("Label, yhat", fontsize=12) 194 | return(len(idxs)) 195 | 196 | def display_digit(X): 197 | """ display a single digit. The input is one digit (400,). """ 198 | fig, ax = plt.subplots(1,1, figsize=(0.5,0.5)) 199 | widgvis(fig) 200 | X_reshaped = X.reshape((20,20)).T 201 | # Display the image 202 | ax.imshow(X_reshaped, cmap='gray') 203 | plt.show() 204 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W1/C1_W1_Lab04_Cost_function_Soln.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # Optional Lab: Cost Function 5 | #
6 | #
7 | #
8 | # 9 | # 10 | 11 | # ## Goals 12 | # In this lab you will: 13 | # - you will implement and explore the `cost` function for linear regression with one variable. 14 | # 15 | 16 | # ## Tools 17 | # In this lab we will make use of: 18 | # - NumPy, a popular library for scientific computing 19 | # - Matplotlib, a popular library for plotting data 20 | # - local plotting routines in the lab_utils_uni.py file in the local directory 21 | 22 | # In[1]: 23 | 24 | 25 | import numpy as np 26 | get_ipython().run_line_magic('matplotlib', 'widget') 27 | import matplotlib.pyplot as plt 28 | from lab_utils_uni import plt_intuition, plt_stationary, plt_update_onclick, soup_bowl 29 | plt.style.use('./deeplearning.mplstyle') 30 | 31 | 32 | # ## Problem Statement 33 | # 34 | # You would like a model which can predict housing prices given the size of the house. 35 | # Let's use the same two data points as before the previous lab- a house with 1000 square feet sold for \\$300,000 and a house with 2000 square feet sold for \\$500,000. 36 | # 37 | # 38 | # | Size (1000 sqft) | Price (1000s of dollars) | 39 | # | -------------------| ------------------------ | 40 | # | 1 | 300 | 41 | # | 2 | 500 | 42 | # 43 | 44 | # In[2]: 45 | 46 | 47 | x_train = np.array([1.0, 2.0]) #(size in 1000 square feet) 48 | y_train = np.array([300.0, 500.0]) #(price in 1000s of dollars) 49 | 50 | 51 | # ## Computing Cost 52 | # The term 'cost' in this assignment might be a little confusing since the data is housing cost. Here, cost is a measure how well our model is predicting the target price of the house. The term 'price' is used for housing data. 53 | # 54 | # The equation for cost with one variable is: 55 | # $$J(w,b) = \frac{1}{2m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)})^2 \tag{1}$$ 56 | # 57 | # where 58 | # $$f_{w,b}(x^{(i)}) = wx^{(i)} + b \tag{2}$$ 59 | # 60 | # - $f_{w,b}(x^{(i)})$ is our prediction for example $i$ using parameters $w,b$. 61 | # - $(f_{w,b}(x^{(i)}) -y^{(i)})^2$ is the squared difference between the target value and the prediction. 62 | # - These differences are summed over all the $m$ examples and divided by `2m` to produce the cost, $J(w,b)$. 63 | # >Note, in lecture summation ranges are typically from 1 to m, while code will be from 0 to m-1. 64 | # 65 | 66 | # The code below calculates cost by looping over each example. In each loop: 67 | # - `f_wb`, a prediction is calculated 68 | # - the difference between the target and the prediction is calculated and squared. 69 | # - this is added to the total cost. 70 | 71 | # In[3]: 72 | 73 | 74 | def compute_cost(x, y, w, b): 75 | """ 76 | Computes the cost function for linear regression. 77 | 78 | Args: 79 | x (ndarray (m,)): Data, m examples 80 | y (ndarray (m,)): target values 81 | w,b (scalar) : model parameters 82 | 83 | Returns 84 | total_cost (float): The cost of using w,b as the parameters for linear regression 85 | to fit the data points in x and y 86 | """ 87 | # number of training examples 88 | m = x.shape[0] 89 | 90 | cost_sum = 0 91 | for i in range(m): 92 | f_wb = w * x[i] + b 93 | cost = (f_wb - y[i]) ** 2 94 | cost_sum = cost_sum + cost 95 | total_cost = (1 / (2 * m)) * cost_sum 96 | 97 | return total_cost 98 | 99 | 100 | # ## Cost Function Intuition 101 | 102 | # Your goal is to find a model $f_{w,b}(x) = wx + b$, with parameters $w,b$, which will accurately predict house values given an input $x$. The cost is a measure of how accurate the model is on the training data. 103 | # 104 | # The cost equation (1) above shows that if $w$ and $b$ can be selected such that the predictions $f_{w,b}(x)$ match the target data $y$, the $(f_{w,b}(x^{(i)}) - y^{(i)})^2 $ term will be zero and the cost minimized. In this simple two point example, you can achieve this! 105 | # 106 | # In the previous lab, you determined that $b=100$ provided an optimal solution so let's set $b$ to 100 and focus on $w$. 107 | # 108 | #
109 | # Below, use the slider control to select the value of $w$ that minimizes cost. It can take a few seconds for the plot to update. 110 | 111 | # In[4]: 112 | 113 | 114 | plt_intuition(x_train,y_train) 115 | 116 | 117 | # The plot contains a few points that are worth mentioning. 118 | # - cost is minimized when $w = 200$, which matches results from the previous lab 119 | # - Because the difference between the target and pediction is squared in the cost equation, the cost increases rapidly when $w$ is either too large or too small. 120 | # - Using the `w` and `b` selected by minimizing cost results in a line which is a perfect fit to the data. 121 | 122 | # ## Cost Function Visualization- 3D 123 | # 124 | # You can see how cost varies with respect to *both* `w` and `b` by plotting in 3D or using a contour plot. 125 | # It is worth noting that some of the plotting in this course can become quite involved. The plotting routines are provided and while it can be instructive to read through the code to become familiar with the methods, it is not needed to complete the course successfully. The routines are in lab_utils_uni.py in the local directory. 126 | 127 | # ### Larger Data Set 128 | # It's use instructive to view a scenario with a few more data points. This data set includes data points that do not fall on the same line. What does that mean for the cost equation? Can we find $w$, and $b$ that will give us a cost of 0? 129 | 130 | # In[5]: 131 | 132 | 133 | x_train = np.array([1.0, 1.7, 2.0, 2.5, 3.0, 3.2]) 134 | y_train = np.array([250, 300, 480, 430, 630, 730,]) 135 | 136 | 137 | # In the contour plot, click on a point to select `w` and `b` to achieve the lowest cost. Use the contours to guide your selections. Note, it can take a few seconds to update the graph. 138 | 139 | # In[6]: 140 | 141 | 142 | plt.close('all') 143 | fig, ax, dyn_items = plt_stationary(x_train, y_train) 144 | updater = plt_update_onclick(fig, ax, x_train, y_train, dyn_items) 145 | 146 | 147 | # Above, note the dashed lines in the left plot. These represent the portion of the cost contributed by each example in your training set. In this case, values of approximately $w=209$ and $b=2.4$ provide low cost. Note that, because our training examples are not on a line, the minimum cost is not zero. 148 | 149 | # ### Convex Cost surface 150 | # The fact that the cost function squares the loss ensures that the 'error surface' is convex like a soup bowl. It will always have a minimum that can be reached by following the gradient in all dimensions. In the previous plot, because the $w$ and $b$ dimensions scale differently, this is not easy to recognize. The following plot, where $w$ and $b$ are symmetric, was shown in lecture: 151 | 152 | # In[7]: 153 | 154 | 155 | soup_bowl() 156 | 157 | 158 | # # Congratulations! 159 | # You have learned the following: 160 | # - The cost equation provides a measure of how well your predictions match your training data. 161 | # - Minimizing the cost can provide optimal values of $w$, $b$. 162 | 163 | # In[ ]: 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /src/lab_utils_multiclass_TF.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import matplotlib as mpl 4 | import warnings 5 | from matplotlib import cm 6 | from matplotlib.patches import FancyArrowPatch 7 | from matplotlib.colors import ListedColormap, LinearSegmentedColormap 8 | import matplotlib.colors as colors 9 | from lab_utils_common import dlc 10 | from matplotlib import cm 11 | 12 | 13 | 14 | dlc = dict(dlblue = '#0096ff', dlorange = '#FF9300', dldarkred='#C00000', dlmagenta='#FF40FF', dlpurple='#7030A0', dldarkblue = '#0D5BDC') 15 | dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0'; dldarkblue = '#0D5BDC' 16 | dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple] 17 | plt.style.use('./deeplearning.mplstyle') 18 | 19 | dkcolors = plt.cm.Paired((1,3,7,9,5,11)) 20 | ltcolors = plt.cm.Paired((0,2,6,8,4,10)) 21 | dkcolors_map = mpl.colors.ListedColormap(dkcolors) 22 | ltcolors_map = mpl.colors.ListedColormap(ltcolors) 23 | 24 | #Plot a multi-class categorical decision boundary 25 | # This version handles a non-vector prediction (adds a for-loop over points) 26 | def plot_cat_decision_boundary_mc(ax, X, predict , class_labels=None, legend=False, vector=True): 27 | 28 | # create a mesh to points to plot 29 | x_min, x_max = X[:, 0].min()- 0.5, X[:, 0].max()+0.5 30 | y_min, y_max = X[:, 1].min()- 0.5, X[:, 1].max()+0.5 31 | h = max(x_max-x_min, y_max-y_min)/100 32 | xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 33 | np.arange(y_min, y_max, h)) 34 | points = np.c_[xx.ravel(), yy.ravel()] 35 | #print("points", points.shape) 36 | #print("xx.shape", xx.shape) 37 | 38 | #make predictions for each point in mesh 39 | if vector: 40 | Z = predict(points) 41 | else: 42 | Z = np.zeros((len(points),)) 43 | for i in range(len(points)): 44 | Z[i] = predict(points[i].reshape(1,2)) 45 | Z = Z.reshape(xx.shape) 46 | 47 | #contour plot highlights boundaries between values - classes in this case 48 | ax.contour(xx, yy, Z, linewidths=1) 49 | #ax.axis('tight') 50 | 51 | 52 | def plt_mc_data(ax, X, y, classes, class_labels=None, map=plt.cm.Paired, 53 | legend=False, size=50, m='o', equal_xy = False): 54 | """ Plot multiclass data. Note, if equal_xy is True, setting ylim on the plot may not work """ 55 | for i in range(classes): 56 | idx = np.where(y == i) 57 | col = len(idx[0])*[i] 58 | label = class_labels[i] if class_labels else "c{}".format(i) 59 | # this didn't work on coursera but did in local version 60 | #ax.scatter(X[idx, 0], X[idx, 1], marker=m, 61 | # c=col, vmin=0, vmax=map.N, cmap=map, 62 | # s=size, label=label) 63 | ax.scatter(X[idx, 0], X[idx, 1], marker=m, 64 | color=map(col), vmin=0, vmax=map.N, 65 | s=size, label=label) 66 | if legend: ax.legend() 67 | if equal_xy: ax.axis("equal") 68 | 69 | def plt_mc(X_train,y_train,classes, centers, std): 70 | css = np.unique(y_train) 71 | fig,ax = plt.subplots(1,1,figsize=(3,3)) 72 | fig.canvas.toolbar_visible = False 73 | fig.canvas.header_visible = False 74 | fig.canvas.footer_visible = False 75 | plt_mc_data(ax, X_train,y_train,classes, map=dkcolors_map, legend=True, size=50, equal_xy = False) 76 | ax.set_title("Multiclass Data") 77 | ax.set_xlabel("x0") 78 | ax.set_ylabel("x1") 79 | #for c in css: 80 | # circ = plt.Circle(centers[c], 2*std, color=dkcolors_map(c), clip_on=False, fill=False, lw=0.5) 81 | # ax.add_patch(circ) 82 | plt.show() 83 | 84 | def plt_cat_mc(X_train, y_train, model, classes): 85 | #make a model for plotting routines to call 86 | model_predict = lambda Xl: np.argmax(model.predict(Xl),axis=1) 87 | 88 | fig,ax = plt.subplots(1,1, figsize=(3,3)) 89 | fig.canvas.toolbar_visible = False 90 | fig.canvas.header_visible = False 91 | fig.canvas.footer_visible = False 92 | 93 | #add the original data to the decison boundary 94 | plt_mc_data(ax, X_train,y_train, classes, map=dkcolors_map, legend=True) 95 | #plot the decison boundary. 96 | plot_cat_decision_boundary_mc(ax, X_train, model_predict, vector=True) 97 | ax.set_title("model decision boundary") 98 | 99 | plt.xlabel(r'$x_0$'); 100 | plt.ylabel(r"$x_1$"); 101 | plt.show() 102 | 103 | 104 | def plt_prob_z(ax,fwb, x0_rng=(-8,8), x1_rng=(-5,4)): 105 | """ plots a decision boundary but include shading to indicate the probability 106 | and adds a conouter to show where z=0 107 | """ 108 | #setup useful ranges and common linspaces 109 | x0_space = np.linspace(x0_rng[0], x0_rng[1], 40) 110 | x1_space = np.linspace(x1_rng[0], x1_rng[1], 40) 111 | 112 | # get probability for x0,x1 ranges 113 | tmp_x0,tmp_x1 = np.meshgrid(x0_space,x1_space) 114 | z = np.zeros_like(tmp_x0) 115 | c = np.zeros_like(tmp_x0) 116 | for i in range(tmp_x0.shape[0]): 117 | for j in range(tmp_x1.shape[1]): 118 | x = np.array([[tmp_x0[i,j],tmp_x1[i,j]]]) 119 | z[i,j] = fwb(x) 120 | c[i,j] = 0. if z[i,j] == 0 else 1. 121 | with warnings.catch_warnings(): # suppress no contour warning 122 | warnings.simplefilter("ignore") 123 | #ax.contour(tmp_x0, tmp_x1, c, colors='b', linewidths=1) 124 | ax.contour(tmp_x0, tmp_x1, c, linewidths=1) 125 | 126 | cmap = plt.get_cmap('Blues') 127 | new_cmap = truncate_colormap(cmap, 0.0, 0.7) 128 | 129 | pcm = ax.pcolormesh(tmp_x0, tmp_x1, z, 130 | norm=cm.colors.Normalize(vmin=np.amin(z), vmax=np.amax(z)), 131 | cmap=new_cmap, shading='nearest', alpha = 0.9) 132 | ax.figure.colorbar(pcm, ax=ax) 133 | 134 | def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100): 135 | """ truncates color map """ 136 | new_cmap = colors.LinearSegmentedColormap.from_list( 137 | 'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval), 138 | cmap(np.linspace(minval, maxval, n))) 139 | return new_cmap 140 | 141 | 142 | def plt_layer_relu(X, Y, W1, b1, classes): 143 | nunits = (W1.shape[1]) 144 | Y = Y.reshape(-1,) 145 | fig,ax = plt.subplots(1,W1.shape[1], figsize=(7,2.5)) 146 | fig.canvas.toolbar_visible = False 147 | fig.canvas.header_visible = False 148 | fig.canvas.footer_visible = False 149 | 150 | for i in range(nunits): 151 | layerf= lambda x : np.maximum(0,(np.dot(x,W1[:,i]) + b1[i])) 152 | plt_prob_z(ax[i], layerf) 153 | plt_mc_data(ax[i], X, Y, classes, map=dkcolors_map,legend=True, size=50, m='o') 154 | ax[i].set_title(f"Layer 1 Unit {i}") 155 | ax[i].set_ylabel(r"$x_1$",size=10) 156 | ax[i].set_xlabel(r"$x_0$",size=10) 157 | fig.tight_layout() 158 | plt.show() 159 | 160 | 161 | def plt_output_layer_linear(X, Y, W, b, classes, x0_rng=None, x1_rng=None): 162 | nunits = (W.shape[1]) 163 | Y = Y.reshape(-1,) 164 | fig,ax = plt.subplots(2,int(nunits/2), figsize=(7,5)) 165 | fig.canvas.toolbar_visible = False 166 | fig.canvas.header_visible = False 167 | fig.canvas.footer_visible = False 168 | for i,axi in enumerate(ax.flat): 169 | layerf = lambda x : np.dot(x,W[:,i]) + b[i] 170 | plt_prob_z(axi, layerf, x0_rng=x0_rng, x1_rng=x1_rng) 171 | plt_mc_data(axi, X, Y, classes, map=dkcolors_map,legend=True, size=50, m='o') 172 | axi.set_ylabel(r"$a^{[1]}_1$",size=9) 173 | axi.set_xlabel(r"$a^{[1]}_0$",size=9) 174 | axi.set_xlim(x0_rng) 175 | axi.set_ylim(x1_rng) 176 | axi.set_title(f"Linear Output Unit {i}") 177 | fig.tight_layout() 178 | plt.show() -------------------------------------------------------------------------------- /Advanced Learning Algorithms/W2/C2_W2_Relu.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Optional Lab - ReLU activation" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "from matplotlib.gridspec import GridSpec\n", 19 | "plt.style.use('./deeplearning.mplstyle')\n", 20 | "import tensorflow as tf\n", 21 | "from tensorflow.keras.models import Sequential\n", 22 | "from tensorflow.keras.layers import Dense, LeakyReLU\n", 23 | "from tensorflow.keras.activations import linear, relu, sigmoid\n", 24 | "%matplotlib widget\n", 25 | "from matplotlib.widgets import Slider\n", 26 | "from lab_utils_common import dlc\n", 27 | "from autils import plt_act_trio\n", 28 | "from lab_utils_relu import *\n", 29 | "import warnings\n", 30 | "warnings.simplefilter(action='ignore', category=UserWarning)\n" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "\n", 38 | "## 2 - ReLU Activation\n", 39 | "This week, a new activation was introduced, the Rectified Linear Unit (ReLU). \n", 40 | "$$ a = max(0,z) \\quad\\quad\\text {# ReLU function} $$" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "application/vnd.jupyter.widget-view+json": { 51 | "model_id": "078090c48783438ab1a719a0b088fed7", 52 | "version_major": 2, 53 | "version_minor": 0 54 | }, 55 | "text/plain": [ 56 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 57 | ] 58 | }, 59 | "metadata": {}, 60 | "output_type": "display_data" 61 | } 62 | ], 63 | "source": [ 64 | "plt_act_trio()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "\n", 72 | "The example from the lecture on the right shows an application of the ReLU. In this example, the derived \"awareness\" feature is not binary but has a continuous range of values. The sigmoid is best for on/off or binary situations. The ReLU provides a continuous linear relationship. Additionally it has an 'off' range where the output is zero. \n", 73 | "The \"off\" feature makes the ReLU a Non-Linear activation. Why is this needed? Let's examine this below. " 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "### Why Non-Linear Activations? \n", 81 | " The function shown is composed of linear pieces (piecewise linear). The slope is consistent during the linear portion and then changes abruptly at transition points. At transition points, a new linear function is added which, when added to the existing function, will produce the new slope. The new function is added at transition point but does not contribute to the output prior to that point. The non-linear activation function is responsible for disabling the input prior to and sometimes after the transition points. The following exercise provides a more tangible example." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "The exercise will use the network below in a regression problem where you must model a piecewise linear target :\n", 89 | " \n", 90 | "The network has 3 units in the first layer. Each is required to form the target. Unit 0 is pre-programmed and fixed to map the first segment. You will modify weights and biases in unit 1 and 2 to model the 2nd and 3rd segment. The output unit is also fixed and simply sums the outputs of the first layer. \n", 91 | "\n", 92 | "Using the sliders below, modify weights and bias to match the target. \n", 93 | "Hints: Start with `w1` and `b1` and leave `w2` and `b2` zero until you match the 2nd segment. Clicking rather than sliding is quicker. If you have trouble, don't worry, the text below will describe this in more detail." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 3, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "application/vnd.jupyter.widget-view+json": { 104 | "model_id": "03427ce487ce42ee95edf0b51535ff43", 105 | "version_major": 2, 106 | "version_minor": 0 107 | }, 108 | "text/plain": [ 109 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 110 | ] 111 | }, 112 | "metadata": {}, 113 | "output_type": "display_data" 114 | } 115 | ], 116 | "source": [ 117 | "_ = plt_relu_ex()" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | " \n", 125 | "The goal of this exercise is to appreciate how the ReLU's non-linear behavior provides the needed ability to turn functions off until they are needed. Let's see how this worked in this example.\n", 126 | " \n", 127 | "The plots on the right contain the output of the units in the first layer. \n", 128 | "Starting at the top, unit 0 is responsible for the first segment marked with a 1. Both the linear function $z$ and the function following the ReLU $a$ are shown. You can see that the ReLU cuts off the function after the interval [0,1]. This is important as it prevents Unit 0 from interfering with the following segment. \n", 129 | "\n", 130 | "Unit 1 is responsible for the 2nd segment. Here the ReLU kept this unit quiet until after x is 1. Since the first unit is not contributing, the slope for unit 1, $w^{[1]}_1$, is just the slope of the target line. The bias must be adjusted to keep the output negative until x has reached 1. Note how the contribution of Unit 1 extends to the 3rd segment as well.\n", 131 | "\n", 132 | "Unit 2 is responsible for the 3rd segment. The ReLU again zeros the output until x reaches the right value.The slope of the unit, $w^{[1]}_2$, must be set so that the sum of unit 1 and 2 have the desired slope. The bias is again adjusted to keep the output negative until x has reached 2. \n", 133 | "\n", 134 | "The \"off\" or disable feature of the ReLU activation enables models to stitch together linear segments to model complex non-linear functions.\n" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## Congratulations!\n", 142 | "You are now more familiar with the ReLU and the importance of its non-linear behavior." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Python 3", 156 | "language": "python", 157 | "name": "python3" 158 | }, 159 | "language_info": { 160 | "codemirror_mode": { 161 | "name": "ipython", 162 | "version": 3 163 | }, 164 | "file_extension": ".py", 165 | "mimetype": "text/x-python", 166 | "name": "python", 167 | "nbconvert_exporter": "python", 168 | "pygments_lexer": "ipython3", 169 | "version": "3.7.6" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 5 174 | } 175 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W3/C1_W3_Lab01_Classification_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Optional Lab: Classification\n", 8 | "\n", 9 | "In this lab, you will contrast regression and classification." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 4, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "%matplotlib widget\n", 20 | "import matplotlib.pyplot as plt\n", 21 | "from lab_utils_common import dlc, plot_data\n", 22 | "from plt_one_addpt_onclick import plt_one_addpt_onclick\n", 23 | "plt.style.use('./deeplearning.mplstyle')" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "\n", 31 | "## Classification Problems\n", 32 | " Examples of classification problems are things like: identifying email as Spam or Not Spam or determining if a tumor is malignant or benign. In particular, these are examples of *binary* classification where there are two possible outcomes. Outcomes can be described in pairs of 'positive'/'negative' such as 'yes'/'no, 'true'/'false' or '1'/'0'. \n", 33 | "\n", 34 | "Plots of classification data sets often use symbols to indicate the outcome of an example. In the plots below, 'X' is used to represent the positive values while 'O' represents negative outcomes. " 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 5, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "x_train = np.array([0., 1, 2, 3, 4, 5])\n", 44 | "y_train = np.array([0, 0, 0, 1, 1, 1])\n", 45 | "X_train2 = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])\n", 46 | "y_train2 = np.array([0, 0, 0, 1, 1, 1])" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 6, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "application/vnd.jupyter.widget-view+json": { 57 | "model_id": "3f96b08d23ca4d53ac5a34aa0d725b1e", 58 | "version_major": 2, 59 | "version_minor": 0 60 | }, 61 | "text/plain": [ 62 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 63 | ] 64 | }, 65 | "metadata": {}, 66 | "output_type": "display_data" 67 | } 68 | ], 69 | "source": [ 70 | "pos = y_train == 1\n", 71 | "neg = y_train == 0\n", 72 | "\n", 73 | "fig,ax = plt.subplots(1,2,figsize=(8,3))\n", 74 | "#plot 1, single variable\n", 75 | "ax[0].scatter(x_train[pos], y_train[pos], marker='x', s=80, c = 'red', label=\"y=1\")\n", 76 | "ax[0].scatter(x_train[neg], y_train[neg], marker='o', s=100, label=\"y=0\", facecolors='none', \n", 77 | " edgecolors=dlc[\"dlblue\"],lw=3)\n", 78 | "\n", 79 | "ax[0].set_ylim(-0.08,1.1)\n", 80 | "ax[0].set_ylabel('y', fontsize=12)\n", 81 | "ax[0].set_xlabel('x', fontsize=12)\n", 82 | "ax[0].set_title('one variable plot')\n", 83 | "ax[0].legend()\n", 84 | "\n", 85 | "#plot 2, two variables\n", 86 | "plot_data(X_train2, y_train2, ax[1])\n", 87 | "ax[1].axis([0, 4, 0, 4])\n", 88 | "ax[1].set_ylabel('$x_1$', fontsize=12)\n", 89 | "ax[1].set_xlabel('$x_0$', fontsize=12)\n", 90 | "ax[1].set_title('two variable plot')\n", 91 | "ax[1].legend()\n", 92 | "plt.tight_layout()\n", 93 | "plt.show()\n" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Note in the plots above:\n", 101 | "- In the single variable plot, positive results are shown both a red 'X's and as y=1. Negative results are blue 'O's and are located at y=0.\n", 102 | " - Recall in the case of linear regression, y would not have been limited to two values but could have been any value.\n", 103 | "- In the two-variable plot, the y axis is not available. Positive results are shown as red 'X's, while negative results use the blue 'O' symbol.\n", 104 | " - Recall in the case of linear regression with multiple variables, y would not have been limited to two values and a similar plot would have been three-dimensional." 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "## Linear Regression approach\n", 112 | "In the previous week, you applied linear regression to build a prediction model. Let's try that approach here using the simple example that was described in the lecture. The model will predict if a tumor is benign or malignant based on tumor size. Try the following:\n", 113 | "- Click on 'Run Linear Regression' to find the best linear regression model for the given data.\n", 114 | " - Note the resulting linear model does **not** match the data well. \n", 115 | "One option to improve the results is to apply a *threshold*. \n", 116 | "- Tick the box on the 'Toggle 0.5 threshold' to show the predictions if a threshold is applied.\n", 117 | " - These predictions look good, the predictions match the data\n", 118 | "- *Important*: Now, add further 'malignant' data points on the far right, in the large tumor size range (near 10), and re-run linear regression.\n", 119 | " - Now, the model predicts the larger tumor, but data point at x=3 is being incorrectly predicted!\n", 120 | "- to clear/renew the plot, rerun the cell containing the plot command." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "application/vnd.jupyter.widget-view+json": { 131 | "model_id": "734e19e3767d4186a1b89c0fa10925a9", 132 | "version_major": 2, 133 | "version_minor": 0 134 | }, 135 | "text/plain": [ 136 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 137 | ] 138 | }, 139 | "metadata": {}, 140 | "output_type": "display_data" 141 | } 142 | ], 143 | "source": [ 144 | "w_in = np.zeros((1))\n", 145 | "b_in = 0\n", 146 | "plt.close('all') \n", 147 | "addpt = plt_one_addpt_onclick( x_train,y_train, w_in, b_in, logistic=False)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "The example above demonstrates that the linear model is insufficient to model categorical data. The model can be extended as described in the following lab." 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "## Congratulations!\n", 162 | "In this lab you:\n", 163 | "- explored categorical data sets and plotting\n", 164 | "- determined that linear regression was insufficient for a classification problem." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.7.6" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 5 203 | } 204 | -------------------------------------------------------------------------------- /src/plt_logistic_loss.py: -------------------------------------------------------------------------------- 1 | """---------------------------------------------------------------- 2 | logistic_loss plotting routines and support 3 | """ 4 | 5 | from matplotlib import cm 6 | from lab_utils_common import sigmoid, dlblue, dlorange, np, plt, compute_cost_matrix 7 | 8 | def compute_cost_logistic_sq_err(X, y, w, b): 9 | """ 10 | compute sq error cost on logicist data (for negative example only, not used in practice) 11 | Args: 12 | X (ndarray): Shape (m,n) matrix of examples with multiple features 13 | w (ndarray): Shape (n) parameters for prediction 14 | b (scalar): parameter for prediction 15 | Returns: 16 | cost (scalar): cost 17 | """ 18 | m = X.shape[0] 19 | cost = 0.0 20 | for i in range(m): 21 | z_i = np.dot(X[i],w) + b 22 | f_wb_i = sigmoid(z_i) #add sigmoid to normal sq error cost for linear regression 23 | cost = cost + (f_wb_i - y[i])**2 24 | cost = cost / (2 * m) 25 | return np.squeeze(cost) 26 | 27 | def plt_logistic_squared_error(X,y): 28 | """ plots logistic squared error for demonstration """ 29 | wx, by = np.meshgrid(np.linspace(-6,12,50), 30 | np.linspace(10, -20, 40)) 31 | points = np.c_[wx.ravel(), by.ravel()] 32 | cost = np.zeros(points.shape[0]) 33 | 34 | for i in range(points.shape[0]): 35 | w,b = points[i] 36 | cost[i] = compute_cost_logistic_sq_err(X.reshape(-1,1), y, w, b) 37 | cost = cost.reshape(wx.shape) 38 | 39 | fig = plt.figure() 40 | fig.canvas.toolbar_visible = False 41 | fig.canvas.header_visible = False 42 | fig.canvas.footer_visible = False 43 | ax = fig.add_subplot(1, 1, 1, projection='3d') 44 | ax.plot_surface(wx, by, cost, alpha=0.6,cmap=cm.jet,) 45 | 46 | ax.set_xlabel('w', fontsize=16) 47 | ax.set_ylabel('b', fontsize=16) 48 | ax.set_zlabel("Cost", rotation=90, fontsize=16) 49 | ax.set_title('"Logistic" Squared Error Cost vs (w, b)') 50 | ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 51 | ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 52 | ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 53 | 54 | 55 | def plt_logistic_cost(X,y): 56 | """ plots logistic cost """ 57 | wx, by = np.meshgrid(np.linspace(-6,12,50), 58 | np.linspace(0, -20, 40)) 59 | points = np.c_[wx.ravel(), by.ravel()] 60 | cost = np.zeros(points.shape[0],dtype=np.longdouble) 61 | 62 | for i in range(points.shape[0]): 63 | w,b = points[i] 64 | cost[i] = compute_cost_matrix(X.reshape(-1,1), y, w, b, logistic=True, safe=True) 65 | cost = cost.reshape(wx.shape) 66 | 67 | fig = plt.figure(figsize=(9,5)) 68 | fig.canvas.toolbar_visible = False 69 | fig.canvas.header_visible = False 70 | fig.canvas.footer_visible = False 71 | ax = fig.add_subplot(1, 2, 1, projection='3d') 72 | ax.plot_surface(wx, by, cost, alpha=0.6,cmap=cm.jet,) 73 | 74 | ax.set_xlabel('w', fontsize=16) 75 | ax.set_ylabel('b', fontsize=16) 76 | ax.set_zlabel("Cost", rotation=90, fontsize=16) 77 | ax.set_title('Logistic Cost vs (w, b)') 78 | ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 79 | ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 80 | ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 81 | 82 | ax = fig.add_subplot(1, 2, 2, projection='3d') 83 | 84 | ax.plot_surface(wx, by, np.log(cost), alpha=0.6,cmap=cm.jet,) 85 | 86 | ax.set_xlabel('w', fontsize=16) 87 | ax.set_ylabel('b', fontsize=16) 88 | ax.set_zlabel('\nlog(Cost)', fontsize=16) 89 | ax.set_title('log(Logistic Cost) vs (w, b)') 90 | ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 91 | ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 92 | ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 93 | 94 | plt.show() 95 | return cost 96 | 97 | 98 | def soup_bowl(): 99 | """ creates 3D quadratic error surface """ 100 | #Create figure and plot with a 3D projection 101 | fig = plt.figure(figsize=(4,4)) 102 | fig.canvas.toolbar_visible = False 103 | fig.canvas.header_visible = False 104 | fig.canvas.footer_visible = False 105 | 106 | #Plot configuration 107 | ax = fig.add_subplot(111, projection='3d') 108 | ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 109 | ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 110 | ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 111 | ax.zaxis.set_rotate_label(False) 112 | ax.view_init(15, -120) 113 | 114 | #Useful linearspaces to give values to the parameters w and b 115 | w = np.linspace(-20, 20, 100) 116 | b = np.linspace(-20, 20, 100) 117 | 118 | #Get the z value for a bowl-shaped cost function 119 | z=np.zeros((len(w), len(b))) 120 | j=0 121 | for x in w: 122 | i=0 123 | for y in b: 124 | z[i,j] = x**2 + y**2 125 | i+=1 126 | j+=1 127 | 128 | #Meshgrid used for plotting 3D functions 129 | W, B = np.meshgrid(w, b) 130 | 131 | #Create the 3D surface plot of the bowl-shaped cost function 132 | ax.plot_surface(W, B, z, cmap = "Spectral_r", alpha=0.7, antialiased=False) 133 | ax.plot_wireframe(W, B, z, color='k', alpha=0.1) 134 | ax.set_xlabel("$w$") 135 | ax.set_ylabel("$b$") 136 | ax.set_zlabel("Cost", rotation=90) 137 | ax.set_title("Squared Error Cost used in Linear Regression") 138 | 139 | plt.show() 140 | 141 | 142 | def plt_simple_example(x, y): 143 | """ plots tumor data """ 144 | pos = y == 1 145 | neg = y == 0 146 | 147 | fig,ax = plt.subplots(1,1,figsize=(5,3)) 148 | fig.canvas.toolbar_visible = False 149 | fig.canvas.header_visible = False 150 | fig.canvas.footer_visible = False 151 | 152 | ax.scatter(x[pos], y[pos], marker='x', s=80, c = 'red', label="malignant") 153 | ax.scatter(x[neg], y[neg], marker='o', s=100, label="benign", facecolors='none', edgecolors=dlblue,lw=3) 154 | ax.set_ylim(-0.075,1.1) 155 | ax.set_ylabel('y') 156 | ax.set_xlabel('Tumor Size') 157 | ax.legend(loc='lower right') 158 | ax.set_title("Example of Logistic Regression on Categorical Data") 159 | 160 | 161 | def plt_two_logistic_loss_curves(): 162 | """ plots the logistic loss """ 163 | fig,ax = plt.subplots(1,2,figsize=(6,3),sharey=True) 164 | fig.canvas.toolbar_visible = False 165 | fig.canvas.header_visible = False 166 | fig.canvas.footer_visible = False 167 | x = np.linspace(0.01,1-0.01,20) 168 | ax[0].plot(x,-np.log(x)) 169 | #ax[0].set_title("y = 1") 170 | ax[0].text(0.5, 4.0, "y = 1", fontsize=12) 171 | ax[0].set_ylabel("loss") 172 | ax[0].set_xlabel(r"$f_{w,b}(x)$") 173 | ax[1].plot(x,-np.log(1-x)) 174 | #ax[1].set_title("y = 0") 175 | ax[1].text(0.5, 4.0, "y = 0", fontsize=12) 176 | ax[1].set_xlabel(r"$f_{w,b}(x)$") 177 | ax[0].annotate("prediction \nmatches \ntarget ", xy= [1,0], xycoords='data', 178 | xytext=[-10,30],textcoords='offset points', ha="right", va="center", 179 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 180 | ax[0].annotate("loss increases as prediction\n differs from target", xy= [0.1,-np.log(0.1)], xycoords='data', 181 | xytext=[10,30],textcoords='offset points', ha="left", va="center", 182 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 183 | ax[1].annotate("prediction \nmatches \ntarget ", xy= [0,0], xycoords='data', 184 | xytext=[10,30],textcoords='offset points', ha="left", va="center", 185 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 186 | ax[1].annotate("loss increases as prediction\n differs from target", xy= [0.9,-np.log(1-0.9)], xycoords='data', 187 | xytext=[-10,30],textcoords='offset points', ha="right", va="center", 188 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 189 | plt.suptitle("Loss Curves for Two Categorical Target Values", fontsize=12) 190 | plt.tight_layout() 191 | plt.show() 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Screenshot 2022-07-28 at 13 17 34 2 | 3 | ------------------------------------------------------------------------------------------- 4 | ![GitHub stars](https://img.shields.io/github/stars/quocviethere/Coursera-Machine-Learning-Specialization.svg?style=social&label=Stars&maxAge=2592000) 5 | ![GitHub forks](https://img.shields.io/github/forks/quocviethere/Coursera-Machine-Learning-Specialization.svg?style=social&label=Forks&maxAge=2592000) 6 | 7 | # [Machine Learning Specialization](https://www.coursera.org/specializations/machine-learning-introduction?#instructors) 8 | *This Repository Contains Solution to the Assignments of the Machine Learning Specialization from deeplearning.ai on Coursera Taught by 9 | [Andrew Ng](https://www.coursera.org/instructor/andrewng) 10 | 11 | ### Disclaimer 12 | 13 | #### Anyone looking to enter the field of artificial intelligence for the first time can check out the Machine Learning Specialization offered by Coursera. You should only use the programming assignments placed in this repository as a resource and to get you out of a jam. Do not simply copy and paste the code if you are a participant in this course; doing so will not help you. Please watch every lecture video, finish the quizzes and code assignments yourself as it is comparatively simple to understand. 14 | ------------------------------------------------------------------------------------------- 15 | 16 | ## WHAT YOU WILL LEARN 17 | 18 | - Build ML models with NumPy & scikit-learn, build & train supervised models for prediction & binary classification tasks (linear, logistic regression) 19 | - Build & train a neural network with TensorFlow to perform multi-class classification, & build & use decision trees & tree ensemble methods 20 | - Apply best practices for ML development & use unsupervised learning techniques for unsupervised learning including clustering & anomaly detection 21 | - Build recommender systems with a collaborative filtering approach & a content-based deep learning method & build a deep reinforcement learning model 22 | 23 | --- 24 | 25 | ## About this Specialization 26 | 27 | The Machine Learning Specialization is a foundational online program created in collaboration between DeepLearning.AI and Stanford Online. This beginner-friendly program will teach you the fundamentals of machine learning and how to use these techniques to build real-world AI applications. 28 | 29 | This Specialization is taught by Andrew Ng, an AI visionary who has led critical research at Stanford University and groundbreaking work at Google Brain, Baidu, and Landing.AI to advance the AI field. 30 | 31 | This 3-course Specialization is an updated version of Andrew’s pioneering Machine Learning course, rated 4.9 out of 5 and taken by over 4.8 million learners since it launched in 2012. 32 | 33 | It provides a broad introduction to modern machine learning, including supervised learning (multiple linear regression, logistic regression, neural networks, and decision trees), unsupervised learning (clustering, dimensionality reduction, recommender systems), and some of the best practices used in Silicon Valley for artificial intelligence and machine learning innovation (evaluating and tuning models, taking a data-centric approach to improving performance, and more.) 34 | 35 | By the end of this Specialization, you will have mastered key concepts and gained the practical know-how to quickly and powerfully apply machine learning to challenging real-world problems. If you’re looking to break into AI or build a career in machine learning, the new Machine Learning Specialization is the best place to start. 36 | 37 | --- 38 | 39 | ## Applied Learning Project 40 | By the end of this Specialization, you will be ready to: 41 | 42 | -Build machine learning models in Python using popular machine learning libraries NumPy and scikit-learn. 43 | - Build and train supervised machine learning models for prediction and binary classification tasks, including linear regression and logistic regression. 44 | - Build and train a neural network with TensorFlow to perform multi-class classification. 45 | - Apply best practices for machine learning development so that your models generalize to data and tasks in the real world. 46 | - Build and use decision trees and tree ensemble methods, including random forests and boosted trees. 47 | - Use unsupervised learning techniques for unsupervised learning: including clustering and anomaly detection. 48 | - Build recommender systems with a collaborative filtering approach and a content-based deep learning method. 49 | - Build a deep reinforcement learning model. 50 | 51 | ------------------------------------------------------------------------------------------------------------- 52 | 53 | ## There are 3 Courses in this Specialization 54 | 55 | ### [COURSE 1 - Supervised Machine Learning: Regression and Classification](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/tree/main/Supervised%20Machine%20Learning:%20Regression%20and%20Classification) 56 | 57 | In the first course of the Machine Learning Specialization, you will: 58 | - Build machine learning models in Python using popular machine learning libraries NumPy and scikit-learn. 59 | - Build and train supervised machine learning models for prediction and binary classification tasks, including linear regression and logistic regression 60 | 61 | ### [COURSE 2 - Advanced Learning Algorithms](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/tree/main/Advanced%20Learning%20Algorithms) 62 | 63 | In the second course of the Machine Learning Specialization, you will: 64 | - Build and train a neural network with TensorFlow to perform multi-class classification 65 | - Apply best practices for machine learning development so that your models generalize to data and tasks in the real world 66 | - Build and use decision trees and tree ensemble methods, including random forests and boosted trees 67 | 68 | ### [COURSE 3 - Unsupervised Learning, Recommenders, Reinforcement Learning](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/tree/main/Unsupervised%20Learning%2C%20Recommenders%2C%20Reinforcement%20Learning) 69 | In the third course of the Machine Learning Specialization, you will: 70 | - Use unsupervised learning techniques for unsupervised learning: including clustering and anomaly detection. 71 | - Build recommender systems with a collaborative filtering approach and a content-based deep learning method. 72 | - Build a deep reinforcement learning model. 73 | 74 | ------------------------------------------------------------------------------------------------------------- 75 | 76 | ## Certificate 77 | 78 | 1. [Machine Learning Specialization](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/blob/main/Certificates/Machine%20Learning.pdf) 79 | 2. [Supervised Machine Learning: Regression and Classification](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/blob/main/Certificates/Supervised%20Machine%20Learning-%20Regression%20and%20Classification.pdf) 80 | 3. [Advanced Learning Algorithms](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/blob/main/Certificates/Advanced%20Learning%20Algorithms.pdf) 81 | 4. [Unsupervised Learning, Recommenders, Reinforcement Learning](https://github.com/quocviethere/Coursera-Machine-Learning-Specialization/blob/main/Certificates/Unsupervised%20Learning%2C%20Recommenders%2C%20Reinforcement%20Learning.pdf) 82 | 83 | 84 | -------------------------------------------------------------------------------------------------------------- 85 | 86 | ## References 87 | 1. [Supervised Machine Learning: Regression and Classification](https://www.coursera.org/learn/machine-learning?specialization=machine-learning-introduction) 88 | 2. [Advanced Learning Algorithms](https://www.coursera.org/learn/advanced-learning-algorithms?specialization=machine-learning-introduction) 89 | 3. [Unsupervised Learning, Recommenders, Reinforcement Learning](https://www.coursera.org/learn/unsupervised-learning-recommenders-reinforcement-learning?specialization=machine-learning-introduction) 90 | 91 | -------------------------------------------------------------------------------- /src/public_tests_a1.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.keras.models import Sequential 3 | from tensorflow.keras.layers import Dense 4 | from tensorflow.keras.activations import relu,linear 5 | from tensorflow.keras.losses import SparseCategoricalCrossentropy 6 | from tensorflow.keras.optimizers import Adam 7 | 8 | import numpy as np 9 | 10 | def test_eval_mse(target): 11 | y_hat = np.array([2.4, 4.2]) 12 | y_tmp = np.array([2.3, 4.1]) 13 | result = target(y_hat, y_tmp) 14 | 15 | assert np.isclose(result, 0.005, atol=1e-6), f"Wrong value. Expected 0.005, got {result}" 16 | 17 | y_hat = np.array([3.] * 10) 18 | y_tmp = np.array([3.] * 10) 19 | result = target(y_hat, y_tmp) 20 | assert np.isclose(result, 0.), f"Wrong value. Expected 0.0 when y_hat == t_tmp, but got {result}" 21 | 22 | y_hat = np.array([3.]) 23 | y_tmp = np.array([0.]) 24 | result = target(y_hat, y_tmp) 25 | assert np.isclose(result, 4.5), f"Wrong value. Expected 4.5, but got {result}. Remember the square termn" 26 | 27 | y_hat = np.array([3.] * 5) 28 | y_tmp = np.array([2.] * 5) 29 | result = target(y_hat, y_tmp) 30 | assert np.isclose(result, 0.5), f"Wrong value. Expected 0.5, but got {result}. Remember to divide by (2*m)" 31 | 32 | print("\033[92m All tests passed.") 33 | 34 | def test_eval_cat_err(target): 35 | y_hat = np.array([1, 0, 1, 1, 1, 0]) 36 | y_tmp = np.array([0, 1, 0, 0, 0, 1]) 37 | result = target(y_hat, y_tmp) 38 | assert not np.isclose(result, 6.), f"Wrong value. Expected 1, but got {result}. Did you divided by m?" 39 | 40 | y_hat = np.array([1, 2, 0]) 41 | y_tmp = np.array([1, 2, 3]) 42 | result = target(y_hat, y_tmp) 43 | assert np.isclose(result, 1./3., atol=1e-6), f"Wrong value. Expected 0.333, but got {result}" 44 | 45 | y_hat = np.array([1, 0, 1, 1, 1, 0]) 46 | y_tmp = np.array([1, 1, 1, 0, 0, 0]) 47 | result = target(y_hat, y_tmp) 48 | assert np.isclose(result, 3./6., atol=1e-6), f"Wrong value. Expected 0.5, but got {result}" 49 | 50 | y_hat = np.array([[1], [2], [0], [3]]) 51 | y_tmp = np.array([[1], [2], [1], [3]]) 52 | res_tmp = target(y_hat, y_tmp) 53 | assert type(res_tmp) != np.ndarray, f"The output must be an scalar but got {type(res_tmp)}" 54 | 55 | print("\033[92m All tests passed.") 56 | 57 | def model_test(target, classes, input_size): 58 | target.build(input_shape=(None,input_size)) 59 | expected_lr = 0.01 60 | 61 | assert len(target.layers) == 3, \ 62 | f"Wrong number of layers. Expected 3 but got {len(target.layers)}" 63 | assert target.input.shape.as_list() == [None, input_size], \ 64 | f"Wrong input shape. Expected [None, {input_size}] but got {target.input.shape.as_list()}" 65 | i = 0 66 | expected = [[Dense, [None, 120], relu], 67 | [Dense, [None, 40], relu], 68 | [Dense, [None, classes], linear]] 69 | 70 | for layer in target.layers: 71 | assert type(layer) == expected[i][0], \ 72 | f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}" 73 | assert layer.output.shape.as_list() == expected[i][1], \ 74 | f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}" 75 | assert layer.activation == expected[i][2], \ 76 | f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}" 77 | assert layer.kernel_regularizer == None, "You must not specify any regularizer for any layer" 78 | i = i + 1 79 | 80 | assert type(target.loss)==SparseCategoricalCrossentropy, f"Wrong loss function. Expected {SparseCategoricalCrossentropy}, but got {target.loss}" 81 | assert type(target.optimizer)==Adam, f"Wrong loss function. Expected {Adam}, but got {target.optimizer}" 82 | lr = target.optimizer.learning_rate.numpy() 83 | assert np.isclose(lr, expected_lr, atol=1e-8), f"Wrong learning rate. Expected {expected_lr}, but got {lr}" 84 | assert target.loss.get_config()['from_logits'], f"Set from_logits=True in loss function" 85 | 86 | print("\033[92mAll tests passed!") 87 | 88 | def model_s_test(target, classes, input_size): 89 | target.build(input_shape=(None,input_size)) 90 | expected_lr = 0.01 91 | 92 | assert len(target.layers) == 2, \ 93 | f"Wrong number of layers. Expected 3 but got {len(target.layers)}" 94 | assert target.input.shape.as_list() == [None, input_size], \ 95 | f"Wrong input shape. Expected [None, {input_size}] but got {target.input.shape.as_list()}" 96 | i = 0 97 | expected = [[Dense, [None, 6], relu], 98 | [Dense, [None, classes], linear]] 99 | 100 | for layer in target.layers: 101 | assert type(layer) == expected[i][0], \ 102 | f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}" 103 | assert layer.output.shape.as_list() == expected[i][1], \ 104 | f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}" 105 | assert layer.activation == expected[i][2], \ 106 | f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}" 107 | assert layer.kernel_regularizer == None, "You must not specify any regularizer any layer" 108 | i = i + 1 109 | 110 | assert type(target.loss)==SparseCategoricalCrossentropy, f"Wrong loss function. Expected {SparseCategoricalCrossentropy}, but got {target.loss}" 111 | assert type(target.optimizer)==Adam, f"Wrong loss function. Expected {Adam}, but got {target.optimizer}" 112 | lr = target.optimizer.learning_rate.numpy() 113 | assert np.isclose(lr, expected_lr, atol=1e-8), f"Wrong learning rate. Expected {expected_lr}, but got {lr}" 114 | assert target.loss.get_config()['from_logits'], f"Set from_logits=True in loss function" 115 | 116 | print("\033[92mAll tests passed!") 117 | 118 | def model_r_test(target, classes, input_size): 119 | target.build(input_shape=(None,input_size)) 120 | expected_lr = 0.01 121 | print("ddd") 122 | assert len(target.layers) == 3, \ 123 | f"Wrong number of layers. Expected 3 but got {len(target.layers)}" 124 | assert target.input.shape.as_list() == [None, input_size], \ 125 | f"Wrong input shape. Expected [None, {input_size}] but got {target.input.shape.as_list()}" 126 | i = 0 127 | expected = [[Dense, [None, 120], relu, (tf.keras.regularizers.l2, 0.1)], 128 | [Dense, [None, 40], relu, (tf.keras.regularizers.l2, 0.1)], 129 | [Dense, [None, classes], linear, None]] 130 | 131 | for layer in target.layers: 132 | assert type(layer) == expected[i][0], \ 133 | f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}" 134 | assert layer.output.shape.as_list() == expected[i][1], \ 135 | f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}" 136 | assert layer.activation == expected[i][2], \ 137 | f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}" 138 | if not (expected[i][3] == None): 139 | assert type(layer.kernel_regularizer) == expected[i][3][0], f"Wrong regularizer. Expected L2 regularizer but got {type(layer.kernel_regularizer)}" 140 | assert np.isclose(layer.kernel_regularizer.l2, expected[i][3][1]), f"Wrong regularization factor. Expected {expected[i][3][1]}, but got {layer.kernel_regularizer.l2}" 141 | else: 142 | assert layer.kernel_regularizer == None, "You must not specify any regularizer for the 3th layer" 143 | i = i + 1 144 | 145 | assert type(target.loss)==SparseCategoricalCrossentropy, f"Wrong loss function. Expected {SparseCategoricalCrossentropy}, but got {target.loss}" 146 | assert type(target.optimizer)==Adam, f"Wrong loss function. Expected {Adam}, but got {target.optimizer}" 147 | lr = target.optimizer.learning_rate.numpy() 148 | assert np.isclose(lr, expected_lr, atol=1e-8), f"Wrong learning rate. Expected {expected_lr}, but got {lr}" 149 | assert target.loss.get_config()['from_logits'], f"Set from_logits=True in loss function" 150 | 151 | print("\033[92mAll tests passed!") 152 | -------------------------------------------------------------------------------- /src/plt_one_addpt_onclick.py: -------------------------------------------------------------------------------- 1 | import time 2 | import copy 3 | from ipywidgets import Output 4 | from matplotlib.widgets import Button, CheckButtons 5 | from matplotlib.patches import FancyArrowPatch 6 | from lab_utils_common import np, plt, dlblue, dlorange, sigmoid, dldarkred, gradient_descent 7 | 8 | # for debug 9 | #output = Output() # sends hidden error messages to display when using widgets 10 | #display(output) 11 | 12 | class plt_one_addpt_onclick: 13 | """ class to run one interactive plot """ 14 | def __init__(self, x, y, w, b, logistic=True): 15 | self.logistic=logistic 16 | pos = y == 1 17 | neg = y == 0 18 | 19 | fig,ax = plt.subplots(1,1,figsize=(8,4)) 20 | fig.canvas.toolbar_visible = False 21 | fig.canvas.header_visible = False 22 | fig.canvas.footer_visible = False 23 | 24 | plt.subplots_adjust(bottom=0.25) 25 | ax.scatter(x[pos], y[pos], marker='x', s=80, c = 'red', label="malignant") 26 | ax.scatter(x[neg], y[neg], marker='o', s=100, label="benign", facecolors='none', edgecolors=dlblue,lw=3) 27 | ax.set_ylim(-0.05,1.1) 28 | xlim = ax.get_xlim() 29 | ax.set_xlim(xlim[0],xlim[1]*2) 30 | ax.set_ylabel('y') 31 | ax.set_xlabel('Tumor Size') 32 | self.alegend = ax.legend(loc='lower right') 33 | if self.logistic: 34 | ax.set_title("Example of Logistic Regression on Categorical Data") 35 | else: 36 | ax.set_title("Example of Linear Regression on Categorical Data") 37 | 38 | ax.text(0.65,0.8,"[Click to add data points]", size=10, transform=ax.transAxes) 39 | 40 | axcalc = plt.axes([0.1, 0.05, 0.38, 0.075]) #l,b,w,h 41 | axthresh = plt.axes([0.5, 0.05, 0.38, 0.075]) #l,b,w,h 42 | self.tlist = [] 43 | 44 | self.fig = fig 45 | self.ax = [ax,axcalc,axthresh] 46 | self.x = x 47 | self.y = y 48 | self.w = copy.deepcopy(w) 49 | self.b = b 50 | f_wb = np.matmul(self.x.reshape(-1,1), self.w) + self.b 51 | if self.logistic: 52 | self.aline = self.ax[0].plot(self.x, sigmoid(f_wb), color=dlblue) 53 | self.bline = self.ax[0].plot(self.x, f_wb, color=dlorange,lw=1) 54 | else: 55 | self.aline = self.ax[0].plot(self.x, sigmoid(f_wb), color=dlblue) 56 | 57 | self.cid = fig.canvas.mpl_connect('button_press_event', self.add_data) 58 | if self.logistic: 59 | self.bcalc = Button(axcalc, 'Run Logistic Regression (click)', color=dlblue) 60 | self.bcalc.on_clicked(self.calc_logistic) 61 | else: 62 | self.bcalc = Button(axcalc, 'Run Linear Regression (click)', color=dlblue) 63 | self.bcalc.on_clicked(self.calc_linear) 64 | self.bthresh = CheckButtons(axthresh, ('Toggle 0.5 threshold (after regression)',)) 65 | self.bthresh.on_clicked(self.thresh) 66 | self.resize_sq(self.bthresh) 67 | 68 | # @output.capture() # debug 69 | def add_data(self, event): 70 | #self.ax[0].text(0.1,0.1, f"in onclick") 71 | if event.inaxes == self.ax[0]: 72 | x_coord = event.xdata 73 | y_coord = event.ydata 74 | 75 | if y_coord > 0.5: 76 | self.ax[0].scatter(x_coord, 1, marker='x', s=80, c = 'red' ) 77 | self.y = np.append(self.y,1) 78 | else: 79 | self.ax[0].scatter(x_coord, 0, marker='o', s=100, facecolors='none', edgecolors=dlblue,lw=3) 80 | self.y = np.append(self.y,0) 81 | self.x = np.append(self.x,x_coord) 82 | self.fig.canvas.draw() 83 | 84 | # @output.capture() # debug 85 | def calc_linear(self, event): 86 | if self.bthresh.get_status()[0]: 87 | self.remove_thresh() 88 | for it in [1,1,1,1,1,2,4,8,16,32,64,128,256]: 89 | self.w, self.b, _ = gradient_descent(self.x.reshape(-1,1), self.y.reshape(-1,1), 90 | self.w.reshape(-1,1), self.b, 0.01, it, 91 | logistic=False, lambda_=0, verbose=False) 92 | self.aline[0].remove() 93 | self.alegend.remove() 94 | y_hat = np.matmul(self.x.reshape(-1,1), self.w) + self.b 95 | self.aline = self.ax[0].plot(self.x, y_hat, color=dlblue, 96 | label=f"y = {np.squeeze(self.w):0.2f}x+({self.b:0.2f})") 97 | self.alegend = self.ax[0].legend(loc='lower right') 98 | time.sleep(0.3) 99 | self.fig.canvas.draw() 100 | if self.bthresh.get_status()[0]: 101 | self.draw_thresh() 102 | self.fig.canvas.draw() 103 | 104 | def calc_logistic(self, event): 105 | if self.bthresh.get_status()[0]: 106 | self.remove_thresh() 107 | for it in [1, 8,16,32,64,128,256,512,1024,2048,4096]: 108 | self.w, self.b, _ = gradient_descent(self.x.reshape(-1,1), self.y.reshape(-1,1), 109 | self.w.reshape(-1,1), self.b, 0.1, it, 110 | logistic=True, lambda_=0, verbose=False) 111 | self.aline[0].remove() 112 | self.bline[0].remove() 113 | self.alegend.remove() 114 | xlim = self.ax[0].get_xlim() 115 | x_hat = np.linspace(*xlim, 30) 116 | y_hat = sigmoid(np.matmul(x_hat.reshape(-1,1), self.w) + self.b) 117 | self.aline = self.ax[0].plot(x_hat, y_hat, color=dlblue, 118 | label="y = sigmoid(z)") 119 | f_wb = np.matmul(x_hat.reshape(-1,1), self.w) + self.b 120 | self.bline = self.ax[0].plot(x_hat, f_wb, color=dlorange, lw=1, 121 | label=f"z = {np.squeeze(self.w):0.2f}x+({self.b:0.2f})") 122 | self.alegend = self.ax[0].legend(loc='lower right') 123 | time.sleep(0.3) 124 | self.fig.canvas.draw() 125 | if self.bthresh.get_status()[0]: 126 | self.draw_thresh() 127 | self.fig.canvas.draw() 128 | 129 | 130 | def thresh(self, event): 131 | if self.bthresh.get_status()[0]: 132 | #plt.figtext(0,0, f"in thresh {self.bthresh.get_status()}") 133 | self.draw_thresh() 134 | else: 135 | #plt.figtext(0,0.3, f"in thresh {self.bthresh.get_status()}") 136 | self.remove_thresh() 137 | 138 | def draw_thresh(self): 139 | ws = np.squeeze(self.w) 140 | xp5 = -self.b/ws if self.logistic else (0.5 - self.b) / ws 141 | ylim = self.ax[0].get_ylim() 142 | xlim = self.ax[0].get_xlim() 143 | a = self.ax[0].fill_between([xlim[0], xp5], [ylim[1], ylim[1]], alpha=0.2, color=dlblue) 144 | b = self.ax[0].fill_between([xp5, xlim[1]], [ylim[1], ylim[1]], alpha=0.2, color=dldarkred) 145 | c = self.ax[0].annotate("Malignant", xy= [xp5,0.5], xycoords='data', 146 | xytext=[30,5],textcoords='offset points') 147 | d = FancyArrowPatch( 148 | posA=(xp5, 0.5), posB=(xp5+1.5, 0.5), color=dldarkred, 149 | arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0', 150 | ) 151 | self.ax[0].add_artist(d) 152 | 153 | e = self.ax[0].annotate("Benign", xy= [xp5,0.5], xycoords='data', 154 | xytext=[-70,5],textcoords='offset points', ha='left') 155 | f = FancyArrowPatch( 156 | posA=(xp5, 0.5), posB=(xp5-1.5, 0.5), color=dlblue, 157 | arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0', 158 | ) 159 | self.ax[0].add_artist(f) 160 | self.tlist = [a,b,c,d,e,f] 161 | 162 | self.fig.canvas.draw() 163 | 164 | def remove_thresh(self): 165 | #plt.figtext(0.5,0.0, f"rem thresh {self.bthresh.get_status()}") 166 | for artist in self.tlist: 167 | artist.remove() 168 | self.fig.canvas.draw() 169 | 170 | def resize_sq(self, bcid): 171 | """ resizes the check box """ 172 | #future reference 173 | #print(f"width : {bcid.rectangles[0].get_width()}") 174 | #print(f"height : {bcid.rectangles[0].get_height()}") 175 | #print(f"xy : {bcid.rectangles[0].get_xy()}") 176 | #print(f"bb : {bcid.rectangles[0].get_bbox()}") 177 | #print(f"points : {bcid.rectangles[0].get_bbox().get_points()}") #[[xmin,ymin],[xmax,ymax]] 178 | 179 | h = bcid.rectangles[0].get_height() 180 | bcid.rectangles[0].set_height(3*h) 181 | 182 | ymax = bcid.rectangles[0].get_bbox().y1 183 | ymin = bcid.rectangles[0].get_bbox().y0 184 | 185 | bcid.lines[0][0].set_ydata([ymax,ymin]) 186 | bcid.lines[0][1].set_ydata([ymin,ymax]) 187 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W3/C1_W3_Lab03_Decision_Boundary_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Optional Lab: Logistic Regression, Decision Boundary\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Goals\n", 15 | "In this lab, you will:\n", 16 | "- Plot the decision boundary for a logistic regression model. This will give you a better sense of what the model is predicting.\n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "import numpy as np\n", 26 | "%matplotlib widget\n", 27 | "import matplotlib.pyplot as plt\n", 28 | "from lab_utils_common import plot_data, sigmoid, draw_vthresh\n", 29 | "plt.style.use('./deeplearning.mplstyle')" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## Dataset\n", 37 | "\n", 38 | "Let's suppose you have following training dataset\n", 39 | "- The input variable `X` is a numpy array which has 6 training examples, each with two features\n", 40 | "- The output variable `y` is also a numpy array with 6 examples, and `y` is either `0` or `1`" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "X = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])\n", 50 | "y = np.array([0, 0, 0, 1, 1, 1]).reshape(-1,1) " 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "### Plot data \n", 58 | "\n", 59 | "Let's use a helper function to plot this data. The data points with label $y=1$ are shown as red crosses, while the data points with label $y=0$ are shown as blue circles. " 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "application/vnd.jupyter.widget-view+json": { 70 | "model_id": "b9bf4f8993884d78ab627021737da678", 71 | "version_major": 2, 72 | "version_minor": 0 73 | }, 74 | "text/plain": [ 75 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 76 | ] 77 | }, 78 | "metadata": {}, 79 | "output_type": "display_data" 80 | } 81 | ], 82 | "source": [ 83 | "fig,ax = plt.subplots(1,1,figsize=(4,4))\n", 84 | "plot_data(X, y, ax)\n", 85 | "\n", 86 | "ax.axis([0, 4, 0, 3.5])\n", 87 | "ax.set_ylabel('$x_1$')\n", 88 | "ax.set_xlabel('$x_0$')\n", 89 | "plt.show()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "## Logistic regression model\n", 97 | "\n", 98 | "\n", 99 | "* Suppose you'd like to train a logistic regression model on this data which has the form \n", 100 | "\n", 101 | " $f(x) = g(w_0x_0+w_1x_1 + b)$\n", 102 | " \n", 103 | " where $g(z) = \\frac{1}{1+e^{-z}}$, which is the sigmoid function\n", 104 | "\n", 105 | "\n", 106 | "* Let's say that you trained the model and get the parameters as $b = -3, w_0 = 1, w_1 = 1$. That is,\n", 107 | "\n", 108 | " $f(x) = g(x_0+x_1-3)$\n", 109 | "\n", 110 | " (You'll learn how to fit these parameters to the data further in the course)\n", 111 | " \n", 112 | " \n", 113 | "Let's try to understand what this trained model is predicting by plotting its decision boundary" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "### Refresher on logistic regression and decision boundary\n", 121 | "\n", 122 | "* Recall that for logistic regression, the model is represented as \n", 123 | "\n", 124 | " $$f_{\\mathbf{w},b}(\\mathbf{x}^{(i)}) = g(\\mathbf{w} \\cdot \\mathbf{x}^{(i)} + b) \\tag{1}$$\n", 125 | "\n", 126 | " where $g(z)$ is known as the sigmoid function and it maps all input values to values between 0 and 1:\n", 127 | "\n", 128 | " $g(z) = \\frac{1}{1+e^{-z}}\\tag{2}$\n", 129 | " and $\\mathbf{w} \\cdot \\mathbf{x}$ is the vector dot product:\n", 130 | " \n", 131 | " $$\\mathbf{w} \\cdot \\mathbf{x} = w_0 x_0 + w_1 x_1$$\n", 132 | " \n", 133 | " \n", 134 | " * We interpret the output of the model ($f_{\\mathbf{w},b}(x)$) as the probability that $y=1$ given $\\mathbf{x}$ and parameterized by $\\mathbf{w}$ and $b$.\n", 135 | "* Therefore, to get a final prediction ($y=0$ or $y=1$) from the logistic regression model, we can use the following heuristic -\n", 136 | "\n", 137 | " if $f_{\\mathbf{w},b}(x) >= 0.5$, predict $y=1$\n", 138 | " \n", 139 | " if $f_{\\mathbf{w},b}(x) < 0.5$, predict $y=0$\n", 140 | " \n", 141 | " \n", 142 | "* Let's plot the sigmoid function to see where $g(z) >= 0.5$" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 4, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "application/vnd.jupyter.widget-view+json": { 153 | "model_id": "d894c7c4318345fb81bfbfe338a9907d", 154 | "version_major": 2, 155 | "version_minor": 0 156 | }, 157 | "text/plain": [ 158 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 159 | ] 160 | }, 161 | "metadata": {}, 162 | "output_type": "display_data" 163 | } 164 | ], 165 | "source": [ 166 | "# Plot sigmoid(z) over a range of values from -10 to 10\n", 167 | "z = np.arange(-10,11)\n", 168 | "\n", 169 | "fig,ax = plt.subplots(1,1,figsize=(5,3))\n", 170 | "# Plot z vs sigmoid(z)\n", 171 | "ax.plot(z, sigmoid(z), c=\"b\")\n", 172 | "\n", 173 | "ax.set_title(\"Sigmoid function\")\n", 174 | "ax.set_ylabel('sigmoid(z)')\n", 175 | "ax.set_xlabel('z')\n", 176 | "draw_vthresh(ax,0)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "* As you can see, $g(z) >= 0.5$ for $z >=0$\n", 184 | "\n", 185 | "* For a logistic regression model, $z = \\mathbf{w} \\cdot \\mathbf{x} + b$. Therefore,\n", 186 | "\n", 187 | " if $\\mathbf{w} \\cdot \\mathbf{x} + b >= 0$, the model predicts $y=1$\n", 188 | " \n", 189 | " if $\\mathbf{w} \\cdot \\mathbf{x} + b < 0$, the model predicts $y=0$\n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | "### Plotting decision boundary\n", 194 | "\n", 195 | "Now, let's go back to our example to understand how the logistic regression model is making predictions.\n", 196 | "\n", 197 | "* Our logistic regression model has the form\n", 198 | "\n", 199 | " $f(\\mathbf{x}) = g(-3 + x_0+x_1)$\n", 200 | "\n", 201 | "\n", 202 | "* From what you've learnt above, you can see that this model predicts $y=1$ if $-3 + x_0+x_1 >= 0$\n", 203 | "\n", 204 | "Let's see what this looks like graphically. We'll start by plotting $-3 + x_0+x_1 = 0$, which is equivalent to $x_1 = 3 - x_0$.\n" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 5, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "data": { 214 | "application/vnd.jupyter.widget-view+json": { 215 | "model_id": "04ad4fb5c5fc4512a0d53122cd222349", 216 | "version_major": 2, 217 | "version_minor": 0 218 | }, 219 | "text/plain": [ 220 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 221 | ] 222 | }, 223 | "metadata": {}, 224 | "output_type": "display_data" 225 | } 226 | ], 227 | "source": [ 228 | "# Choose values between 0 and 6\n", 229 | "x0 = np.arange(0,6)\n", 230 | "\n", 231 | "x1 = 3 - x0\n", 232 | "fig,ax = plt.subplots(1,1,figsize=(5,4))\n", 233 | "# Plot the decision boundary\n", 234 | "ax.plot(x0,x1, c=\"b\")\n", 235 | "ax.axis([0, 4, 0, 3.5])\n", 236 | "\n", 237 | "# Fill the region below the line\n", 238 | "ax.fill_between(x0,x1, alpha=0.2)\n", 239 | "\n", 240 | "# Plot the original data\n", 241 | "plot_data(X,y,ax)\n", 242 | "ax.set_ylabel(r'$x_1$')\n", 243 | "ax.set_xlabel(r'$x_0$')\n", 244 | "plt.show()" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "* In the plot above, the blue line represents the line $x_0 + x_1 - 3 = 0$ and it should intersect the x1 axis at 3 (if we set $x_1$ = 3, $x_0$ = 0) and the x0 axis at 3 (if we set $x_1$ = 0, $x_0$ = 3). \n", 252 | "\n", 253 | "\n", 254 | "* The shaded region represents $-3 + x_0+x_1 < 0$. The region above the line is $-3 + x_0+x_1 > 0$.\n", 255 | "\n", 256 | "\n", 257 | "* Any point in the shaded region (under the line) is classified as $y=0$. Any point on or above the line is classified as $y=1$. This line is known as the \"decision boundary\".\n", 258 | "\n", 259 | "As we've seen in the lectures, by using higher order polynomial terms (eg: $f(x) = g( x_0^2 + x_1 -1)$, we can come up with more complex non-linear boundaries." 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "## Congratulations!\n", 267 | "You have explored the decision boundary in the context of logistic regression." 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [] 283 | } 284 | ], 285 | "metadata": { 286 | "kernelspec": { 287 | "display_name": "Python 3", 288 | "language": "python", 289 | "name": "python3" 290 | }, 291 | "language_info": { 292 | "codemirror_mode": { 293 | "name": "ipython", 294 | "version": 3 295 | }, 296 | "file_extension": ".py", 297 | "mimetype": "text/x-python", 298 | "name": "python", 299 | "nbconvert_exporter": "python", 300 | "pygments_lexer": "ipython3", 301 | "version": "3.7.6" 302 | } 303 | }, 304 | "nbformat": 4, 305 | "nbformat_minor": 5 306 | } 307 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W3/C1_W3_Lab05_Cost_Function_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Optional Lab: Cost Function for Logistic Regression\n", 8 | "\n", 9 | "## Goals\n", 10 | "In this lab, you will:\n", 11 | "- examine the implementation and utilize the cost function for logistic regression." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "import numpy as np\n", 21 | "%matplotlib widget\n", 22 | "import matplotlib.pyplot as plt\n", 23 | "from lab_utils_common import plot_data, sigmoid, dlc\n", 24 | "plt.style.use('./deeplearning.mplstyle')" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## Dataset \n", 32 | "Let's start with the same dataset as was used in the decision boundary lab." 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": { 39 | "tags": [] 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "X_train = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]]) #(m,n)\n", 44 | "y_train = np.array([0, 0, 0, 1, 1, 1]) #(m,)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "We will use a helper function to plot this data. The data points with label $y=1$ are shown as red crosses, while the data points with label $y=0$ are shown as blue circles." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "application/vnd.jupyter.widget-view+json": { 62 | "model_id": "fb21446bc7764e98857907d2d73db8ad", 63 | "version_major": 2, 64 | "version_minor": 0 65 | }, 66 | "text/plain": [ 67 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 68 | ] 69 | }, 70 | "metadata": {}, 71 | "output_type": "display_data" 72 | } 73 | ], 74 | "source": [ 75 | "fig,ax = plt.subplots(1,1,figsize=(4,4))\n", 76 | "plot_data(X_train, y_train, ax)\n", 77 | "\n", 78 | "# Set both axes to be from 0-4\n", 79 | "ax.axis([0, 4, 0, 3.5])\n", 80 | "ax.set_ylabel('$x_1$', fontsize=12)\n", 81 | "ax.set_xlabel('$x_0$', fontsize=12)\n", 82 | "plt.show()" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "## Cost function\n", 90 | "\n", 91 | "In a previous lab, you developed the *logistic loss* function. Recall, loss is defined to apply to one example. Here you combine the losses to form the **cost**, which includes all the examples.\n", 92 | "\n", 93 | "\n", 94 | "Recall that for logistic regression, the cost function is of the form \n", 95 | "\n", 96 | "$$ J(\\mathbf{w},b) = \\frac{1}{m} \\sum_{i=0}^{m-1} \\left[ loss(f_{\\mathbf{w},b}(\\mathbf{x}^{(i)}), y^{(i)}) \\right] \\tag{1}$$\n", 97 | "\n", 98 | "where\n", 99 | "* $loss(f_{\\mathbf{w},b}(\\mathbf{x}^{(i)}), y^{(i)})$ is the cost for a single data point, which is:\n", 100 | "\n", 101 | " $$loss(f_{\\mathbf{w},b}(\\mathbf{x}^{(i)}), y^{(i)}) = -y^{(i)} \\log\\left(f_{\\mathbf{w},b}\\left( \\mathbf{x}^{(i)} \\right) \\right) - \\left( 1 - y^{(i)}\\right) \\log \\left( 1 - f_{\\mathbf{w},b}\\left( \\mathbf{x}^{(i)} \\right) \\right) \\tag{2}$$\n", 102 | " \n", 103 | "* where m is the number of training examples in the data set and:\n", 104 | "$$\n", 105 | "\\begin{align}\n", 106 | " f_{\\mathbf{w},b}(\\mathbf{x^{(i)}}) &= g(z^{(i)})\\tag{3} \\\\\n", 107 | " z^{(i)} &= \\mathbf{w} \\cdot \\mathbf{x}^{(i)}+ b\\tag{4} \\\\\n", 108 | " g(z^{(i)}) &= \\frac{1}{1+e^{-z^{(i)}}}\\tag{5} \n", 109 | "\\end{align}\n", 110 | "$$\n", 111 | " " 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "\n", 119 | "#### Code Description\n", 120 | "\n", 121 | "The algorithm for `compute_cost_logistic` loops over all the examples calculating the loss for each example and accumulating the total.\n", 122 | "\n", 123 | "Note that the variables X and y are not scalar values but matrices of shape ($m, n$) and ($𝑚$,) respectively, where $𝑛$ is the number of features and $𝑚$ is the number of training examples.\n" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 4, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "def compute_cost_logistic(X, y, w, b):\n", 133 | " \"\"\"\n", 134 | " Computes cost\n", 135 | "\n", 136 | " Args:\n", 137 | " X (ndarray (m,n)): Data, m examples with n features\n", 138 | " y (ndarray (m,)) : target values\n", 139 | " w (ndarray (n,)) : model parameters \n", 140 | " b (scalar) : model parameter\n", 141 | " \n", 142 | " Returns:\n", 143 | " cost (scalar): cost\n", 144 | " \"\"\"\n", 145 | "\n", 146 | " m = X.shape[0]\n", 147 | " cost = 0.0\n", 148 | " for i in range(m):\n", 149 | " z_i = np.dot(X[i],w) + b\n", 150 | " f_wb_i = sigmoid(z_i)\n", 151 | " cost += -y[i]*np.log(f_wb_i) - (1-y[i])*np.log(1-f_wb_i)\n", 152 | " \n", 153 | " cost = cost / m\n", 154 | " return cost\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Check the implementation of the cost function using the cell below." 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 5, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "0.36686678640551745\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "w_tmp = np.array([1,1])\n", 179 | "b_tmp = -3\n", 180 | "print(compute_cost_logistic(X_train, y_train, w_tmp, b_tmp))" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "**Expected output**: 0.3668667864055175" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "## Example\n", 195 | "Now, let's see what the cost function output is for a different value of $w$. \n", 196 | "\n", 197 | "* In a previous lab, you plotted the decision boundary for $b = -3, w_0 = 1, w_1 = 1$. That is, you had `b = -3, w = np.array([1,1])`.\n", 198 | "\n", 199 | "* Let's say you want to see if $b = -4, w_0 = 1, w_1 = 1$, or `b = -4, w = np.array([1,1])` provides a better model.\n", 200 | "\n", 201 | "Let's first plot the decision boundary for these two different $b$ values to see which one fits the data better.\n", 202 | "\n", 203 | "* For $b = -3, w_0 = 1, w_1 = 1$, we'll plot $-3 + x_0+x_1 = 0$ (shown in blue)\n", 204 | "* For $b = -4, w_0 = 1, w_1 = 1$, we'll plot $-4 + x_0+x_1 = 0$ (shown in magenta)" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 6, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "data": { 214 | "application/vnd.jupyter.widget-view+json": { 215 | "model_id": "24af642294ae430fb5c0c7ad97d98f79", 216 | "version_major": 2, 217 | "version_minor": 0 218 | }, 219 | "text/plain": [ 220 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 221 | ] 222 | }, 223 | "metadata": {}, 224 | "output_type": "display_data" 225 | } 226 | ], 227 | "source": [ 228 | "import matplotlib.pyplot as plt\n", 229 | "\n", 230 | "# Choose values between 0 and 6\n", 231 | "x0 = np.arange(0,6)\n", 232 | "\n", 233 | "# Plot the two decision boundaries\n", 234 | "x1 = 3 - x0\n", 235 | "x1_other = 4 - x0\n", 236 | "\n", 237 | "fig,ax = plt.subplots(1, 1, figsize=(4,4))\n", 238 | "# Plot the decision boundary\n", 239 | "ax.plot(x0,x1, c=dlc[\"dlblue\"], label=\"$b$=-3\")\n", 240 | "ax.plot(x0,x1_other, c=dlc[\"dlmagenta\"], label=\"$b$=-4\")\n", 241 | "ax.axis([0, 4, 0, 4])\n", 242 | "\n", 243 | "# Plot the original data\n", 244 | "plot_data(X_train,y_train,ax)\n", 245 | "ax.axis([0, 4, 0, 4])\n", 246 | "ax.set_ylabel('$x_1$', fontsize=12)\n", 247 | "ax.set_xlabel('$x_0$', fontsize=12)\n", 248 | "plt.legend(loc=\"upper right\")\n", 249 | "plt.title(\"Decision Boundary\")\n", 250 | "plt.show()" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "You can see from this plot that `b = -4, w = np.array([1,1])` is a worse model for the training data. Let's see if the cost function implementation reflects this." 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 7, 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "name": "stdout", 267 | "output_type": "stream", 268 | "text": [ 269 | "Cost for b = -3 : 0.36686678640551745\n", 270 | "Cost for b = -4 : 0.5036808636748461\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "w_array1 = np.array([1,1])\n", 276 | "b_1 = -3\n", 277 | "w_array2 = np.array([1,1])\n", 278 | "b_2 = -4\n", 279 | "\n", 280 | "print(\"Cost for b = -3 : \", compute_cost_logistic(X_train, y_train, w_array1, b_1))\n", 281 | "print(\"Cost for b = -4 : \", compute_cost_logistic(X_train, y_train, w_array2, b_2))" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "**Expected output**\n", 289 | "\n", 290 | "Cost for b = -3 : 0.3668667864055175\n", 291 | "\n", 292 | "Cost for b = -4 : 0.5036808636748461\n", 293 | "\n", 294 | "\n", 295 | "You can see the cost function behaves as expected and the cost for `b = -4, w = np.array([1,1])` is indeed higher than the cost for `b = -3, w = np.array([1,1])`" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "## Congratulations!\n", 303 | "In this lab you examined and utilized the cost function for logistic regression." 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [] 312 | } 313 | ], 314 | "metadata": { 315 | "kernelspec": { 316 | "display_name": "Python 3", 317 | "language": "python", 318 | "name": "python3" 319 | }, 320 | "language_info": { 321 | "codemirror_mode": { 322 | "name": "ipython", 323 | "version": 3 324 | }, 325 | "file_extension": ".py", 326 | "mimetype": "text/x-python", 327 | "name": "python", 328 | "nbconvert_exporter": "python", 329 | "pygments_lexer": "ipython3", 330 | "version": "3.7.6" 331 | } 332 | }, 333 | "nbformat": 4, 334 | "nbformat_minor": 5 335 | } 336 | -------------------------------------------------------------------------------- /Supervised Machine Learning: Regression and Classification/W3/C1_W3_Lab02_Sigmoid_function_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Optional Lab: Logistic Regression\n", 8 | "\n", 9 | "In this ungraded lab, you will \n", 10 | "- explore the sigmoid function (also known as the logistic function)\n", 11 | "- explore logistic regression; which uses the sigmoid function" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "import numpy as np\n", 21 | "%matplotlib widget\n", 22 | "import matplotlib.pyplot as plt\n", 23 | "from plt_one_addpt_onclick import plt_one_addpt_onclick\n", 24 | "from lab_utils_common import draw_vthresh\n", 25 | "plt.style.use('./deeplearning.mplstyle')" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## Sigmoid or Logistic Function\n", 33 | "As discussed in the lecture videos, for a classification task, we can start by using our linear regression model, $f_{\\mathbf{w},b}(\\mathbf{x}^{(i)}) = \\mathbf{w} \\cdot \\mathbf{x}^{(i)} + b$, to predict $y$ given $x$. \n", 34 | "- However, we would like the predictions of our classification model to be between 0 and 1 since our output variable $y$ is either 0 or 1. \n", 35 | "- This can be accomplished by using a \"sigmoid function\" which maps all input values to values between 0 and 1. \n", 36 | "\n", 37 | "\n", 38 | "Let's implement the sigmoid function and see this for ourselves.\n", 39 | "\n", 40 | "## Formula for Sigmoid function\n", 41 | "\n", 42 | "The formula for a sigmoid function is as follows - \n", 43 | "\n", 44 | "$g(z) = \\frac{1}{1+e^{-z}}\\tag{1}$\n", 45 | "\n", 46 | "In the case of logistic regression, z (the input to the sigmoid function), is the output of a linear regression model. \n", 47 | "- In the case of a single example, $z$ is scalar.\n", 48 | "- in the case of multiple examples, $z$ may be a vector consisting of $m$ values, one for each example. \n", 49 | "- The implementation of the sigmoid function should cover both of these potential input formats.\n", 50 | "Let's implement this in Python." 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "NumPy has a function called [`exp()`](https://numpy.org/doc/stable/reference/generated/numpy.exp.html), which offers a convenient way to calculate the exponential ( $e^{z}$) of all elements in the input array (`z`).\n", 58 | " \n", 59 | "It also works with a single number as an input, as shown below." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 2, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "Input to exp: [1 2 3]\n", 72 | "Output of exp: [ 2.72 7.39 20.09]\n", 73 | "Input to exp: 1\n", 74 | "Output of exp: 2.718281828459045\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "# Input is an array. \n", 80 | "input_array = np.array([1,2,3])\n", 81 | "exp_array = np.exp(input_array)\n", 82 | "\n", 83 | "print(\"Input to exp:\", input_array)\n", 84 | "print(\"Output of exp:\", exp_array)\n", 85 | "\n", 86 | "# Input is a single number\n", 87 | "input_val = 1 \n", 88 | "exp_val = np.exp(input_val)\n", 89 | "\n", 90 | "print(\"Input to exp:\", input_val)\n", 91 | "print(\"Output of exp:\", exp_val)" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "The `sigmoid` function is implemented in python as shown in the cell below." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 3, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "def sigmoid(z):\n", 108 | " \"\"\"\n", 109 | " Compute the sigmoid of z\n", 110 | "\n", 111 | " Args:\n", 112 | " z (ndarray): A scalar, numpy array of any size.\n", 113 | "\n", 114 | " Returns:\n", 115 | " g (ndarray): sigmoid(z), with the same shape as z\n", 116 | " \n", 117 | " \"\"\"\n", 118 | "\n", 119 | " g = 1/(1+np.exp(-z))\n", 120 | " \n", 121 | " return g" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "Let's see what the output of this function is for various value of `z`" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 4, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "Input (z), Output (sigmoid(z))\n", 141 | "[[-1.000e+01 4.540e-05]\n", 142 | " [-9.000e+00 1.234e-04]\n", 143 | " [-8.000e+00 3.354e-04]\n", 144 | " [-7.000e+00 9.111e-04]\n", 145 | " [-6.000e+00 2.473e-03]\n", 146 | " [-5.000e+00 6.693e-03]\n", 147 | " [-4.000e+00 1.799e-02]\n", 148 | " [-3.000e+00 4.743e-02]\n", 149 | " [-2.000e+00 1.192e-01]\n", 150 | " [-1.000e+00 2.689e-01]\n", 151 | " [ 0.000e+00 5.000e-01]\n", 152 | " [ 1.000e+00 7.311e-01]\n", 153 | " [ 2.000e+00 8.808e-01]\n", 154 | " [ 3.000e+00 9.526e-01]\n", 155 | " [ 4.000e+00 9.820e-01]\n", 156 | " [ 5.000e+00 9.933e-01]\n", 157 | " [ 6.000e+00 9.975e-01]\n", 158 | " [ 7.000e+00 9.991e-01]\n", 159 | " [ 8.000e+00 9.997e-01]\n", 160 | " [ 9.000e+00 9.999e-01]\n", 161 | " [ 1.000e+01 1.000e+00]]\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "# Generate an array of evenly spaced values between -10 and 10\n", 167 | "z_tmp = np.arange(-10,11)\n", 168 | "\n", 169 | "# Use the function implemented above to get the sigmoid values\n", 170 | "y = sigmoid(z_tmp)\n", 171 | "\n", 172 | "# Code for pretty printing the two arrays next to each other\n", 173 | "np.set_printoptions(precision=3) \n", 174 | "print(\"Input (z), Output (sigmoid(z))\")\n", 175 | "print(np.c_[z_tmp, y])" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "The values in the left column are `z`, and the values in the right column are `sigmoid(z)`. As you can see, the input values to the sigmoid range from -10 to 10, and the output values range from 0 to 1. \n", 183 | "\n", 184 | "Now, let's try to plot this function using the `matplotlib` library." 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 5, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "application/vnd.jupyter.widget-view+json": { 195 | "model_id": "5b6d95c17b694add96e8302ce14ac9f8", 196 | "version_major": 2, 197 | "version_minor": 0 198 | }, 199 | "text/plain": [ 200 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 201 | ] 202 | }, 203 | "metadata": {}, 204 | "output_type": "display_data" 205 | } 206 | ], 207 | "source": [ 208 | "# Plot z vs sigmoid(z)\n", 209 | "fig,ax = plt.subplots(1,1,figsize=(5,3))\n", 210 | "ax.plot(z_tmp, y, c=\"b\")\n", 211 | "\n", 212 | "ax.set_title(\"Sigmoid function\")\n", 213 | "ax.set_ylabel('sigmoid(z)')\n", 214 | "ax.set_xlabel('z')\n", 215 | "draw_vthresh(ax,0)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "As you can see, the sigmoid function approaches `0` as `z` goes to large negative values and approaches `1` as `z` goes to large positive values.\n" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "## Logistic Regression\n", 230 | " A logistic regression model applies the sigmoid to the familiar linear regression model as shown below:\n", 231 | "\n", 232 | "$$ f_{\\mathbf{w},b}(\\mathbf{x}^{(i)}) = g(\\mathbf{w} \\cdot \\mathbf{x}^{(i)} + b ) \\tag{2} $$ \n", 233 | "\n", 234 | " where\n", 235 | "\n", 236 | " $g(z) = \\frac{1}{1+e^{-z}}\\tag{3}$\n" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | " \n", 244 | "Let's apply logistic regression to the categorical data example of tumor classification. \n", 245 | "First, load the examples and initial values for the parameters.\n", 246 | " \n" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 6, 252 | "metadata": { 253 | "tags": [] 254 | }, 255 | "outputs": [], 256 | "source": [ 257 | "x_train = np.array([0., 1, 2, 3, 4, 5])\n", 258 | "y_train = np.array([0, 0, 0, 1, 1, 1])\n", 259 | "\n", 260 | "w_in = np.zeros((1))\n", 261 | "b_in = 0" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "Try the following steps:\n", 269 | "- Click on 'Run Logistic Regression' to find the best logistic regression model for the given training data\n", 270 | " - Note the resulting model fits the data quite well.\n", 271 | " - Note, the orange line is '$z$' or $\\mathbf{w} \\cdot \\mathbf{x}^{(i)} + b$ above. It does not match the line in a linear regression model.\n", 272 | "Further improve these results by applying a *threshold*. \n", 273 | "- Tick the box on the 'Toggle 0.5 threshold' to show the predictions if a threshold is applied.\n", 274 | " - These predictions look good. The predictions match the data\n", 275 | " - Now, add further data points in the large tumor size range (near 10), and re-run logistic regression.\n", 276 | " - unlike the linear regression model, this model continues to make correct predictions" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 7, 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "data": { 286 | "application/vnd.jupyter.widget-view+json": { 287 | "model_id": "83b1cbf16e184091b287ed8306d49d4a", 288 | "version_major": 2, 289 | "version_minor": 0 290 | }, 291 | "text/plain": [ 292 | "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" 293 | ] 294 | }, 295 | "metadata": {}, 296 | "output_type": "display_data" 297 | } 298 | ], 299 | "source": [ 300 | "plt.close('all') \n", 301 | "addpt = plt_one_addpt_onclick( x_train,y_train, w_in, b_in, logistic=True)" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "## Congratulations!\n", 309 | "You have explored the use of the sigmoid function in logistic regression." 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": null, 315 | "metadata": {}, 316 | "outputs": [], 317 | "source": [] 318 | } 319 | ], 320 | "metadata": { 321 | "kernelspec": { 322 | "display_name": "Python 3", 323 | "language": "python", 324 | "name": "python3" 325 | }, 326 | "language_info": { 327 | "codemirror_mode": { 328 | "name": "ipython", 329 | "version": 3 330 | }, 331 | "file_extension": ".py", 332 | "mimetype": "text/x-python", 333 | "name": "python", 334 | "nbconvert_exporter": "python", 335 | "pygments_lexer": "ipython3", 336 | "version": "3.7.6" 337 | } 338 | }, 339 | "nbformat": 4, 340 | "nbformat_minor": 5 341 | } 342 | -------------------------------------------------------------------------------- /src/lab_utils_common.py: -------------------------------------------------------------------------------- 1 | """ 2 | lab_utils_common 3 | contains common routines and variable definitions 4 | used by all the labs in this week. 5 | by contrast, specific, large plotting routines will be in separate files 6 | and are generally imported into the week where they are used. 7 | those files will import this file 8 | """ 9 | import copy 10 | import math 11 | import numpy as np 12 | import matplotlib.pyplot as plt 13 | from matplotlib.patches import FancyArrowPatch 14 | from ipywidgets import Output 15 | 16 | np.set_printoptions(precision=2) 17 | 18 | dlc = dict(dlblue = '#0096ff', dlorange = '#FF9300', dldarkred='#C00000', dlmagenta='#FF40FF', dlpurple='#7030A0') 19 | dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0' 20 | dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple] 21 | plt.style.use('./deeplearning.mplstyle') 22 | 23 | def sigmoid(z): 24 | """ 25 | Compute the sigmoid of z 26 | 27 | Parameters 28 | ---------- 29 | z : array_like 30 | A scalar or numpy array of any size. 31 | 32 | Returns 33 | ------- 34 | g : array_like 35 | sigmoid(z) 36 | """ 37 | z = np.clip( z, -500, 500 ) # protect against overflow 38 | g = 1.0/(1.0+np.exp(-z)) 39 | 40 | return g 41 | 42 | ########################################################## 43 | # Regression Routines 44 | ########################################################## 45 | 46 | def predict_logistic(X, w, b): 47 | """ performs prediction """ 48 | return sigmoid(X @ w + b) 49 | 50 | def predict_linear(X, w, b): 51 | """ performs prediction """ 52 | return X @ w + b 53 | 54 | def compute_cost_logistic(X, y, w, b, lambda_=0, safe=False): 55 | """ 56 | Computes cost using logistic loss, non-matrix version 57 | 58 | Args: 59 | X (ndarray): Shape (m,n) matrix of examples with n features 60 | y (ndarray): Shape (m,) target values 61 | w (ndarray): Shape (n,) parameters for prediction 62 | b (scalar): parameter for prediction 63 | lambda_ : (scalar, float) Controls amount of regularization, 0 = no regularization 64 | safe : (boolean) True-selects under/overflow safe algorithm 65 | Returns: 66 | cost (scalar): cost 67 | """ 68 | 69 | m,n = X.shape 70 | cost = 0.0 71 | for i in range(m): 72 | z_i = np.dot(X[i],w) + b #(n,)(n,) or (n,) () 73 | if safe: #avoids overflows 74 | cost += -(y[i] * z_i ) + log_1pexp(z_i) 75 | else: 76 | f_wb_i = sigmoid(z_i) #(n,) 77 | cost += -y[i] * np.log(f_wb_i) - (1 - y[i]) * np.log(1 - f_wb_i) # scalar 78 | cost = cost/m 79 | 80 | reg_cost = 0 81 | if lambda_ != 0: 82 | for j in range(n): 83 | reg_cost += (w[j]**2) # scalar 84 | reg_cost = (lambda_/(2*m))*reg_cost 85 | 86 | return cost + reg_cost 87 | 88 | 89 | def log_1pexp(x, maximum=20): 90 | ''' approximate log(1+exp^x) 91 | https://stats.stackexchange.com/questions/475589/numerical-computation-of-cross-entropy-in-practice 92 | Args: 93 | x : (ndarray Shape (n,1) or (n,) input 94 | out : (ndarray Shape matches x output ~= np.log(1+exp(x)) 95 | ''' 96 | 97 | out = np.zeros_like(x,dtype=float) 98 | i = x <= maximum 99 | ni = np.logical_not(i) 100 | 101 | out[i] = np.log(1 + np.exp(x[i])) 102 | out[ni] = x[ni] 103 | return out 104 | 105 | 106 | def compute_cost_matrix(X, y, w, b, logistic=False, lambda_=0, safe=True): 107 | """ 108 | Computes the cost using using matrices 109 | Args: 110 | X : (ndarray, Shape (m,n)) matrix of examples 111 | y : (ndarray Shape (m,) or (m,1)) target value of each example 112 | w : (ndarray Shape (n,) or (n,1)) Values of parameter(s) of the model 113 | b : (scalar ) Values of parameter of the model 114 | verbose : (Boolean) If true, print out intermediate value f_wb 115 | Returns: 116 | total_cost: (scalar) cost 117 | """ 118 | m = X.shape[0] 119 | y = y.reshape(-1,1) # ensure 2D 120 | w = w.reshape(-1,1) # ensure 2D 121 | if logistic: 122 | if safe: #safe from overflow 123 | z = X @ w + b #(m,n)(n,1)=(m,1) 124 | cost = -(y * z) + log_1pexp(z) 125 | cost = np.sum(cost)/m # (scalar) 126 | else: 127 | f = sigmoid(X @ w + b) # (m,n)(n,1) = (m,1) 128 | cost = (1/m)*(np.dot(-y.T, np.log(f)) - np.dot((1-y).T, np.log(1-f))) # (1,m)(m,1) = (1,1) 129 | cost = cost[0,0] # scalar 130 | else: 131 | f = X @ w + b # (m,n)(n,1) = (m,1) 132 | cost = (1/(2*m)) * np.sum((f - y)**2) # scalar 133 | 134 | reg_cost = (lambda_/(2*m)) * np.sum(w**2) # scalar 135 | 136 | total_cost = cost + reg_cost # scalar 137 | 138 | return total_cost # scalar 139 | 140 | def compute_gradient_matrix(X, y, w, b, logistic=False, lambda_=0): 141 | """ 142 | Computes the gradient using matrices 143 | 144 | Args: 145 | X : (ndarray, Shape (m,n)) matrix of examples 146 | y : (ndarray Shape (m,) or (m,1)) target value of each example 147 | w : (ndarray Shape (n,) or (n,1)) Values of parameters of the model 148 | b : (scalar ) Values of parameter of the model 149 | logistic: (boolean) linear if false, logistic if true 150 | lambda_: (float) applies regularization if non-zero 151 | Returns 152 | dj_dw: (array_like Shape (n,1)) The gradient of the cost w.r.t. the parameters w 153 | dj_db: (scalar) The gradient of the cost w.r.t. the parameter b 154 | """ 155 | m = X.shape[0] 156 | y = y.reshape(-1,1) # ensure 2D 157 | w = w.reshape(-1,1) # ensure 2D 158 | 159 | f_wb = sigmoid( X @ w + b ) if logistic else X @ w + b # (m,n)(n,1) = (m,1) 160 | err = f_wb - y # (m,1) 161 | dj_dw = (1/m) * (X.T @ err) # (n,m)(m,1) = (n,1) 162 | dj_db = (1/m) * np.sum(err) # scalar 163 | 164 | dj_dw += (lambda_/m) * w # regularize # (n,1) 165 | 166 | return dj_db, dj_dw # scalar, (n,1) 167 | 168 | def gradient_descent(X, y, w_in, b_in, alpha, num_iters, logistic=False, lambda_=0, verbose=True): 169 | """ 170 | Performs batch gradient descent to learn theta. Updates theta by taking 171 | num_iters gradient steps with learning rate alpha 172 | 173 | Args: 174 | X (ndarray): Shape (m,n) matrix of examples 175 | y (ndarray): Shape (m,) or (m,1) target value of each example 176 | w_in (ndarray): Shape (n,) or (n,1) Initial values of parameters of the model 177 | b_in (scalar): Initial value of parameter of the model 178 | logistic: (boolean) linear if false, logistic if true 179 | lambda_: (float) applies regularization if non-zero 180 | alpha (float): Learning rate 181 | num_iters (int): number of iterations to run gradient descent 182 | 183 | Returns: 184 | w (ndarray): Shape (n,) or (n,1) Updated values of parameters; matches incoming shape 185 | b (scalar): Updated value of parameter 186 | """ 187 | # An array to store cost J and w's at each iteration primarily for graphing later 188 | J_history = [] 189 | w = copy.deepcopy(w_in) #avoid modifying global w within function 190 | b = b_in 191 | w = w.reshape(-1,1) #prep for matrix operations 192 | y = y.reshape(-1,1) 193 | 194 | for i in range(num_iters): 195 | 196 | # Calculate the gradient and update the parameters 197 | dj_db,dj_dw = compute_gradient_matrix(X, y, w, b, logistic, lambda_) 198 | 199 | # Update Parameters using w, b, alpha and gradient 200 | w = w - alpha * dj_dw 201 | b = b - alpha * dj_db 202 | 203 | # Save cost J at each iteration 204 | if i<100000: # prevent resource exhaustion 205 | J_history.append( compute_cost_matrix(X, y, w, b, logistic, lambda_) ) 206 | 207 | # Print cost every at intervals 10 times or as many iterations if < 10 208 | if i% math.ceil(num_iters / 10) == 0: 209 | if verbose: print(f"Iteration {i:4d}: Cost {J_history[-1]} ") 210 | 211 | return w.reshape(w_in.shape), b, J_history #return final w,b and J history for graphing 212 | 213 | def zscore_normalize_features(X): 214 | """ 215 | computes X, zcore normalized by column 216 | 217 | Args: 218 | X (ndarray): Shape (m,n) input data, m examples, n features 219 | 220 | Returns: 221 | X_norm (ndarray): Shape (m,n) input normalized by column 222 | mu (ndarray): Shape (n,) mean of each feature 223 | sigma (ndarray): Shape (n,) standard deviation of each feature 224 | """ 225 | # find the mean of each column/feature 226 | mu = np.mean(X, axis=0) # mu will have shape (n,) 227 | # find the standard deviation of each column/feature 228 | sigma = np.std(X, axis=0) # sigma will have shape (n,) 229 | # element-wise, subtract mu for that column from each example, divide by std for that column 230 | X_norm = (X - mu) / sigma 231 | 232 | return X_norm, mu, sigma 233 | 234 | #check our work 235 | #from sklearn.preprocessing import scale 236 | #scale(X_orig, axis=0, with_mean=True, with_std=True, copy=True) 237 | 238 | ###################################################### 239 | # Common Plotting Routines 240 | ###################################################### 241 | 242 | 243 | def plot_data(X, y, ax, pos_label="y=1", neg_label="y=0", s=80, loc='best' ): 244 | """ plots logistic data with two axis """ 245 | # Find Indices of Positive and Negative Examples 246 | pos = y == 1 247 | neg = y == 0 248 | pos = pos.reshape(-1,) #work with 1D or 1D y vectors 249 | neg = neg.reshape(-1,) 250 | 251 | # Plot examples 252 | ax.scatter(X[pos, 0], X[pos, 1], marker='x', s=s, c = 'red', label=pos_label) 253 | ax.scatter(X[neg, 0], X[neg, 1], marker='o', s=s, label=neg_label, facecolors='none', edgecolors=dlblue, lw=3) 254 | ax.legend(loc=loc) 255 | 256 | ax.figure.canvas.toolbar_visible = False 257 | ax.figure.canvas.header_visible = False 258 | ax.figure.canvas.footer_visible = False 259 | 260 | def plt_tumor_data(x, y, ax): 261 | """ plots tumor data on one axis """ 262 | pos = y == 1 263 | neg = y == 0 264 | 265 | ax.scatter(x[pos], y[pos], marker='x', s=80, c = 'red', label="malignant") 266 | ax.scatter(x[neg], y[neg], marker='o', s=100, label="benign", facecolors='none', edgecolors=dlblue,lw=3) 267 | ax.set_ylim(-0.175,1.1) 268 | ax.set_ylabel('y') 269 | ax.set_xlabel('Tumor Size') 270 | ax.set_title("Logistic Regression on Categorical Data") 271 | 272 | ax.figure.canvas.toolbar_visible = False 273 | ax.figure.canvas.header_visible = False 274 | ax.figure.canvas.footer_visible = False 275 | 276 | # Draws a threshold at 0.5 277 | def draw_vthresh(ax,x): 278 | """ draws a threshold """ 279 | ylim = ax.get_ylim() 280 | xlim = ax.get_xlim() 281 | ax.fill_between([xlim[0], x], [ylim[1], ylim[1]], alpha=0.2, color=dlblue) 282 | ax.fill_between([x, xlim[1]], [ylim[1], ylim[1]], alpha=0.2, color=dldarkred) 283 | ax.annotate("z >= 0", xy= [x,0.5], xycoords='data', 284 | xytext=[30,5],textcoords='offset points') 285 | d = FancyArrowPatch( 286 | posA=(x, 0.5), posB=(x+3, 0.5), color=dldarkred, 287 | arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0', 288 | ) 289 | ax.add_artist(d) 290 | ax.annotate("z < 0", xy= [x,0.5], xycoords='data', 291 | xytext=[-50,5],textcoords='offset points', ha='left') 292 | f = FancyArrowPatch( 293 | posA=(x, 0.5), posB=(x-3, 0.5), color=dlblue, 294 | arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0', 295 | ) 296 | ax.add_artist(f) 297 | -------------------------------------------------------------------------------- /src/recsysNN_utils.py: -------------------------------------------------------------------------------- 1 | import pickle5 as pickle 2 | import numpy as np 3 | from numpy import genfromtxt 4 | from collections import defaultdict 5 | import pandas as pd 6 | import tensorflow as tf 7 | from tensorflow.keras.models import Model 8 | from sklearn.preprocessing import StandardScaler, MinMaxScaler 9 | from sklearn.model_selection import train_test_split 10 | import csv 11 | import re 12 | import tabulate 13 | 14 | 15 | def load_data(): 16 | item_train = genfromtxt('./data/content_item_train.csv', delimiter=',') 17 | user_train = genfromtxt('./data/content_user_train.csv', delimiter=',') 18 | y_train = genfromtxt('./data/content_y_train.csv', delimiter=',') 19 | with open('./data/content_item_train_header.txt', newline='') as f: #csv reader handles quoted strings better 20 | item_features = list(csv.reader(f))[0] 21 | with open('./data/content_user_train_header.txt', newline='') as f: 22 | user_features = list(csv.reader(f))[0] 23 | item_vecs = genfromtxt('./data/content_item_vecs.csv', delimiter=',') 24 | 25 | movie_dict = defaultdict(dict) 26 | count = 0 27 | # with open('./data/movies.csv', newline='') as csvfile: 28 | with open('./data/content_movie_list.csv', newline='') as csvfile: 29 | reader = csv.reader(csvfile, delimiter=',', quotechar='"') 30 | for line in reader: 31 | if count == 0: 32 | count +=1 #skip header 33 | #print(line) 34 | else: 35 | count +=1 36 | movie_id = int(line[0]) 37 | movie_dict[movie_id]["title"] = line[1] 38 | movie_dict[movie_id]["genres"] =line[2] 39 | 40 | with open('./data/content_user_to_genre.pickle', 'rb') as f: 41 | user_to_genre = pickle.load(f) 42 | 43 | return(item_train, user_train, y_train, item_features, user_features, item_vecs, movie_dict, user_to_genre) 44 | 45 | 46 | def pprint_train(x_train, features, vs, u_s, maxcount = 5, user=True): 47 | """ Prints user_train or item_train nicely """ 48 | if user: 49 | flist = [".0f",".0f",".1f", 50 | ".1f", ".1f", ".1f", ".1f",".1f",".1f", ".1f",".1f",".1f", ".1f",".1f",".1f",".1f",".1f"] 51 | else: 52 | flist = [".0f",".0f",".1f", 53 | ".0f",".0f",".0f", ".0f",".0f",".0f", ".0f",".0f",".0f", ".0f",".0f",".0f",".0f",".0f"] 54 | 55 | head = features[:vs] 56 | if vs < u_s: print("error, vector start {vs} should be greater then user start {u_s}") 57 | for i in range(u_s): 58 | head[i] = "[" + head[i] + "]" 59 | genres = features[vs:] 60 | hdr = head + genres 61 | disp = [split_str(hdr, 5)] 62 | count = 0 63 | for i in range(0,x_train.shape[0]): 64 | if count == maxcount: break 65 | count += 1 66 | disp.append( [ 67 | x_train[i,0].astype(int), 68 | x_train[i,1].astype(int), 69 | x_train[i,2].astype(float), 70 | *x_train[i,3:].astype(float) 71 | ]) 72 | table = tabulate.tabulate(disp, tablefmt='html',headers="firstrow", floatfmt=flist, numalign='center') 73 | return(table) 74 | 75 | 76 | def pprint_data(y_p, user_train, item_train, printfull=False): 77 | np.set_printoptions(precision=1) 78 | 79 | for i in range(0,1000): 80 | #print(f"{y_p[i,0]: 0.2f}, {ynorm_train.numpy()[i].item(): 0.2f}") 81 | print(f"{y_pu[i,0]: 0.2f}, {y_train[i]: 0.2f}, ", end='') 82 | print(f"{user_train[i,0].astype(int):d}, ", end='') # userid 83 | print(f"{user_train[i,1].astype(int):d}, ", end=''), # rating cnt 84 | print(f"{user_train[i,2].astype(float): 0.2f}, ", end='') # rating ave 85 | print(": ", end = '') 86 | print(f"{item_train[i,0].astype(int):d}, ", end='') # movie id 87 | print(f"{item_train[i,2].astype(float):0.1f}, ", end='') # ave movie rating 88 | if printfull: 89 | for j in range(8, user_train.shape[1]): 90 | print(f"{user_train[i,j].astype(float):0.1f}, ", end='') # rating 91 | print(":", end='') 92 | for j in range(3, item_train.shape[1]): 93 | print(f"{item_train[i,j].astype(int):d}, ", end='') # rating 94 | print() 95 | else: 96 | a = user_train[i, uvs:user_train.shape[1]] 97 | b = item_train[i, ivs:item_train.shape[1]] 98 | c = np.multiply(a,b) 99 | print(c) 100 | 101 | def split_str(ifeatures, smax): 102 | ofeatures = [] 103 | for s in ifeatures: 104 | if ' ' not in s: # skip string that already have a space 105 | if len(s) > smax: 106 | mid = int(len(s)/2) 107 | s = s[:mid] + " " + s[mid:] 108 | ofeatures.append(s) 109 | return(ofeatures) 110 | 111 | def pprint_data_tab(y_p, user_train, item_train, uvs, ivs, user_features, item_features, maxcount = 20, printfull=False): 112 | flist = [".1f", ".1f", ".0f", ".1f", ".0f", ".0f", ".0f", 113 | ".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f",".1f"] 114 | user_head = user_features[:uvs] 115 | genres = user_features[uvs:] 116 | item_head = item_features[:ivs] 117 | hdr = ["y_p", "y"] + user_head + item_head + genres 118 | disp = [split_str(hdr, 5)] 119 | count = 0 120 | for i in range(0,y_p.shape[0]): 121 | if count == maxcount: break 122 | count += 1 123 | a = user_train[i, uvs:user_train.shape[1]] 124 | b = item_train[i, ivs:item_train.shape[1]] 125 | c = np.multiply(a,b) 126 | 127 | disp.append( [ y_p[i,0], y_train[i], 128 | user_train[i,0].astype(int), # user id 129 | user_train[i,1].astype(int), # rating cnt 130 | user_train[i,2].astype(float), # user rating ave 131 | item_train[i,0].astype(int), # movie id 132 | item_train[i,1].astype(int), # year 133 | item_train[i,2].astype(float), # ave movie rating 134 | *c 135 | ]) 136 | table = tabulate.tabulate(disp, tablefmt='html',headers="firstrow", floatfmt=flist, numalign='center') 137 | return(table) 138 | 139 | 140 | 141 | 142 | def print_pred_movies(y_p, user, item, movie_dict, maxcount=10): 143 | """ print results of prediction of a new user. inputs are expected to be in 144 | sorted order, unscaled. """ 145 | count = 0 146 | movies_listed = defaultdict(int) 147 | disp = [["y_p", "movie id", "rating ave", "title", "genres"]] 148 | 149 | for i in range(0, y_p.shape[0]): 150 | if count == maxcount: 151 | break 152 | count += 1 153 | movie_id = item[i, 0].astype(int) 154 | if movie_id in movies_listed: 155 | continue 156 | movies_listed[movie_id] = 1 157 | disp.append([y_p[i, 0], item[i, 0].astype(int), item[i, 2].astype(float), 158 | movie_dict[movie_id]['title'], movie_dict[movie_id]['genres']]) 159 | 160 | table = tabulate.tabulate(disp, tablefmt='html',headers="firstrow") 161 | return(table) 162 | 163 | def gen_user_vecs(user_vec, num_items): 164 | """ given a user vector return: 165 | user predict maxtrix to match the size of item_vecs """ 166 | user_vecs = np.tile(user_vec, (num_items, 1)) 167 | return(user_vecs) 168 | 169 | # predict on everything, filter on print/use 170 | def predict_uservec(user_vecs, item_vecs, model, u_s, i_s, scaler, ScalerUser, ScalerItem, scaledata=False): 171 | """ given a user vector, does the prediction on all movies in item_vecs returns 172 | an array predictions sorted by predicted rating, 173 | arrays of user and item, sorted by predicted rating sorting index 174 | """ 175 | if scaledata: 176 | scaled_user_vecs = ScalerUser.transform(user_vecs) 177 | scaled_item_vecs = ScalerItem.transform(item_vecs) 178 | y_p = model.predict([scaled_user_vecs[:, u_s:], scaled_item_vecs[:, i_s:]]) 179 | else: 180 | y_p = model.predict([user_vecs[:, u_s:], item_vecs[:, i_s:]]) 181 | y_pu = scaler.inverse_transform(y_p) 182 | 183 | if np.any(y_pu < 0) : 184 | print("Error, expected all positive predictions") 185 | sorted_index = np.argsort(-y_pu,axis=0).reshape(-1).tolist() #negate to get largest rating first 186 | sorted_ypu = y_pu[sorted_index] 187 | sorted_items = item_vecs[sorted_index] 188 | sorted_user = user_vecs[sorted_index] 189 | return(sorted_index, sorted_ypu, sorted_items, sorted_user) 190 | 191 | 192 | def print_pred_debug(y_p, y, user, item, maxcount=10, onlyrating=False, printfull=False): 193 | """ hopefully reusable print. Keep for debug """ 194 | count = 0 195 | for i in range(0, y_p.shape[0]): 196 | if onlyrating == False or (onlyrating == True and y[i,0] != 0): 197 | if count == maxcount: break 198 | count += 1 199 | print(f"{y_p[i, 0]: 0.2f}, {y[i,0]: 0.2f}, ", end='') 200 | print(f"{user[i, 0].astype(int):d}, ", end='') # userid 201 | print(f"{user[i, 1].astype(int):d}, ", end=''), # rating cnt 202 | print(f"{user[i, 2].astype(float):0.1f}, ", end=''), # rating ave 203 | print(": ", end = '') 204 | print(f"{item[i, 0].astype(int):d}, ", end='') # movie id 205 | print(f"{item[i, 2].astype(float):0.1f}, ", end='') # ave movie rating 206 | print(": ", end = '') 207 | if printfull: 208 | for j in range(uvs, user.shape[1]): 209 | print(f"{user[i, j].astype(float):0.1f}, ", end='') # rating 210 | print(":", end='') 211 | for j in range(ivs, item.shape[1]): 212 | print(f"{item[i, j].astype(int):d}, ", end='') # rating 213 | print() 214 | else: 215 | a = user[i, uvs:user.shape[1]] 216 | b = item[i, ivs:item.shape[1]] 217 | c = np.multiply(a,b) 218 | print(c) 219 | 220 | 221 | def get_user_vecs(user_id, user_train, item_vecs, user_to_genre): 222 | """ given a user_id, return: 223 | user train/predict matrix to match the size of item_vecs 224 | y vector with ratings for all rated movies and 0 for others of size item_vecs """ 225 | 226 | if user_id not in user_to_genre: 227 | print("error: unknown user id") 228 | return(None) 229 | else: 230 | user_vec_found = False 231 | for i in range(len(user_train)): 232 | if user_train[i, 0] == user_id: 233 | user_vec = user_train[i] 234 | user_vec_found = True 235 | break 236 | if not user_vec_found: 237 | print("error in get_user_vecs, did not find uid in user_train") 238 | num_items = len(item_vecs) 239 | user_vecs = np.tile(user_vec, (num_items, 1)) 240 | 241 | y = np.zeros(num_items) 242 | for i in range(num_items): # walk through movies in item_vecs and get the movies, see if user has rated them 243 | movie_id = item_vecs[i, 0] 244 | if movie_id in user_to_genre[user_id]['movies']: 245 | rating = user_to_genre[user_id]['movies'][movie_id] 246 | else: 247 | rating = 0 248 | y[i] = rating 249 | return(user_vecs, y) 250 | 251 | 252 | def get_item_genre(item, ivs, item_features): 253 | offset = np.where(item[ivs:] == 1)[0][0] 254 | genre = item_features[ivs + offset] 255 | return(genre, offset) 256 | 257 | 258 | def print_existing_user(y_p, y, user, items, item_features, ivs, uvs, movie_dict, maxcount=10): 259 | """ print results of prediction a user who was in the datatbase. inputs are expected to be in sorted order, unscaled. """ 260 | count = 0 261 | movies_listed = defaultdict(int) 262 | disp = [["y_p", "y", "user", "user genre ave", "movie rating ave", "title", "genres"]] 263 | listed = [] 264 | count = 0 265 | for i in range(0, y.shape[0]): 266 | if y[i, 0] != 0: 267 | if count == maxcount: 268 | break 269 | count += 1 270 | movie_id = items[i, 0].astype(int) 271 | 272 | offset = np.where(items[i, ivs:] == 1)[0][0] 273 | genre_rating = user[i, uvs + offset] 274 | genre = item_features[ivs + offset] 275 | disp.append([y_p[i, 0], y[i, 0], 276 | user[i, 0].astype(int), # userid 277 | genre_rating.astype(float), 278 | items[i, 2].astype(float), # movie average rating 279 | movie_dict[movie_id]['title'], genre]) 280 | 281 | table = tabulate.tabulate(disp, tablefmt='html', headers="firstrow", floatfmt=[".1f", ".1f", ".0f", ".2f", ".2f"]) 282 | return(table) --------------------------------------------------------------------------------