├── Advanced Learning Algorithms ├── C2_W1_Assignment.ipynb ├── C2_W1_Lab01_Neurons_and_Layers.ipynb ├── C2_W1_Lab02_CoffeeRoasting_TF.ipynb ├── C2_W1_Lab03_CoffeeRoasting_Numpy.ipynb ├── C2_W2_Assignment.ipynb ├── C2_W2_Multiclass_TF.ipynb ├── C2_W2_Relu.ipynb ├── C2_W2_SoftMax.ipynb ├── C2_W3_Assignment.ipynb ├── C2_W4_Decision_Tree_with_Markdown.ipynb ├── Decision trees.jpg ├── Neural network implementation in Python.jpg ├── Practice quiz Activation Functions.jpg ├── Practice quiz Additional Neural Network Concepts.jpg ├── Practice quiz Advice for applying machine learning.jpg ├── Practice quiz Bias and variance.jpg ├── Practice quiz Decision tree learning.jpg ├── Practice quiz Machine learning development process.jpg ├── Practice quiz Multiclass Classification.jpg ├── Practice quiz Neural Network Training.jpg ├── Practice quiz Neural network model.jpg ├── Practice quiz Neural networks intuition.jpg ├── Practice quiz TensorFlow implementation.jpg ├── Practice quiz Tree ensembles.jpg ├── assigment_utils.py ├── autils.py ├── data │ ├── X.npy │ └── y.npy ├── deeplearning.mplstyle ├── images │ ├── C2_W1_Assign1.PNG │ ├── C2_W1_Assign1_BroadcastIndexes.PNG │ ├── C2_W1_Assign1_BroadcastMatrix.PNG │ ├── C2_W1_Assign1_Broadcasting.gif │ ├── C2_W1_Assign1_VectorAdd.PNG │ ├── C2_W1_CoffeeRoasting.png │ ├── C2_W1_L3_Lab01_3Neurons.PNG │ ├── C2_W1_MatrixMatrix.PNG │ ├── C2_W1_NeuronsAndLayers.png │ ├── C2_W1_RoastingDecision.PNG │ ├── C2_W1_RoastingNetwork.PNG │ ├── C2_W1_VectorMatrix.PNG │ ├── C2_W1_dense.PNG │ ├── C2_W1_dense2.PNG │ ├── C2_W2_Assigment_NN.png │ ├── C2_W2_BinaryVsMultiClass.png │ ├── C2_W2_GradDesc.png │ ├── C2_W2_NNSoftmax.PNG │ ├── C2_W2_ReLU_Graph.png │ ├── C2_W2_ReLU_Network.png │ ├── C2_W2_ReLU_Plot.png │ ├── C2_W2_ReLu.png │ ├── C2_W2_SoftMaxCost.png │ ├── C2_W2_SoftMaxNN.png │ ├── C2_W2_Softmax.png │ ├── C2_W2_SoftmaxReg_NN.png │ ├── C2_W2_Softmax_Header.PNG │ ├── C2_W2_mclass_header.png │ ├── C2_W2_mclass_lab_network.PNG │ ├── C2_W2_mclass_layer1.png │ ├── C2_W2_mclass_layer2.png │ ├── C2_W2_mclass_relu.png │ ├── C2_W2_smallnetwork.png │ ├── C2_W2_softmax_accurate.png │ ├── C2_W3_BiasVarianceDegree.png │ ├── C2_W3_Compute_Cost_linear.png │ ├── C2_W3_TrainingVsNew.png │ ├── C2_W3_TrainingVsNew_Slide.png │ ├── C2_W4_degree.png │ └── C2_W4_justright.PNG ├── lab_coffee_utils.py ├── lab_neurons_utils.py ├── lab_utils_common.py ├── lab_utils_multiclass.py ├── lab_utils_multiclass_TF.py ├── lab_utils_relu.py ├── lab_utils_softmax.py ├── public_tests.py ├── public_tests_a1.py └── utils.py ├── README.md ├── Regression and Classification ├── C1_W1_Lab01_Python_Jupyter_Soln.ipynb ├── C1_W1_Lab02_Course_Preview_Soln.ipynb ├── C1_W1_Lab03_Model_Representation_Soln.ipynb ├── C1_W1_Lab04_Cost_function_Soln.ipynb ├── C1_W1_Lab05_Gradient_Descent_Soln.ipynb ├── C1_W2_Lab01_Python_Numpy_Vectorization_Soln.ipynb ├── C1_W2_Lab02_Multiple_Variable_Soln.ipynb ├── C1_W2_Lab03_Feature_Scaling_and_Learning_Rate_Soln.ipynb ├── C1_W2_Lab04_FeatEng_PolyReg_Soln.ipynb ├── C1_W2_Lab05_Sklearn_GD_Soln.ipynb ├── C1_W2_Lab06_Sklearn_Normal_Soln.ipynb ├── C1_W2_Linear_Regression.ipynb ├── C1_W3_Lab01_Classification_Soln.ipynb ├── C1_W3_Lab02_Sigmoid_function_Soln.ipynb ├── C1_W3_Lab03_Decision_Boundary_Soln.ipynb ├── C1_W3_Lab04_LogisticLoss_Soln.ipynb ├── C1_W3_Lab05_Cost_Function_Soln.ipynb ├── C1_W3_Lab06_Gradient_Descent_Soln.ipynb ├── C1_W3_Lab07_Scikit_Learn_Soln.ipynb ├── C1_W3_Lab08_Overfitting_Soln.ipynb ├── C1_W3_Lab09_Regularization_Soln.ipynb ├── C1_W3_Logistic_Regression.ipynb ├── Practice quiz Classification with logistic regression.jpg ├── Practice quiz Cost function for logistic regression.jpg ├── Practice quiz Gradient descent for logistic regression.jpg ├── Practice quiz Gradient descent in practice.jpg ├── Practice quiz Multiple linear regression.jpg ├── Practice quiz Regression.jpg ├── Practice quiz Supervised vs unsupervised learning.jpg ├── Practice quiz The problem of overfitting.jpg ├── Practice quiz Train the model with gradient descent.jpg ├── __pycache__ │ ├── lab_utils_common.cpython-38.pyc │ └── lab_utils_uni.cpython-38.pyc ├── data.txt ├── data │ ├── ex1data1.txt │ ├── ex1data2.txt │ ├── ex2data1.txt │ ├── ex2data2.txt │ └── houses.txt ├── deeplearning.mplstyle ├── images │ ├── C1W1L1_Markdown.PNG │ ├── C1W1L1_Run.PNG │ ├── C1W1L1_Tour.PNG │ ├── C1W3_XW.PNG │ ├── C1W3_boundary.PNG │ ├── C1W3_example2.PNG │ ├── C1W3_mcpredict.PNG │ ├── C1W3_trainvpredict.PNG │ ├── C1_W1_L3_S1_Lecture_b.png │ ├── C1_W1_L3_S1_model.png │ ├── C1_W1_L3_S1_trainingdata.png │ ├── C1_W1_L3_S2_Lecture_b.png │ ├── C1_W1_L4_S1_Lecture_GD.png │ ├── C1_W1_Lab02_GoalOfRegression.PNG │ ├── C1_W1_Lab03_alpha_too_big.PNG │ ├── C1_W1_Lab03_lecture_learningrate.PNG │ ├── C1_W1_Lab03_lecture_slopes.PNG │ ├── C1_W2_L1_S1_Lecture_b.png │ ├── C1_W2_L1_S1_model.png │ ├── C1_W2_L1_S1_trainingdata.png │ ├── C1_W2_L1_S2_Lectureb.png │ ├── C1_W2_L2_S1_Lecture_GD.png │ ├── C1_W2_Lab02_GoalOfRegression.PNG │ ├── C1_W2_Lab03_alpha_to_big.PNG │ ├── C1_W2_Lab03_lecture_learningrate.PNG │ ├── C1_W2_Lab03_lecture_slopes.PNG │ ├── C1_W2_Lab04_Figures And animations.pptx │ ├── C1_W2_Lab04_Matrices.PNG │ ├── C1_W2_Lab04_Vectors.PNG │ ├── C1_W2_Lab04_dot_notrans.gif │ ├── C1_W2_Lab06_LongRun.PNG │ ├── C1_W2_Lab06_ShortRun.PNG │ ├── C1_W2_Lab06_contours.PNG │ ├── C1_W2_Lab06_featurescalingheader.PNG │ ├── C1_W2_Lab06_learningrate.PNG │ ├── C1_W2_Lab06_scale.PNG │ ├── C1_W2_Lab07_FeatureEngLecture.PNG │ ├── C1_W3_Classification.png │ ├── C1_W3_Lab07_overfitting.PNG │ ├── C1_W3_LinearCostRegularized.png │ ├── C1_W3_LinearGradientRegularized.png │ ├── C1_W3_LogisticCostRegularized.png │ ├── C1_W3_LogisticGradientRegularized.png │ ├── C1_W3_LogisticLoss_a.png │ ├── C1_W3_LogisticLoss_b.png │ ├── C1_W3_LogisticLoss_c.png │ ├── C1_W3_LogisticRegression.png │ ├── C1_W3_LogisticRegression_left.png │ ├── C1_W3_LogisticRegression_right.png │ ├── C1_W3_Logistic_gradient_descent.png │ ├── C1_W3_Overfitting_a.png │ ├── C1_W3_Overfitting_b.png │ ├── C1_W3_Overfitting_c.png │ ├── C1_W3_SqErrorVsLogistic.png │ ├── figure 1.png │ ├── figure 2.png │ ├── figure 3.png │ ├── figure 4.png │ ├── figure 5.png │ └── figure 6.png ├── lab_utils_common.py ├── lab_utils_multi.py ├── lab_utils_uni.py ├── plt_logistic_loss.py ├── plt_one_addpt_onclick.py ├── plt_overfit.py ├── plt_quad_logistic.py ├── public_tests.py ├── solutions.py ├── test_utils.py └── utils.py └── Unsupervised Learning, Recommenders, Reinforcement Learning ├── Anomaly detection.jpg ├── Clustering.jpg ├── Collaborative Filtering.jpg ├── Collaborative ├── C3_W2_Collaborative_RecSys_Assignment.ipynb ├── __pycache__ │ ├── public_tests.cpython-37.pyc │ └── recsys_utils.cpython-37.pyc ├── data │ ├── small_movie_list.csv │ ├── small_movies_R.csv │ ├── small_movies_W.csv │ ├── small_movies_X.csv │ ├── small_movies_Y.csv │ └── small_movies_b.csv ├── images │ ├── ColabFilterLearn.PNG │ ├── ColabFilterUse.PNG │ ├── RecSysNN.png │ ├── distmatrix.PNG │ ├── film_award.png │ ├── film_filter.png │ ├── film_man_action.png │ ├── film_movie_camera.png │ ├── film_rating.png │ ├── film_reel.png │ ├── film_strip_vertical.png │ └── movie_camera.png ├── public_tests.py └── recsys_utils.py ├── Content-Based Filtering ├── C3_W2_RecSysNN_Assignment.ipynb ├── data │ ├── content_item_train.csv │ ├── content_item_train_header.txt │ ├── content_item_vecs.csv │ ├── content_movie_list.csv │ ├── content_user_to_genre.pickle │ ├── content_user_train.csv │ ├── content_user_train_header.txt │ └── content_y_train.csv ├── images │ ├── ColabFilterLearn.PNG │ ├── ColabFilterUse.PNG │ ├── RecSysNN.png │ ├── distmatrix.PNG │ ├── film_award.png │ ├── film_filter.png │ ├── film_man_action.png │ ├── film_movie_camera.png │ ├── film_rating.png │ ├── film_reel.png │ ├── film_strip_vertical.png │ └── movie_camera.png ├── public_tests.py └── recsysNN_utils.py ├── Content-based filtering.jpg ├── Continuous state spaces.jpg ├── Recommender systems implementation.jpg ├── Reinforcement Learning ├── C3_W3_A1_Assignment.ipynb ├── __pycache__ │ ├── public_tests.cpython-37.pyc │ └── utils.cpython-37.pyc ├── images │ ├── deep_q_algorithm.png │ ├── lunar_lander.gif │ └── rl_formalism.png ├── public_tests.py ├── utils.py └── videos │ └── rl_formalism.m4v ├── Reinforcement learning introduction.jpg ├── State-action value function example.ipynb ├── State-action value function.jpg ├── anomaly ├── C3_W1_Anomaly_Detection.ipynb ├── data │ ├── X_part1.npy │ ├── X_part2.npy │ ├── X_val_part1.npy │ ├── X_val_part2.npy │ ├── y_val_part1.npy │ └── y_val_part2.npy ├── images │ ├── figure1.png │ ├── figure2.png │ └── figure3.png ├── public_tests.py └── utils.py └── k-means ├── C3_W1_KMeans_Assignment.ipynb ├── bird_small.png ├── data └── ex7_X.npy ├── images ├── bird_small.png ├── figure 1.png ├── figure 2.png └── figure 3.png ├── public_tests.py └── utils.py /Advanced Learning Algorithms/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": "e8631645d39248c7bba85e5608139136", 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": "d89e83fedc19457bb6a7267d3ba2f530", 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 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Decision trees.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Decision trees.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Neural network implementation in Python.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Neural network implementation in Python.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Activation Functions.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Activation Functions.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Additional Neural Network Concepts.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Additional Neural Network Concepts.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Advice for applying machine learning.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Advice for applying machine learning.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Bias and variance.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Bias and variance.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Decision tree learning.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Decision tree learning.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Machine learning development process.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Machine learning development process.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Multiclass Classification.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Multiclass Classification.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Neural Network Training.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Neural Network Training.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Neural network model.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Neural network model.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Neural networks intuition.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Neural networks intuition.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz TensorFlow implementation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz TensorFlow implementation.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/Practice quiz Tree ensembles.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/Practice quiz Tree ensembles.jpg -------------------------------------------------------------------------------- /Advanced Learning Algorithms/autils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def load_data(): 4 | X = np.load("data/X.npy") 5 | y = np.load("data/y.npy") 6 | X = X[0:1000] 7 | y = y[0:1000] 8 | return X, y 9 | 10 | def load_weights(): 11 | w1 = np.load("data/w1.npy") 12 | b1 = np.load("data/b1.npy") 13 | w2 = np.load("data/w2.npy") 14 | b2 = np.load("data/b2.npy") 15 | return w1, b1, w2, b2 16 | 17 | def sigmoid(x): 18 | return 1. / (1. + np.exp(-x)) 19 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/data/X.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/data/X.npy -------------------------------------------------------------------------------- /Advanced Learning Algorithms/data/y.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/data/y.npy -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_Assign1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_Assign1.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_Assign1_BroadcastIndexes.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_Assign1_BroadcastIndexes.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_Assign1_BroadcastMatrix.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_Assign1_BroadcastMatrix.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_Assign1_Broadcasting.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_Assign1_Broadcasting.gif -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_Assign1_VectorAdd.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_Assign1_VectorAdd.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_CoffeeRoasting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_CoffeeRoasting.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_L3_Lab01_3Neurons.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_L3_Lab01_3Neurons.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_MatrixMatrix.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_MatrixMatrix.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_NeuronsAndLayers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_NeuronsAndLayers.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_RoastingDecision.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_RoastingDecision.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_RoastingNetwork.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_RoastingNetwork.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_VectorMatrix.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_VectorMatrix.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_dense.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_dense.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W1_dense2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W1_dense2.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_Assigment_NN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_Assigment_NN.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_BinaryVsMultiClass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_BinaryVsMultiClass.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_GradDesc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_GradDesc.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_NNSoftmax.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_NNSoftmax.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_ReLU_Graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_ReLU_Graph.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_ReLU_Network.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_ReLU_Network.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_ReLU_Plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_ReLU_Plot.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_ReLu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_ReLu.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_SoftMaxCost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_SoftMaxCost.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_SoftMaxNN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_SoftMaxNN.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_Softmax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_Softmax.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_SoftmaxReg_NN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_SoftmaxReg_NN.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_Softmax_Header.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_Softmax_Header.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_mclass_header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_mclass_header.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_mclass_lab_network.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_mclass_lab_network.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_mclass_layer1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_mclass_layer1.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_mclass_layer2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_mclass_layer2.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_mclass_relu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_mclass_relu.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_smallnetwork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_smallnetwork.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W2_softmax_accurate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W2_softmax_accurate.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W3_BiasVarianceDegree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W3_BiasVarianceDegree.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W3_Compute_Cost_linear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W3_Compute_Cost_linear.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W3_TrainingVsNew.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W3_TrainingVsNew.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W3_TrainingVsNew_Slide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W3_TrainingVsNew_Slide.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W4_degree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W4_degree.png -------------------------------------------------------------------------------- /Advanced Learning Algorithms/images/C2_W4_justright.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Advanced Learning Algorithms/images/C2_W4_justright.PNG -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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/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 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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') -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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/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 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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.") -------------------------------------------------------------------------------- /Advanced Learning Algorithms/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 | -------------------------------------------------------------------------------- /Advanced Learning Algorithms/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn import datasets 3 | 4 | 5 | def load_data(): 6 | iris = datasets.load_iris() 7 | X = iris.data[:, :2] # we only take the first two features. 8 | y = iris.target 9 | 10 | X = X[y != 2] # only two classes 11 | y = y[y != 2] 12 | return X, y -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stanford Machine Learning Specialization 2 | Machine Learning Specialization 3 | ### About this Specialization 4 | 5 | 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. 6 | 7 | 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. 8 | 9 | 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. 10 | 11 | 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.) 12 | 13 | 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. 14 | ### Applied Learning Project 15 | 16 | By the end of this Specialization, you will be ready to: 17 | 18 | 19 | 20 | • Build machine learning models in Python using popular machine learning libraries NumPy and scikit-learn. 21 | 22 | • Build and train supervised machine learning models for prediction and binary classification tasks, including linear regression and logistic regression. 23 | 24 | • Build and train a neural network with TensorFlow to perform multi-class classification. 25 | 26 | • Apply best practices for machine learning development so that your models generalize to data and tasks in the real world. 27 | 28 | • Build and use decision trees and tree ensemble methods, including random forests and boosted trees. 29 | 30 | • Use unsupervised learning techniques for unsupervised learning: including clustering and anomaly detection. 31 | 32 | • Build recommender systems with a collaborative filtering approach and a content-based deep learning method. 33 | 34 | • Build a deep reinforcement learning model. 35 | -------------------------------------------------------------------------------- /Regression and Classification/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 | -------------------------------------------------------------------------------- /Regression and Classification/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 | -------------------------------------------------------------------------------- /Regression and Classification/C1_W3_Lab07_Scikit_Learn_Soln.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3181d386", 6 | "metadata": {}, 7 | "source": [ 8 | "# Ungraded Lab: Logistic Regression using Scikit-Learn\n", 9 | "\n", 10 | "\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "a6c490d5", 16 | "metadata": {}, 17 | "source": [ 18 | "## Goals\n", 19 | "In this lab you will:\n", 20 | "- Train a logistic regression model using scikit-learn.\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "57561f87", 26 | "metadata": {}, 27 | "source": [ 28 | "## Dataset \n", 29 | "Let's start with the same dataset as before." 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "id": "751fdca2", 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "import numpy as np\n", 40 | "\n", 41 | "X = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])\n", 42 | "y = np.array([0, 0, 0, 1, 1, 1])" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "id": "463c90fc", 48 | "metadata": {}, 49 | "source": [ 50 | "## Fit the model\n", 51 | "\n", 52 | "The code below imports the [logistic regression model](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression) from scikit-learn. You can fit this model on the training data by calling `fit` function." 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "id": "66ac000a", 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "LogisticRegression()" 65 | ] 66 | }, 67 | "execution_count": 2, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "from sklearn.linear_model import LogisticRegression\n", 74 | "\n", 75 | "lr_model = LogisticRegression()\n", 76 | "lr_model.fit(X, y)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "26382361", 82 | "metadata": {}, 83 | "source": [ 84 | "## Make Predictions\n", 85 | "\n", 86 | "You can see the predictions made by this model by calling the `predict` function." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 3, 92 | "id": "f1341258", 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "Prediction on training set: [0 0 0 1 1 1]\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "y_pred = lr_model.predict(X)\n", 105 | "\n", 106 | "print(\"Prediction on training set:\", y_pred)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "id": "a5c57c3f", 112 | "metadata": {}, 113 | "source": [ 114 | "## Calculate accuracy\n", 115 | "\n", 116 | "You can calculate this accuracy of this model by calling the `score` function." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 4, 122 | "id": "d74ea592", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "Accuracy on training set: 1.0\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print(\"Accuracy on training set:\", lr_model.score(X, y))" 135 | ] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 3 (ipykernel)", 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.8.8" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 5 159 | } 160 | -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Classification with logistic regression.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Classification with logistic regression.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Cost function for logistic regression.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Cost function for logistic regression.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Gradient descent for logistic regression.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Gradient descent for logistic regression.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Gradient descent in practice.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Gradient descent in practice.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Multiple linear regression.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Multiple linear regression.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Regression.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Regression.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Supervised vs unsupervised learning.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Supervised vs unsupervised learning.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz The problem of overfitting.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz The problem of overfitting.jpg -------------------------------------------------------------------------------- /Regression and Classification/Practice quiz Train the model with gradient descent.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/Practice quiz Train the model with gradient descent.jpg -------------------------------------------------------------------------------- /Regression and Classification/__pycache__/lab_utils_common.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/__pycache__/lab_utils_common.cpython-38.pyc -------------------------------------------------------------------------------- /Regression and Classification/__pycache__/lab_utils_uni.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/__pycache__/lab_utils_uni.cpython-38.pyc -------------------------------------------------------------------------------- /Regression and Classification/data.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 -------------------------------------------------------------------------------- /Regression and Classification/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 | -------------------------------------------------------------------------------- /Regression and Classification/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 | -------------------------------------------------------------------------------- /Regression and Classification/data/ex2data1.txt: -------------------------------------------------------------------------------- 1 | 34.62365962451697,78.0246928153624,0 2 | 30.28671076822607,43.89499752400101,0 3 | 35.84740876993872,72.90219802708364,0 4 | 60.18259938620976,86.30855209546826,1 5 | 79.0327360507101,75.3443764369103,1 6 | 45.08327747668339,56.3163717815305,0 7 | 61.10666453684766,96.51142588489624,1 8 | 75.02474556738889,46.55401354116538,1 9 | 76.09878670226257,87.42056971926803,1 10 | 84.43281996120035,43.53339331072109,1 11 | 95.86155507093572,38.22527805795094,0 12 | 75.01365838958247,30.60326323428011,0 13 | 82.30705337399482,76.48196330235604,1 14 | 69.36458875970939,97.71869196188608,1 15 | 39.53833914367223,76.03681085115882,0 16 | 53.9710521485623,89.20735013750205,1 17 | 69.07014406283025,52.74046973016765,1 18 | 67.94685547711617,46.67857410673128,0 19 | 70.66150955499435,92.92713789364831,1 20 | 76.97878372747498,47.57596364975532,1 21 | 67.37202754570876,42.83843832029179,0 22 | 89.67677575072079,65.79936592745237,1 23 | 50.534788289883,48.85581152764205,0 24 | 34.21206097786789,44.20952859866288,0 25 | 77.9240914545704,68.9723599933059,1 26 | 62.27101367004632,69.95445795447587,1 27 | 80.1901807509566,44.82162893218353,1 28 | 93.114388797442,38.80067033713209,0 29 | 61.83020602312595,50.25610789244621,0 30 | 38.78580379679423,64.99568095539578,0 31 | 61.379289447425,72.80788731317097,1 32 | 85.40451939411645,57.05198397627122,1 33 | 52.10797973193984,63.12762376881715,0 34 | 52.04540476831827,69.43286012045222,1 35 | 40.23689373545111,71.16774802184875,0 36 | 54.63510555424817,52.21388588061123,0 37 | 33.91550010906887,98.86943574220611,0 38 | 64.17698887494485,80.90806058670817,1 39 | 74.78925295941542,41.57341522824434,0 40 | 34.1836400264419,75.2377203360134,0 41 | 83.90239366249155,56.30804621605327,1 42 | 51.54772026906181,46.85629026349976,0 43 | 94.44336776917852,65.56892160559052,1 44 | 82.36875375713919,40.61825515970618,0 45 | 51.04775177128865,45.82270145776001,0 46 | 62.22267576120188,52.06099194836679,0 47 | 77.19303492601364,70.45820000180959,1 48 | 97.77159928000232,86.7278223300282,1 49 | 62.07306379667647,96.76882412413983,1 50 | 91.56497449807442,88.69629254546599,1 51 | 79.94481794066932,74.16311935043758,1 52 | 99.2725269292572,60.99903099844988,1 53 | 90.54671411399852,43.39060180650027,1 54 | 34.52451385320009,60.39634245837173,0 55 | 50.2864961189907,49.80453881323059,0 56 | 49.58667721632031,59.80895099453265,0 57 | 97.64563396007767,68.86157272420604,1 58 | 32.57720016809309,95.59854761387875,0 59 | 74.24869136721598,69.82457122657193,1 60 | 71.79646205863379,78.45356224515052,1 61 | 75.3956114656803,85.75993667331619,1 62 | 35.28611281526193,47.02051394723416,0 63 | 56.25381749711624,39.26147251058019,0 64 | 30.05882244669796,49.59297386723685,0 65 | 44.66826172480893,66.45008614558913,0 66 | 66.56089447242954,41.09209807936973,0 67 | 40.45755098375164,97.53518548909936,1 68 | 49.07256321908844,51.88321182073966,0 69 | 80.27957401466998,92.11606081344084,1 70 | 66.74671856944039,60.99139402740988,1 71 | 32.72283304060323,43.30717306430063,0 72 | 64.0393204150601,78.03168802018232,1 73 | 72.34649422579923,96.22759296761404,1 74 | 60.45788573918959,73.09499809758037,1 75 | 58.84095621726802,75.85844831279042,1 76 | 99.82785779692128,72.36925193383885,1 77 | 47.26426910848174,88.47586499559782,1 78 | 50.45815980285988,75.80985952982456,1 79 | 60.45555629271532,42.50840943572217,0 80 | 82.22666157785568,42.71987853716458,0 81 | 88.9138964166533,69.80378889835472,1 82 | 94.83450672430196,45.69430680250754,1 83 | 67.31925746917527,66.58935317747915,1 84 | 57.23870631569862,59.51428198012956,1 85 | 80.36675600171273,90.96014789746954,1 86 | 68.46852178591112,85.59430710452014,1 87 | 42.0754545384731,78.84478600148043,0 88 | 75.47770200533905,90.42453899753964,1 89 | 78.63542434898018,96.64742716885644,1 90 | 52.34800398794107,60.76950525602592,0 91 | 94.09433112516793,77.15910509073893,1 92 | 90.44855097096364,87.50879176484702,1 93 | 55.48216114069585,35.57070347228866,0 94 | 74.49269241843041,84.84513684930135,1 95 | 89.84580670720979,45.35828361091658,1 96 | 83.48916274498238,48.38028579728175,1 97 | 42.2617008099817,87.10385094025457,1 98 | 99.31500880510394,68.77540947206617,1 99 | 55.34001756003703,64.9319380069486,1 100 | 74.77589300092767,89.52981289513276,1 101 | -------------------------------------------------------------------------------- /Regression and Classification/data/ex2data2.txt: -------------------------------------------------------------------------------- 1 | 0.051267,0.69956,1 2 | -0.092742,0.68494,1 3 | -0.21371,0.69225,1 4 | -0.375,0.50219,1 5 | -0.51325,0.46564,1 6 | -0.52477,0.2098,1 7 | -0.39804,0.034357,1 8 | -0.30588,-0.19225,1 9 | 0.016705,-0.40424,1 10 | 0.13191,-0.51389,1 11 | 0.38537,-0.56506,1 12 | 0.52938,-0.5212,1 13 | 0.63882,-0.24342,1 14 | 0.73675,-0.18494,1 15 | 0.54666,0.48757,1 16 | 0.322,0.5826,1 17 | 0.16647,0.53874,1 18 | -0.046659,0.81652,1 19 | -0.17339,0.69956,1 20 | -0.47869,0.63377,1 21 | -0.60541,0.59722,1 22 | -0.62846,0.33406,1 23 | -0.59389,0.005117,1 24 | -0.42108,-0.27266,1 25 | -0.11578,-0.39693,1 26 | 0.20104,-0.60161,1 27 | 0.46601,-0.53582,1 28 | 0.67339,-0.53582,1 29 | -0.13882,0.54605,1 30 | -0.29435,0.77997,1 31 | -0.26555,0.96272,1 32 | -0.16187,0.8019,1 33 | -0.17339,0.64839,1 34 | -0.28283,0.47295,1 35 | -0.36348,0.31213,1 36 | -0.30012,0.027047,1 37 | -0.23675,-0.21418,1 38 | -0.06394,-0.18494,1 39 | 0.062788,-0.16301,1 40 | 0.22984,-0.41155,1 41 | 0.2932,-0.2288,1 42 | 0.48329,-0.18494,1 43 | 0.64459,-0.14108,1 44 | 0.46025,0.012427,1 45 | 0.6273,0.15863,1 46 | 0.57546,0.26827,1 47 | 0.72523,0.44371,1 48 | 0.22408,0.52412,1 49 | 0.44297,0.67032,1 50 | 0.322,0.69225,1 51 | 0.13767,0.57529,1 52 | -0.0063364,0.39985,1 53 | -0.092742,0.55336,1 54 | -0.20795,0.35599,1 55 | -0.20795,0.17325,1 56 | -0.43836,0.21711,1 57 | -0.21947,-0.016813,1 58 | -0.13882,-0.27266,1 59 | 0.18376,0.93348,0 60 | 0.22408,0.77997,0 61 | 0.29896,0.61915,0 62 | 0.50634,0.75804,0 63 | 0.61578,0.7288,0 64 | 0.60426,0.59722,0 65 | 0.76555,0.50219,0 66 | 0.92684,0.3633,0 67 | 0.82316,0.27558,0 68 | 0.96141,0.085526,0 69 | 0.93836,0.012427,0 70 | 0.86348,-0.082602,0 71 | 0.89804,-0.20687,0 72 | 0.85196,-0.36769,0 73 | 0.82892,-0.5212,0 74 | 0.79435,-0.55775,0 75 | 0.59274,-0.7405,0 76 | 0.51786,-0.5943,0 77 | 0.46601,-0.41886,0 78 | 0.35081,-0.57968,0 79 | 0.28744,-0.76974,0 80 | 0.085829,-0.75512,0 81 | 0.14919,-0.57968,0 82 | -0.13306,-0.4481,0 83 | -0.40956,-0.41155,0 84 | -0.39228,-0.25804,0 85 | -0.74366,-0.25804,0 86 | -0.69758,0.041667,0 87 | -0.75518,0.2902,0 88 | -0.69758,0.68494,0 89 | -0.4038,0.70687,0 90 | -0.38076,0.91886,0 91 | -0.50749,0.90424,0 92 | -0.54781,0.70687,0 93 | 0.10311,0.77997,0 94 | 0.057028,0.91886,0 95 | -0.10426,0.99196,0 96 | -0.081221,1.1089,0 97 | 0.28744,1.087,0 98 | 0.39689,0.82383,0 99 | 0.63882,0.88962,0 100 | 0.82316,0.66301,0 101 | 0.67339,0.64108,0 102 | 1.0709,0.10015,0 103 | -0.046659,-0.57968,0 104 | -0.23675,-0.63816,0 105 | -0.15035,-0.36769,0 106 | -0.49021,-0.3019,0 107 | -0.46717,-0.13377,0 108 | -0.28859,-0.060673,0 109 | -0.61118,-0.067982,0 110 | -0.66302,-0.21418,0 111 | -0.59965,-0.41886,0 112 | -0.72638,-0.082602,0 113 | -0.83007,0.31213,0 114 | -0.72062,0.53874,0 115 | -0.59389,0.49488,0 116 | -0.48445,0.99927,0 117 | -0.0063364,0.99927,0 118 | 0.63265,-0.030612,0 119 | -------------------------------------------------------------------------------- /Regression and Classification/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 | -------------------------------------------------------------------------------- /Regression and Classification/images/C1W1L1_Markdown.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W1L1_Markdown.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W1L1_Run.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W1L1_Run.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W1L1_Tour.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W1L1_Tour.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W3_XW.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W3_XW.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W3_boundary.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W3_boundary.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W3_example2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W3_example2.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W3_mcpredict.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W3_mcpredict.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1W3_trainvpredict.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1W3_trainvpredict.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_L3_S1_Lecture_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_L3_S1_Lecture_b.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_L3_S1_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_L3_S1_model.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_L3_S1_trainingdata.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_L3_S1_trainingdata.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_L3_S2_Lecture_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_L3_S2_Lecture_b.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_L4_S1_Lecture_GD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_L4_S1_Lecture_GD.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_Lab02_GoalOfRegression.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_Lab02_GoalOfRegression.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_Lab03_alpha_too_big.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_Lab03_alpha_too_big.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_Lab03_lecture_learningrate.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_Lab03_lecture_learningrate.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W1_Lab03_lecture_slopes.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W1_Lab03_lecture_slopes.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_L1_S1_Lecture_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_L1_S1_Lecture_b.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_L1_S1_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_L1_S1_model.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_L1_S1_trainingdata.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_L1_S1_trainingdata.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_L1_S2_Lectureb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_L1_S2_Lectureb.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_L2_S1_Lecture_GD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_L2_S1_Lecture_GD.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab02_GoalOfRegression.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab02_GoalOfRegression.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab03_alpha_to_big.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab03_alpha_to_big.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab03_lecture_learningrate.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab03_lecture_learningrate.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab03_lecture_slopes.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab03_lecture_slopes.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab04_Figures And animations.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab04_Figures And animations.pptx -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab04_Matrices.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab04_Matrices.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab04_Vectors.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab04_Vectors.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab04_dot_notrans.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab04_dot_notrans.gif -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab06_LongRun.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab06_LongRun.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab06_ShortRun.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab06_ShortRun.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab06_contours.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab06_contours.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab06_featurescalingheader.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab06_featurescalingheader.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab06_learningrate.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab06_learningrate.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab06_scale.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab06_scale.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W2_Lab07_FeatureEngLecture.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W2_Lab07_FeatureEngLecture.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_Classification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_Classification.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_Lab07_overfitting.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_Lab07_overfitting.PNG -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LinearCostRegularized.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LinearCostRegularized.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LinearGradientRegularized.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LinearGradientRegularized.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticCostRegularized.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticCostRegularized.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticGradientRegularized.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticGradientRegularized.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticLoss_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticLoss_a.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticLoss_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticLoss_b.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticLoss_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticLoss_c.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticRegression.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticRegression.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticRegression_left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticRegression_left.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_LogisticRegression_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_LogisticRegression_right.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_Logistic_gradient_descent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_Logistic_gradient_descent.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_Overfitting_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_Overfitting_a.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_Overfitting_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_Overfitting_b.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_Overfitting_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_Overfitting_c.png -------------------------------------------------------------------------------- /Regression and Classification/images/C1_W3_SqErrorVsLogistic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/C1_W3_SqErrorVsLogistic.png -------------------------------------------------------------------------------- /Regression and Classification/images/figure 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/figure 1.png -------------------------------------------------------------------------------- /Regression and Classification/images/figure 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/figure 2.png -------------------------------------------------------------------------------- /Regression and Classification/images/figure 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/figure 3.png -------------------------------------------------------------------------------- /Regression and Classification/images/figure 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/figure 4.png -------------------------------------------------------------------------------- /Regression and Classification/images/figure 5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/figure 5.png -------------------------------------------------------------------------------- /Regression and Classification/images/figure 6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Regression and Classification/images/figure 6.png -------------------------------------------------------------------------------- /Regression and Classification/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].set_ylabel("loss") 171 | ax[0].set_xlabel(r"$f_{w,b}(x)$") 172 | ax[1].plot(x,-np.log(1-x)) 173 | ax[1].set_title("y = 0") 174 | ax[1].set_xlabel(r"$f_{w,b}(x)$") 175 | ax[0].annotate("prediction \nmatches \ntarget ", xy= [1,0], xycoords='data', 176 | xytext=[-10,30],textcoords='offset points', ha="right", va="center", 177 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 178 | ax[0].annotate("loss increases as prediction\n differs from target", xy= [0.1,-np.log(0.1)], xycoords='data', 179 | xytext=[10,30],textcoords='offset points', ha="left", va="center", 180 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 181 | ax[1].annotate("prediction \nmatches \ntarget ", xy= [0,0], xycoords='data', 182 | xytext=[10,30],textcoords='offset points', ha="left", va="center", 183 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 184 | ax[1].annotate("loss increases as prediction\n differs from target", xy= [0.9,-np.log(1-0.9)], xycoords='data', 185 | xytext=[-10,30],textcoords='offset points', ha="right", va="center", 186 | arrowprops={'arrowstyle': '->', 'color': dlorange, 'lw': 3},) 187 | plt.suptitle("Loss Curves for Two Categorical Target Values", fontsize=12) 188 | plt.tight_layout() 189 | plt.show() 190 | -------------------------------------------------------------------------------- /Regression and Classification/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 | -------------------------------------------------------------------------------- /Regression and Classification/public_tests.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | 4 | def sigmoid_test(target): 5 | assert np.isclose(target(3.0), 0.9525741268224334), "Failed for scalar input" 6 | assert np.allclose(target(np.array([2.5, 0])), [0.92414182, 0.5]), "Failed for 1D array" 7 | assert np.allclose(target(np.array([[2.5, -2.5], [0, 1]])), 8 | [[0.92414182, 0.07585818], [0.5, 0.73105858]]), "Failed for 2D array" 9 | print('\033[92mAll tests passed!') 10 | 11 | def compute_cost_test(target): 12 | X = np.array([[0, 0, 0, 0]]).T 13 | y = np.array([0, 0, 0, 0]) 14 | w = np.array([0]) 15 | b = 1 16 | result = target(X, y, w, b) 17 | if math.isinf(result): 18 | raise ValueError("Did you get the sigmoid of z_wb?") 19 | 20 | np.random.seed(17) 21 | X = np.random.randn(5, 2) 22 | y = np.array([1, 0, 0, 1, 1]) 23 | w = np.random.randn(2) 24 | b = 0 25 | result = target(X, y, w, b) 26 | assert np.isclose(result, 2.15510667), f"Wrong output. Expected: {2.15510667} got: {result}" 27 | 28 | X = np.random.randn(4, 3) 29 | y = np.array([1, 1, 0, 0]) 30 | w = np.random.randn(3) 31 | b = 0 32 | 33 | result = target(X, y, w, b) 34 | assert np.isclose(result, 0.80709376), f"Wrong output. Expected: {0.80709376} got: {result}" 35 | 36 | X = np.random.randn(4, 3) 37 | y = np.array([1, 0,1, 0]) 38 | w = np.random.randn(3) 39 | b = 3 40 | result = target(X, y, w, b) 41 | assert np.isclose(result, 0.4529660647), f"Wrong output. Expected: {0.4529660647} got: {result}. Did you inizialized z_wb = b?" 42 | 43 | print('\033[92mAll tests passed!') 44 | 45 | def compute_gradient_test(target): 46 | np.random.seed(1) 47 | X = np.random.randn(7, 3) 48 | y = np.array([1, 0, 1, 0, 1, 1, 0]) 49 | test_w = np.array([1, 0.5, -0.35]) 50 | test_b = 1.7 51 | dj_db, dj_dw = target(X, y, test_w, test_b) 52 | 53 | assert np.isclose(dj_db, 0.28936094), f"Wrong value for dj_db. Expected: {0.28936094} got: {dj_db}" 54 | assert dj_dw.shape == test_w.shape, f"Wrong shape for dj_dw. Expected: {test_w.shape} got: {dj_dw.shape}" 55 | assert np.allclose(dj_dw, [-0.11999166, 0.41498775, -0.71968405]), f"Wrong values for dj_dw. Got: {dj_dw}" 56 | 57 | print('\033[92mAll tests passed!') 58 | 59 | def predict_test(target): 60 | np.random.seed(5) 61 | b = 0.5 62 | w = np.random.randn(3) 63 | X = np.random.randn(8, 3) 64 | 65 | result = target(X, w, b) 66 | wrong_1 = [1., 1., 0., 0., 1., 0., 0., 1.] 67 | expected_1 = [1., 1., 1., 0., 1., 0., 0., 1.] 68 | if np.allclose(result, wrong_1): 69 | raise ValueError("Did you apply the sigmoid before applying the threshold?") 70 | assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}" 71 | assert np.allclose(result, expected_1), f"Wrong output: Expected : {expected_1} got: {result}" 72 | 73 | b = -1.7 74 | w = np.random.randn(4) + 0.6 75 | X = np.random.randn(6, 4) 76 | 77 | result = target(X, w, b) 78 | expected_2 = [0., 0., 0., 1., 1., 0.] 79 | assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}" 80 | assert np.allclose(result,expected_2), f"Wrong output: Expected : {expected_2} got: {result}" 81 | 82 | print('\033[92mAll tests passed!') 83 | 84 | def compute_cost_reg_test(target): 85 | np.random.seed(1) 86 | w = np.random.randn(3) 87 | b = 0.4 88 | X = np.random.randn(6, 3) 89 | y = np.array([0, 1, 1, 0, 1, 1]) 90 | lambda_ = 0.1 91 | expected_output = target(X, y, w, b, lambda_) 92 | 93 | assert np.isclose(expected_output, 0.5469746792761936), f"Wrong output. Expected: {0.5469746792761936} got:{expected_output}" 94 | 95 | w = np.random.randn(5) 96 | b = -0.6 97 | X = np.random.randn(8, 5) 98 | y = np.array([1, 0, 1, 0, 0, 1, 0, 1]) 99 | lambda_ = 0.01 100 | output = target(X, y, w, b, lambda_) 101 | assert np.isclose(output, 1.2608591964119995), f"Wrong output. Expected: {1.2608591964119995} got:{output}" 102 | 103 | w = np.array([2, 2, 2, 2, 2]) 104 | b = 0 105 | X = np.zeros((8, 5)) 106 | y = np.array([0.5] * 8) 107 | lambda_ = 3 108 | output = target(X, y, w, b, lambda_) 109 | expected = -np.log(0.5) + 3. / (2. * 8.) * 20. 110 | assert np.isclose(output, expected), f"Wrong output. Expected: {expected} got:{output}" 111 | 112 | print('\033[92mAll tests passed!') 113 | 114 | def compute_gradient_reg_test(target): 115 | np.random.seed(1) 116 | w = np.random.randn(5) 117 | b = 0.2 118 | X = np.random.randn(7, 5) 119 | y = np.array([0, 1, 1, 0, 1, 1, 0]) 120 | lambda_ = 0.1 121 | expected1 = (-0.1506447567869257, np.array([ 0.19530838, -0.00632206, 0.19687367, 0.15741161, 0.02791437])) 122 | dj_db, dj_dw = target(X, y, w, b, lambda_) 123 | 124 | assert np.isclose(dj_db, expected1[0]), f"Wrong dj_db. Expected: {expected1[0]} got: {dj_db}" 125 | assert np.allclose(dj_dw, expected1[1]), f"Wrong dj_dw. Expected: {expected1[1]} got: {dj_dw}" 126 | 127 | 128 | w = np.random.randn(7) 129 | b = 0 130 | X = np.random.randn(7, 7) 131 | y = np.array([1, 0, 0, 0, 1, 1, 0]) 132 | lambda_ = 0 133 | expected2 = (0.02660329857573818, np.array([ 0.23567643, -0.06921029, -0.19705212, -0.0002884 , 0.06490588, 134 | 0.26948175, 0.10777992])) 135 | dj_db, dj_dw = target(X, y, w, b, lambda_) 136 | assert np.isclose(dj_db, expected2[0]), f"Wrong dj_db. Expected: {expected2[0]} got: {dj_db}" 137 | assert np.allclose(dj_dw, expected2[1]), f"Wrong dj_dw. Expected: {expected2[1]} got: {dj_dw}" 138 | 139 | print('\033[92mAll tests passed!') 140 | -------------------------------------------------------------------------------- /Regression and Classification/solutions.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def sigmoid(z): 4 | """ 5 | Compute the sigmoid of z 6 | 7 | Parameters 8 | ---------- 9 | z : array_like 10 | A scalar or numpy array of any size. 11 | 12 | Returns 13 | ------- 14 | g : array_like 15 | sigmoid(z) 16 | """ 17 | # (≈ 1 line of code) 18 | # s = 19 | ### START CODE HERE ### (≈ 1 line of code) 20 | s = 1/(1 + np.exp(-z)) 21 | ### END CODE HERE ### 22 | 23 | return s 24 | 25 | 26 | def compute_cost(X, y, w, b): 27 | m = X.shape[0] 28 | 29 | f_w = sigmoid(np.dot(X, w) + b) 30 | total_cost = (1/m)*np.sum(-y*np.log(f_w) - (1-y)*np.log(1-f_w)) 31 | 32 | return float(np.squeeze(total_cost)) 33 | 34 | def compute_gradient(X, y, w, b): 35 | """ 36 | Computes the gradient for logistic regression. 37 | 38 | Parameters 39 | ---------- 40 | X : array_like 41 | Shape (m, n+1) 42 | 43 | y : array_like 44 | Shape (m,) 45 | 46 | w : array_like 47 | Parameters of the model 48 | Shape (n+1,) 49 | b: scalar 50 | 51 | Returns 52 | ------- 53 | dw : array_like 54 | Shape (n+1,) 55 | The gradient 56 | db: scalar 57 | 58 | """ 59 | m = X.shape[0] 60 | f_w = sigmoid(np.dot(X, w) + b) 61 | err = (f_w - y) 62 | dw = (1/m)*np.dot(X.T, err) 63 | db = (1/m)*np.sum(err) 64 | 65 | return float(np.squeeze(db)), dw 66 | 67 | 68 | def predict(X, w, b): 69 | """ 70 | Predict whether the label is 0 or 1 using learned logistic 71 | regression parameters theta 72 | 73 | Parameters 74 | ---------- 75 | X : array_like 76 | Shape (m, n+1) 77 | 78 | w : array_like 79 | Parameters of the model 80 | Shape (n, 1) 81 | b : scalar 82 | 83 | Returns 84 | ------- 85 | 86 | p: array_like 87 | Shape (m,) 88 | The predictions for X using a threshold at 0.5 89 | i.e. if sigmoid (theta.T*X) >=0.5 predict 1 90 | """ 91 | 92 | # number of training examples 93 | m = X.shape[0] 94 | p = np.zeros(m) 95 | 96 | for i in range(m): 97 | f_w = sigmoid(np.dot(w.T, X[i]) + b) 98 | p[i] = f_w >=0.5 99 | 100 | return p 101 | 102 | def compute_cost_reg(X, y, w, b, lambda_=1): 103 | """ 104 | Computes the cost for logistic regression 105 | with regularization 106 | 107 | Parameters 108 | ---------- 109 | X : array_like 110 | Shape (m, n+1) 111 | 112 | y : array_like 113 | Shape (m,) 114 | 115 | w: array_like 116 | Parameters of the model 117 | Shape (n+1,) 118 | b: scalar 119 | 120 | Returns 121 | ------- 122 | cost : float 123 | The cost of using theta as the parameter for logistic 124 | regression to fit the data points in X and y 125 | 126 | """ 127 | # number of training examples 128 | m = X.shape[0] 129 | 130 | # You need to return the following variables correctly 131 | cost = 0 132 | 133 | f = sigmoid(np.dot(X, w) + b) 134 | reg = (lambda_/(2*m)) * np.sum(np.square(w)) 135 | cost = (1/m)*np.sum(-y*np.log(f) - (1-y)*np.log(1-f)) + reg 136 | return cost 137 | 138 | 139 | def compute_gradient_reg(X, y, w, b, lambda_=1): 140 | """ 141 | Computes the gradient for logistic regression 142 | with regularization 143 | 144 | Parameters 145 | ---------- 146 | X : array_like 147 | Shape (m, n+1) 148 | 149 | y : array_like 150 | Shape (m,) 151 | 152 | w : array_like 153 | Parameters of the model 154 | Shape (n+1,) 155 | b : scalar 156 | 157 | Returns 158 | ------- 159 | db: scalar 160 | dw: array_like 161 | Shape (n+1,) 162 | 163 | """ 164 | # number of training examples 165 | m = X.shape[0] 166 | 167 | # You need to return the following variables correctly 168 | cost = 0 169 | dw = np.zeros_like(w) 170 | 171 | f = sigmoid(np.dot(X, w) + b) 172 | err = (f - y) 173 | dw = (1/m)*np.dot(X.T, err) 174 | dw += (lambda_/m) * w 175 | db = (1/m) * np.sum(err) 176 | 177 | #print(db,dw) 178 | 179 | return db,dw 180 | -------------------------------------------------------------------------------- /Regression and Classification/test_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from copy import deepcopy 3 | 4 | 5 | def datatype_check(expected_output, target_output, error): 6 | success = 0 7 | if isinstance(target_output, dict): 8 | for key in target_output.keys(): 9 | try: 10 | success += datatype_check(expected_output[key], 11 | target_output[key], error) 12 | except: 13 | print("Error: {} in variable {}. Got {} but expected type {}".format(error, 14 | key, 15 | type( 16 | target_output[key]), 17 | type(expected_output[key]))) 18 | if success == len(target_output.keys()): 19 | return 1 20 | else: 21 | return 0 22 | elif isinstance(target_output, tuple) or isinstance(target_output, list): 23 | for i in range(len(target_output)): 24 | try: 25 | success += datatype_check(expected_output[i], 26 | target_output[i], error) 27 | except: 28 | print("Error: {} in variable {}, expected type: {} but expected type {}".format(error, 29 | i, 30 | type( 31 | target_output[i]), 32 | type(expected_output[i] 33 | ))) 34 | if success == len(target_output): 35 | return 1 36 | else: 37 | return 0 38 | 39 | else: 40 | assert isinstance(target_output, type(expected_output)) 41 | return 1 42 | 43 | 44 | def equation_output_check(expected_output, target_output, error): 45 | success = 0 46 | if isinstance(target_output, dict): 47 | for key in target_output.keys(): 48 | try: 49 | success += equation_output_check(expected_output[key], 50 | target_output[key], error) 51 | except: 52 | print("Error: {} for variable {}.".format(error, 53 | key)) 54 | if success == len(target_output.keys()): 55 | return 1 56 | else: 57 | return 0 58 | elif isinstance(target_output, tuple) or isinstance(target_output, list): 59 | for i in range(len(target_output)): 60 | try: 61 | success += equation_output_check(expected_output[i], 62 | target_output[i], error) 63 | except: 64 | print("Error: {} for variable in position {}.".format(error, i)) 65 | if success == len(target_output): 66 | return 1 67 | else: 68 | return 0 69 | 70 | else: 71 | if hasattr(target_output, 'shape'): 72 | np.testing.assert_array_almost_equal( 73 | target_output, expected_output) 74 | else: 75 | assert target_output == expected_output 76 | return 1 77 | 78 | 79 | def shape_check(expected_output, target_output, error): 80 | success = 0 81 | if isinstance(target_output, dict): 82 | for key in target_output.keys(): 83 | try: 84 | success += shape_check(expected_output[key], 85 | target_output[key], error) 86 | except: 87 | print("Error: {} for variable {}.".format(error, key)) 88 | if success == len(target_output.keys()): 89 | return 1 90 | else: 91 | return 0 92 | elif isinstance(target_output, tuple) or isinstance(target_output, list): 93 | for i in range(len(target_output)): 94 | try: 95 | success += shape_check(expected_output[i], 96 | target_output[i], error) 97 | except: 98 | print("Error: {} for variable {}.".format(error, i)) 99 | if success == len(target_output): 100 | return 1 101 | else: 102 | return 0 103 | 104 | else: 105 | if hasattr(target_output, 'shape'): 106 | assert target_output.shape == expected_output.shape 107 | return 1 108 | 109 | 110 | def single_test(test_cases, target): 111 | success = 0 112 | for test_case in test_cases: 113 | try: 114 | if test_case['name'] == "datatype_check": 115 | assert isinstance(target(*test_case['input']), 116 | type(test_case["expected"])) 117 | success += 1 118 | if test_case['name'] == "equation_output_check": 119 | assert np.allclose(test_case["expected"], 120 | target(*test_case['input'])) 121 | success += 1 122 | if test_case['name'] == "shape_check": 123 | assert test_case['expected'].shape == target( 124 | *test_case['input']).shape 125 | success += 1 126 | except: 127 | print("Error: " + test_case['error']) 128 | 129 | if success == len(test_cases): 130 | print("\033[92m All tests passed.") 131 | else: 132 | print('\033[92m', success, " Tests passed") 133 | print('\033[91m', len(test_cases) - success, " Tests failed") 134 | raise AssertionError( 135 | "Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__)) 136 | 137 | 138 | def multiple_test(test_cases, target): 139 | success = 0 140 | for test_case in test_cases: 141 | try: 142 | test_input = deepcopy(test_case['input']) 143 | target_answer = target(*test_input) 144 | if test_case['name'] == "datatype_check": 145 | success += datatype_check(test_case['expected'], 146 | target_answer, test_case['error']) 147 | if test_case['name'] == "equation_output_check": 148 | success += equation_output_check( 149 | test_case['expected'], target_answer, test_case['error']) 150 | if test_case['name'] == "shape_check": 151 | success += shape_check(test_case['expected'], 152 | target_answer, test_case['error']) 153 | except: 154 | print('\33[30m', "Error: " + test_case['error']) 155 | 156 | if success == len(test_cases): 157 | print("\033[92m All tests passed.") 158 | else: 159 | print('\033[92m', success, " Tests passed") 160 | print('\033[91m', len(test_cases) - success, " Tests failed") 161 | raise AssertionError( 162 | "Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__)) 163 | 164 | -------------------------------------------------------------------------------- /Regression and Classification/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | def load_data(filename): 5 | data = np.loadtxt(filename, delimiter=',') 6 | X = data[:,:2] 7 | y = data[:,2] 8 | return X, y 9 | 10 | def sig(z): 11 | 12 | return 1/(1+np.exp(-z)) 13 | 14 | def map_feature(X1, X2): 15 | """ 16 | Feature mapping function to polynomial features 17 | """ 18 | X1 = np.atleast_1d(X1) 19 | X2 = np.atleast_1d(X2) 20 | degree = 6 21 | out = [] 22 | for i in range(1, degree+1): 23 | for j in range(i + 1): 24 | out.append((X1**(i-j) * (X2**j))) 25 | return np.stack(out, axis=1) 26 | 27 | 28 | def plot_data(X, y, pos_label="y=1", neg_label="y=0"): 29 | positive = y == 1 30 | negative = y == 0 31 | 32 | # Plot examples 33 | plt.plot(X[positive, 0], X[positive, 1], 'k+', label=pos_label) 34 | plt.plot(X[negative, 0], X[negative, 1], 'yo', label=neg_label) 35 | 36 | 37 | def plot_decision_boundary(w, b, X, y): 38 | # Credit to dibgerge on Github for this plotting code 39 | 40 | plot_data(X[:, 0:2], y) 41 | 42 | if X.shape[1] <= 2: 43 | plot_x = np.array([min(X[:, 0]), max(X[:, 0])]) 44 | plot_y = (-1. / w[1]) * (w[0] * plot_x + b) 45 | 46 | plt.plot(plot_x, plot_y, c="b") 47 | 48 | else: 49 | u = np.linspace(-1, 1.5, 50) 50 | v = np.linspace(-1, 1.5, 50) 51 | 52 | z = np.zeros((len(u), len(v))) 53 | 54 | # Evaluate z = theta*x over the grid 55 | for i in range(len(u)): 56 | for j in range(len(v)): 57 | z[i,j] = sig(np.dot(map_feature(u[i], v[j]), w) + b) 58 | 59 | # important to transpose z before calling contour 60 | z = z.T 61 | 62 | # Plot z = 0 63 | plt.contour(u,v,z, levels = [0.5], colors="g") 64 | 65 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Anomaly detection.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Anomaly detection.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Clustering.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Clustering.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative Filtering.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative Filtering.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/__pycache__/public_tests.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/__pycache__/public_tests.cpython-37.pyc -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/__pycache__/recsys_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/__pycache__/recsys_utils.cpython-37.pyc -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/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/Collaborative/images/ColabFilterLearn.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/ColabFilterLearn.PNG -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/ColabFilterUse.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/ColabFilterUse.PNG -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/RecSysNN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/RecSysNN.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/distmatrix.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/distmatrix.PNG -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_award.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_award.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_filter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_filter.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_man_action.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_man_action.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_movie_camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_movie_camera.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_rating.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_rating.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_reel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_reel.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_strip_vertical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/film_strip_vertical.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/movie_camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/images/movie_camera.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/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 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Collaborative/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 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/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 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/data/content_user_to_genre.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/data/content_user_to_genre.pickle -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/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/Content-Based Filtering/images/ColabFilterLearn.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/ColabFilterLearn.PNG -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/ColabFilterUse.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/ColabFilterUse.PNG -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/RecSysNN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/RecSysNN.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/distmatrix.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/distmatrix.PNG -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_award.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_award.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_filter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_filter.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_man_action.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_man_action.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_movie_camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_movie_camera.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_rating.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_rating.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_reel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_reel.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_strip_vertical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/film_strip_vertical.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/movie_camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/images/movie_camera.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-Based Filtering/public_tests.py: -------------------------------------------------------------------------------- 1 | from tensorflow.keras.activations import relu, linear 2 | from tensorflow.keras.layers import Dense 3 | 4 | import numpy as np 5 | 6 | def test_tower(target): 7 | num_outputs = 32 8 | i = 0 9 | assert len(target.layers) == 3, f"Wrong number of layers. Expected 3 but got {len(target.layers)}" 10 | expected = [[Dense, [None, 256], relu], 11 | [Dense, [None, 128], relu], 12 | [Dense, [None, num_outputs], linear]] 13 | 14 | for layer in target.layers: 15 | assert type(layer) == expected[i][0], \ 16 | f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}" 17 | assert layer.output.shape.as_list() == expected[i][1], \ 18 | f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}" 19 | assert layer.activation == expected[i][2], \ 20 | f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}" 21 | i = i + 1 22 | 23 | print("\033[92mAll tests passed!") 24 | 25 | 26 | def test_sq_dist(target): 27 | a1 = np.array([1.0, 2.0, 3.0]); b1 = np.array([1.0, 2.0, 3.0]) 28 | c1 = target(a1, b1) 29 | a2 = np.array([1.1, 2.1, 3.1]); b2 = np.array([1.0, 2.0, 3.0]) 30 | c2 = target(a2, b2) 31 | a3 = np.array([0, 1]); b3 = np.array([1, 0]) 32 | c3 = target(a3, b3) 33 | a4 = np.array([1, 1, 1, 1, 1]); b4 = np.array([0, 0, 0, 0, 0]) 34 | c4 = target(a4, b4) 35 | 36 | assert np.isclose(c1, 0), f"Wrong value. Expected {0}, got {c1}" 37 | assert np.isclose(c2, 0.03), f"Wrong value. Expected {0.03}, got {c2}" 38 | assert np.isclose(c3, 2), f"Wrong value. Expected {2}, got {c3}" 39 | assert np.isclose(c4, 5), f"Wrong value. Expected {5}, got {c4}" 40 | 41 | print('\033[92mAll tests passed!') 42 | -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Content-based filtering.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Content-based filtering.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Continuous state spaces.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Continuous state spaces.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Recommender systems implementation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Recommender systems implementation.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/__pycache__/public_tests.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/__pycache__/public_tests.cpython-37.pyc -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/images/deep_q_algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/images/deep_q_algorithm.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/images/lunar_lander.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/images/lunar_lander.gif -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/images/rl_formalism.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/images/rl_formalism.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/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/Reinforcement Learning/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/Reinforcement Learning/videos/rl_formalism.m4v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement Learning/videos/rl_formalism.m4v -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement learning introduction.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/Reinforcement learning introduction.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/State-action value function.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/State-action value function.jpg -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_part1.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_part2.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_val_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_val_part1.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_val_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/X_val_part2.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/y_val_part1.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/y_val_part1.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/y_val_part2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/data/y_val_part2.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/images/figure1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/images/figure1.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/images/figure2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/images/figure2.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/images/figure3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/images/figure3.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/public_tests.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | 4 | def select_threshold_test(target): 5 | p_val = np.array([i / 100 for i in range(30)]) 6 | y_val = np.array([1] * 5 + [0] * 25) 7 | 8 | best_epsilon, best_F1 = target(y_val, p_val) 9 | assert np.isclose(best_epsilon, 0.04, atol=0.3 / 1000), f"Wrong best_epsilon. Expected: {0.04} got: {best_epsilon}" 10 | assert best_F1 == 1, f"Wrong best_F1. Expected: 1 got: {best_F1}" 11 | 12 | y_val = np.array([1] * 5 + [0] * 25) 13 | y_val[2] = 0 # Introduce noise 14 | best_epsilon, best_F1 = target(y_val, p_val) 15 | assert np.isclose(best_epsilon, 0.04, atol=0.3 / 1000), f"Wrong best_epsilon. Expected: {0.04} got: {best_epsilon}" 16 | assert np.isclose(best_F1, 0.8888888), f"Wrong best_F1. Expected: 0.8888888 got: {best_F1}" 17 | 18 | p_val = np.array([i / 1000 for i in range(50)]) 19 | y_val = np.array([1] * 8 + [0] * 42) 20 | y_val[5] = 0 21 | index = [*range(50)] 22 | random.shuffle(index) 23 | p_val = p_val[index] 24 | y_val = y_val[index] 25 | 26 | best_epsilon, best_F1 = target(y_val, p_val) 27 | assert np.isclose(best_epsilon, 0.007, atol=0.05 / 1000), f"Wrong best_epsilon. Expected: {0.0070070} got: {best_epsilon}" 28 | assert np.isclose(best_F1, 0.933333333), f"Wrong best_F1. Expected: 0.933333333 got: {best_F1}" 29 | print("\033[92mAll tests passed!") 30 | 31 | def estimate_gaussian_test(target): 32 | np.random.seed(273) 33 | 34 | X = np.array([[1, 1, 1], 35 | [2, 2, 2], 36 | [3, 3, 3]]).T 37 | 38 | mu, var = target(X) 39 | 40 | assert type(mu) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(mu)}" 41 | assert type(var) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(var)}" 42 | 43 | assert mu.shape == (X.shape[1],), f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {mu.shape}" 44 | assert type(var) == np.ndarray, f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {var.shape}" 45 | 46 | assert np.allclose(mu, [1., 2., 3.]), f"Wrong value for mu. Expected: {[1, 2, 3]} got: {mu}" 47 | assert np.allclose(var, [0., 0., 0.]), f"Wrong value for mu. Expected: {[0, 0, 0]} got: {var}" 48 | 49 | X = np.array([[1, 2, 3], 50 | [2, 4, 6], 51 | [3, 6, 9]]).T 52 | 53 | mu, var = target(X) 54 | 55 | assert type(mu) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(mu)}" 56 | assert type(var) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(var)}" 57 | 58 | assert mu.shape == (X.shape[1],), f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {mu.shape}" 59 | assert type(var) == np.ndarray, f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {var.shape}" 60 | 61 | assert np.allclose(mu, [2., 4., 6.]), f"Wrong value for mu. Expected: {[2., 4., 6.]} got: {mu}" 62 | assert np.allclose(var, [2. / 3, 8. / 3., 18. / 3.]), f"Wrong value for mu. Expected: {[2. / 3, 8. / 3., 18. / 3.]} got: {var}" 63 | 64 | 65 | m = 500 66 | X = np.array([np.random.normal(0, 1, m), 67 | np.random.normal(1, 2, m), 68 | np.random.normal(3, 1.5, m)]).T 69 | 70 | mu, var = target(X) 71 | 72 | assert type(mu) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(mu)}" 73 | assert type(var) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(var)}" 74 | 75 | assert mu.shape == (X.shape[1],), f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {mu.shape}" 76 | assert type(var) == np.ndarray, f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {var.shape}" 77 | 78 | assert np.allclose(mu, [0., 1., 3.], atol=0.2), f"Wrong value for mu. Expected: {[0, 1, 3]} got: {mu}" 79 | assert np.allclose(var, np.square([1., 2., 1.5]), atol=0.2), f"Wrong value for mu. Expected: {np.square([1., 2., 1.5])} got: {var}" 80 | 81 | print("\033[92mAll tests passed!") -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/anomaly/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | def load_data(): 5 | X = np.load("data/X_part1.npy") 6 | X_val = np.load("data/X_val_part1.npy") 7 | y_val = np.load("data/y_val_part1.npy") 8 | return X, X_val, y_val 9 | 10 | def load_data_multi(): 11 | X = np.load("data/X_part2.npy") 12 | X_val = np.load("data/X_val_part2.npy") 13 | y_val = np.load("data/y_val_part2.npy") 14 | return X, X_val, y_val 15 | 16 | 17 | def multivariate_gaussian(X, mu, var): 18 | """ 19 | Computes the probability 20 | density function of the examples X under the multivariate gaussian 21 | distribution with parameters mu and var. If var is a matrix, it is 22 | treated as the covariance matrix. If var is a vector, it is treated 23 | as the var values of the variances in each dimension (a diagonal 24 | covariance matrix 25 | """ 26 | 27 | k = len(mu) 28 | 29 | if var.ndim == 1: 30 | var = np.diag(var) 31 | 32 | X = X - mu 33 | p = (2* np.pi)**(-k/2) * np.linalg.det(var)**(-0.5) * \ 34 | np.exp(-0.5 * np.sum(np.matmul(X, np.linalg.pinv(var)) * X, axis=1)) 35 | 36 | return p 37 | 38 | def visualize_fit(X, mu, var): 39 | """ 40 | This visualization shows you the 41 | probability density function of the Gaussian distribution. Each example 42 | has a location (x1, x2) that depends on its feature values. 43 | """ 44 | 45 | X1, X2 = np.meshgrid(np.arange(0, 35.5, 0.5), np.arange(0, 35.5, 0.5)) 46 | Z = multivariate_gaussian(np.stack([X1.ravel(), X2.ravel()], axis=1), mu, var) 47 | Z = Z.reshape(X1.shape) 48 | 49 | plt.plot(X[:, 0], X[:, 1], 'bx') 50 | 51 | if np.sum(np.isinf(Z)) == 0: 52 | plt.contour(X1, X2, Z, levels=10**(np.arange(-20., 1, 3)), linewidths=1) 53 | 54 | # Set the title 55 | plt.title("The Gaussian contours of the distribution fit to the dataset") 56 | # Set the y-axis label 57 | plt.ylabel('Throughput (mb/s)') 58 | # Set the x-axis label 59 | plt.xlabel('Latency (ms)') -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/bird_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/bird_small.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/data/ex7_X.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/data/ex7_X.npy -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/bird_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/bird_small.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/figure 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/figure 1.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/figure 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/figure 2.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/figure 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dennislamcv1/StanfordMachineLearningSpecialization/4ed19b3e6007a6e124629f9641c8fd2a259f9020/Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/images/figure 3.png -------------------------------------------------------------------------------- /Unsupervised Learning, Recommenders, Reinforcement Learning/k-means/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/k-means/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) --------------------------------------------------------------------------------