├── .gitignore ├── 01. Linear Algebra ├── Notations_Used_In_Course.png ├── Week 1 │ ├── 00. Notes │ │ └── Week_1_Notes.pdf │ └── 01. System of Equations │ │ ├── 01. Practice_Quiz_1 │ │ └── 01.png │ │ ├── 02. Practice_Quiz_2 │ │ └── 01.png │ │ ├── 03. Ungraded Labs │ │ ├── C1_W1_Lab_1_introduction_to_numpy_arrays.ipynb │ │ ├── C1_W1_Lab_2_linear_systems_as_matrices.ipynb │ │ ├── images │ │ │ ├── 2x2x2.PNG │ │ │ ├── applesbananas.PNG │ │ │ ├── broadcasting.png │ │ │ ├── hstack.PNG │ │ │ ├── hubble.jpg │ │ │ ├── imagereshaped.PNG │ │ │ ├── numpylogo.png │ │ │ ├── visualization.png │ │ │ ├── vstack.PNG │ │ │ └── x_image.png │ │ ├── quiz.py │ │ └── utils.py │ │ └── 04. Graded Quiz │ │ └── 01.png ├── Week 2 │ ├── 00. Notes │ │ └── Week_2_Notes.pdf │ ├── 01. Elimination │ │ └── 01. Practice Quiz 1 │ │ │ └── Practice Quiz 1.png │ ├── 02. Ungraded Lab │ │ └── C1W2_UGL_solving_linear_systems_3_variables.ipynb │ ├── 03. Row Echelon Form and Rank │ │ └── 01. Graded Quiz │ │ │ └── 01.png │ └── 04. Graded Labs - Gaussian_Elimination │ │ ├── C1W2_Assignment.ipynb │ │ ├── utils.py │ │ └── w2_unittest.py ├── Week 3 │ ├── 00. Notes │ │ └── Week_3_Notes.pdf │ ├── 01. Vector Algebra │ │ ├── 01. Practice Quiz 1 │ │ │ └── Practice Quiz 1.png │ │ └── 02. Practice Lab 1 │ │ │ └── C1W3_UGL_1_vector_operations.ipynb │ ├── 02. Linear Transformation │ │ └── 01. Graded Quiz │ │ │ └── 01.png │ ├── 03. Ungraded Labs │ │ ├── C1W3_UGL_2_matrix_multiplication.ipynb │ │ └── C1W3_UGL_3_linear_transformations.ipynb │ └── 04. Graded Lab │ │ ├── C1W3_Assignment.ipynb │ │ ├── data │ │ ├── image.txt │ │ └── toy_dataset.csv │ │ ├── images │ │ ├── nn_model_linear_regression_multiple.png │ │ └── nn_model_linear_regression_simple.png │ │ ├── utils.py │ │ └── w3_unittest.py └── Week 4 │ ├── 00. Notes │ └── Week_4_Notes.pdf │ ├── 01. Determinants-in-Depth │ └── 01. Practice Quiz │ │ └── 01.png │ ├── 02. EigenValues and EigenVectors │ └── 01. Graded Quiz │ │ └── 01.png │ ├── 03. Programming_Assignment │ ├── 01. Ungraded Lab │ │ ├── C1_W4_Lab_1_Interpreting_eigenvalues_and_eigenvectors.ipynb │ │ ├── images │ │ │ ├── reflection.png │ │ │ ├── rotation.png │ │ │ └── shear_x.png │ │ └── utils.py │ └── 02. Graded Lab │ │ ├── C1W4_Assignment.ipynb │ │ ├── data │ │ ├── cat (1).jpg │ │ ├── cat (10).jpg │ │ ├── cat (11).jpg │ │ ├── cat (12).jpg │ │ ├── cat (13).jpg │ │ ├── cat (14).jpg │ │ ├── cat (15).jpg │ │ ├── cat (16).jpg │ │ ├── cat (17).jpg │ │ ├── cat (18).jpg │ │ ├── cat (19).jpg │ │ ├── cat (2).jpg │ │ ├── cat (20).jpg │ │ ├── cat (21).jpg │ │ ├── cat (22).jpg │ │ ├── cat (23).jpg │ │ ├── cat (24).jpg │ │ ├── cat (25).jpg │ │ ├── cat (26).jpg │ │ ├── cat (27).jpg │ │ ├── cat (28).jpg │ │ ├── cat (29).jpg │ │ ├── cat (3).jpg │ │ ├── cat (30).jpg │ │ ├── cat (31).jpg │ │ ├── cat (32).jpg │ │ ├── cat (33).jpg │ │ ├── cat (34).jpg │ │ ├── cat (35).jpg │ │ ├── cat (36).jpg │ │ ├── cat (37).jpg │ │ ├── cat (38).jpg │ │ ├── cat (39).jpg │ │ ├── cat (4).jpg │ │ ├── cat (40).jpg │ │ ├── cat (41).jpg │ │ ├── cat (42).jpg │ │ ├── cat (43).jpg │ │ ├── cat (44).jpg │ │ ├── cat (45).jpg │ │ ├── cat (46).jpg │ │ ├── cat (47).jpg │ │ ├── cat (48).jpg │ │ ├── cat (49).jpg │ │ ├── cat (5).jpg │ │ ├── cat (50).jpg │ │ ├── cat (51).jpg │ │ ├── cat (52).jpg │ │ ├── cat (53).jpg │ │ ├── cat (54).jpg │ │ ├── cat (55).jpg │ │ ├── cat (6).jpg │ │ ├── cat (7).jpg │ │ ├── cat (8).jpg │ │ └── cat (9).jpg │ │ ├── images │ │ └── shear_transformation.png │ │ ├── support_files │ │ ├── expected_centered_data.npy │ │ ├── expected_eigvals.npy │ │ ├── expected_eigvecs.npy │ │ ├── expected_pca12.npy │ │ ├── expected_pca2.npy │ │ └── imgs_flatten.npy │ │ ├── utils.py │ │ └── w4_unittest.py │ └── README.md ├── 02. Calculus ├── Week 1 │ ├── 00. Notes │ │ └── Week_1.pdf │ ├── 01. Derivatives │ │ └── 01. Practice Quiz 1 │ │ │ └── 01. Ungraded_Quiz.png │ ├── 02. Ungraded Labs │ │ └── C2_W1_Lab_1_differentiation_in_python.ipynb │ ├── 03. Optimization │ │ └── 01. Graded Quiz │ │ │ └── 01.png │ └── 04. Programming Assignment │ │ └── 01. Graded_Labs │ │ ├── C2_W1_Assignment.ipynb │ │ ├── data │ │ └── prices.csv │ │ └── w1_unittest.py ├── Week 2 │ ├── 00. Notes │ │ └── Week_2.pdf │ ├── 01. Gradients │ │ └── 01. Practice Quiz 1 │ │ │ └── 01. Partial_Derivatives_and_Gradient.png │ └── 02. Gradient Descent │ │ ├── 01. Ungraded_Labs │ │ ├── C2_W2_Lab_1_Optimization_Using_Gradient_Descent_in_One_Variable.ipynb │ │ ├── C2_W2_Lab_2_Optimization_Using_Gradient_Descent_in_Two_Variables.ipynb │ │ └── w2_tools.py │ │ ├── 02. Graded_Quiz │ │ └── 03.Partial_Derivatives_and_Gradient_Descent.png │ │ └── 03. Graded_Lab │ │ ├── C2_W2_Assignment.ipynb │ │ ├── data │ │ └── tvmarketing.csv │ │ └── w2_unittest.py └── Week 3 │ ├── 00. Notes │ └── Week_3.pdf │ ├── 01. Optimization_in_Neural_Network │ ├── 01. Ungraded_Labs │ │ ├── C2_W3_Lab_1_Regression_with_Perceptron.ipynb │ │ ├── C2_W3_Lab_2_Classification_with_Perceptron.ipynb │ │ ├── data │ │ │ ├── house_prices_train.csv │ │ │ └── tvmarketing.csv │ │ └── images │ │ │ ├── nn_model_classification_1_layer.png │ │ │ ├── nn_model_linear_regression_multiple.png │ │ │ └── nn_model_linear_regression_simple.png │ └── 02. Practice Quiz 1 │ │ └── 01. Optimization_in_Neural_Networks_Ungraded_Quiz.png │ └── 02. Newton's_Method │ ├── 01. Ungraded_Lab │ ├── C2_W3_Lab_3_Optimization_Using_Newtons_Method.ipynb │ ├── data │ │ ├── house_prices_train.csv │ │ └── tvmarketing.csv │ └── images │ │ ├── nn_model_classification_1_layer.png │ │ ├── nn_model_linear_regression_multiple.png │ │ └── nn_model_linear_regression_simple.png │ ├── 02. Graded_Quiz │ └── 01. Graded_Quiz_Optimization_in_Neural_Network_and_Newton_Method.png │ └── 03. Graded_Lab │ ├── C2_W3_Assignment.ipynb │ ├── images │ └── nn_model_2_layers.png │ └── w3_unittest.py ├── 03. Probability and Statistics ├── Week 1 │ ├── 00. Notes │ │ └── Week1_Notes.pdf │ ├── 01. Introduction to Probability │ │ └── 01. Practice Week │ │ │ └── Quiz.png │ ├── 02. Probability Distribution │ │ ├── 01. Labs │ │ │ ├── intro-to-pandas-world-happiness.ipynb │ │ │ └── linear-regression-world-happiness.ipynb │ │ └── 02. Graded Quiz │ │ │ └── Quiz.png │ └── 03. Programming Assignment │ │ ├── C3W1_Assignment.ipynb │ │ ├── emails.csv │ │ ├── test_cases_get_word_frequency.joblib │ │ └── w1_unittest.py ├── Week 2 │ ├── 00. Notes │ │ └── Week_2_Notes.pdf │ ├── 01. Describing Distributions │ │ └── 01. Practice Quiz │ │ │ └── Quiz.png │ ├── 02. Probability Distributions with Multiple Variables │ │ ├── 01. Labs │ │ │ ├── 01. Summary Statistics │ │ │ │ ├── C2W2_UGL_datasets.ipynb │ │ │ │ ├── datasaurus.csv │ │ │ │ ├── df_anscombe.csv │ │ │ │ └── utils.py │ │ │ └── 02.Exploratory Data │ │ │ │ ├── Rideshare_Project_Week2.ipynb │ │ │ │ └── utils.py │ │ └── 02. Graded Quiz │ │ │ └── Graded_Quiz.png │ └── 03. Programming Assignment │ │ ├── C3W2_UGL.ipynb │ │ └── Graded_Assignment │ │ ├── C3_W2_Assignment.ipynb │ │ ├── answers.json │ │ └── utils.py ├── Week 3 │ ├── 00. Notes │ │ └── Notes_Week3.pdf │ ├── 01. Population and Sample │ │ └── 01. Practice Quiz │ │ │ └── PQuiz.png │ └── 02. Point Estimation │ │ ├── 01. Lab │ │ ├── linear-regression-world-happiness.ipynb │ │ └── utils.py │ │ └── 02. Graded Quiz │ │ └── GQuiz.png └── Week 4 │ ├── 00. Notes │ └── Notes_Week4.pdf │ ├── 01. Confidence Interval │ └── 01. Practice Quiz │ │ └── PQuiz.png │ ├── 02. Hypothesis Testing │ ├── 01. Lab │ │ ├── Rideshare_Project_Week4.ipynb │ │ └── utils.py │ └── 02. Graded Quiz │ │ └── GQuiz.png │ └── 03. Graded Lab │ ├── .ipynb_checkpoints │ └── C3W4_Assignment-checkpoint.ipynb │ ├── C3W4_Assignment.ipynb │ ├── __pycache__ │ ├── utils.cpython-310.pyc │ └── w4_unittest.cpython-310.pyc │ ├── background_color_experiment.csv │ └── w4_unittest.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | 01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_cov_mat.npy 3 | -------------------------------------------------------------------------------- /01. Linear Algebra/Notations_Used_In_Course.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Notations_Used_In_Course.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/00. Notes/Week_1_Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/00. Notes/Week_1_Notes.pdf -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/01. Practice_Quiz_1/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/01. Practice_Quiz_1/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/02. Practice_Quiz_2/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/02. Practice_Quiz_2/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/2x2x2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/2x2x2.PNG -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/applesbananas.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/applesbananas.PNG -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/broadcasting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/broadcasting.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/hstack.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/hstack.PNG -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/hubble.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/hubble.jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/imagereshaped.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/imagereshaped.PNG -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/numpylogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/numpylogo.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/visualization.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/visualization.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/vstack.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/vstack.PNG -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/x_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/images/x_image.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/quiz.py: -------------------------------------------------------------------------------- 1 | import ipywidgets as widgets 2 | from ipywidgets import interact, Dropdown 3 | 4 | 5 | question1 = "\033[1mSelect one of the options given:" 6 | solution1 = {'A':'\033[91mNot quite. While both functions are used to create empty arrays, np.zeros() is initialized with the value 0.', 7 | 'B':"\033[91mNot quite. np.zeros() is inialized, and it gives an output of 0's.", 8 | 'C':"\033[91mNot quite. Most often, np.empty() is faster since it is not initialized.", 9 | 'D':''' \033[92mTrue! np.empty() creates an array with uninitialized elements from available memory space and may be faster to execute.'''} 10 | 11 | 12 | 13 | def mcq(question, solution): 14 | s = '' 15 | # print(question) 16 | print("\033[1mPlease select the correct option:") 17 | answer_w = Dropdown(options = solution.keys(), value=None, layout=widgets.Layout(width='25%')) 18 | 19 | @interact(Answer = answer_w) 20 | def print_city(Answer): 21 | if(Answer != None): 22 | s = solution[Answer] 23 | # print("\n") 24 | print(s) 25 | -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/03. Ungraded Labs/utils.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | def plot_lines(M): 5 | x_1 = np.linspace(-10,10,100) 6 | x_2_line_1 = (M[0,2] - M[0,0] * x_1) / M[0,1] 7 | x_2_line_2 = (M[1,2] - M[1,0] * x_1) / M[1,1] 8 | 9 | _, ax = plt.subplots(figsize=(10, 10)) 10 | ax.plot(x_1, x_2_line_1, '-', linewidth=2, color='#0075ff', 11 | label=f'$x_2={-M[0,0]/M[0,1]:.2f}x_1 + {M[0,2]/M[0,1]:.2f}$') 12 | ax.plot(x_1, x_2_line_2, '-', linewidth=2, color='#ff7300', 13 | label=f'$x_2={-M[1,0]/M[1,1]:.2f}x_1 + {M[1,2]/M[1,1]:.2f}$') 14 | 15 | A = M[:, 0:-1] 16 | b = M[:, -1::].flatten() 17 | d = np.linalg.det(A) 18 | 19 | if d != 0: 20 | solution = np.linalg.solve(A,b) 21 | ax.plot(solution[0], solution[1], '-o', mfc='none', 22 | markersize=10, markeredgecolor='#ff0000', markeredgewidth=2) 23 | ax.text(solution[0]-0.25, solution[1]+0.75, f'$(${solution[0]:.0f}$,{solution[1]:.0f})$', fontsize=14) 24 | ax.tick_params(axis='x', labelsize=14) 25 | ax.tick_params(axis='y', labelsize=14) 26 | ax.set_xticks(np.arange(-10, 10)) 27 | ax.set_yticks(np.arange(-10, 10)) 28 | 29 | plt.xlabel('$x_1$', size=14) 30 | plt.ylabel('$x_2$', size=14) 31 | plt.legend(loc='upper right', fontsize=14) 32 | plt.axis([-10, 10, -10, 10]) 33 | 34 | plt.grid() 35 | plt.gca().set_aspect("equal") 36 | 37 | plt.show() -------------------------------------------------------------------------------- /01. Linear Algebra/Week 1/01. System of Equations/04. Graded Quiz/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 1/01. System of Equations/04. Graded Quiz/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 2/00. Notes/Week_2_Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 2/00. Notes/Week_2_Notes.pdf -------------------------------------------------------------------------------- /01. Linear Algebra/Week 2/01. Elimination/01. Practice Quiz 1/Practice Quiz 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 2/01. Elimination/01. Practice Quiz 1/Practice Quiz 1.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 2/02. Ungraded Lab/C1W2_UGL_solving_linear_systems_3_variables.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "signed-sight", 6 | "metadata": {}, 7 | "source": [ 8 | "# Introduction to the Numpy.linalg sub-library" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "representative-velvet", 14 | "metadata": {}, 15 | "source": [ 16 | "In this lab, you will keep developing your skills using Python to solve systems of linear equations by using the sub-libraby [numpy.linalg](https://numpy.org/doc/stable/reference/routines.linalg.html). In this notebook you will:\n", 17 | "\n", 18 | "- Use `NumPy` linear algebra package to find the solutions to the system of linear equations\n", 19 | "- Evaluate the determinant of the matrix to see again the connection between matrix singularity and the number of solutions of the linear system\n", 20 | "- Explore the numpy.linalg sub-library to get to know its properties" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "automated-bishop", 26 | "metadata": {}, 27 | "source": [ 28 | "# Table of Contents\n", 29 | "- [ 1 - Representing and Solving a System of Linear Equations using Matrices](#1)\n", 30 | " - [ 1.1 - System of Linear Equations](#1.1)\n", 31 | " - [ 1.2 - Solving Systems of Linear Equations using Matrices](#1.2)\n", 32 | " - [ 1.3 - Evaluating the Determinant of a Matrix](#1.3)\n", 33 | " - [ 1.4 - What happens if the system has no unique solution?](#1.4)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "persistent-sweden", 39 | "metadata": {}, 40 | "source": [ 41 | "## Packages\n", 42 | "\n", 43 | "Load the `NumPy` package to access its functions." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 1, 49 | "id": "metropolitan-shape", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "import numpy as np" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "id": "chief-istanbul", 59 | "metadata": {}, 60 | "source": [ 61 | "\n", 62 | "## 1 - Representing and Solving a System of Linear Equations using Matrices" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "advance-count", 68 | "metadata": {}, 69 | "source": [ 70 | "\n", 71 | "### 1.1 - System of Linear Equations\n", 72 | "\n", 73 | "Here is a **system of linear equations** (or **linear system**) with three equations and three unknown variables:\n", 74 | "\n", 75 | "\n", 76 | "$$\\begin{cases} \n", 77 | "4x_1-3x_2+x_3=-10, \\\\ 2x_1+x_2+3x_3=0, \\\\ -x_1+2x_2-5x_3=17, \\end{cases}\\tag{1}$$\n", 78 | "\n", 79 | "**To solve** this system of linear equations means to find such values of the variables $x_1$, $x_2$, $x_3$, that all of its equations are simultaneously satisfied." 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "sharp-strike", 85 | "metadata": {}, 86 | "source": [ 87 | "\n", 88 | "### 1.2 - Solving Systems of Linear Equations using Matrices\n", 89 | "\n", 90 | "Let's prepare to solve the linear system $(1)$ using `NumPy`. $A$ will be a matrix, each row will represent one equation in the system and each column will correspond to the variable $x_1$, $x_2$, $x_3$. And $b$ is a 1-D array of the free (right side) coefficients:" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 2, 96 | "id": "tired-filing", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "Matrix A:\n", 104 | "[[ 4. -3. 1.]\n", 105 | " [ 2. 1. 3.]\n", 106 | " [-1. 2. -5.]]\n", 107 | "\n", 108 | "Array b:\n", 109 | "[-10. 0. 17.]\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "A = np.array([\n", 115 | " [4, -3, 1],\n", 116 | " [2, 1, 3],\n", 117 | " [-1, 2, -5]\n", 118 | " ], dtype=np.dtype(float))\n", 119 | "\n", 120 | "b = np.array([-10, 0, 17], dtype=np.dtype(float))\n", 121 | "\n", 122 | "print(\"Matrix A:\")\n", 123 | "print(A)\n", 124 | "print(\"\\nArray b:\")\n", 125 | "print(b)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "id": "premium-soviet", 131 | "metadata": {}, 132 | "source": [ 133 | "Check the dimensions of $A$ and $b$ using `shape()` function:" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 3, 139 | "id": "political-aberdeen", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "Shape of A: (3, 3)\n", 147 | "Shape of b: (3,)\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "print(f\"Shape of A: {np.shape(A)}\")\n", 153 | "print(f\"Shape of b: {np.shape(b)}\")" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "id": "complimentary-testimony", 159 | "metadata": {}, 160 | "source": [ 161 | "Now use `np.linalg.solve(A, b)` function to find the solution of the system $(1)$. The result will be saved in the 1-D array $x$. The elements will correspond to the values of $x_1$, $x_2$ and $x_3$:" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 4, 167 | "id": "broke-vietnamese", 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "Solution: [ 1. 4. -2.]\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "x = np.linalg.solve(A, b)\n", 180 | "\n", 181 | "print(f\"Solution: {x}\")" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "id": "correct-insert", 187 | "metadata": {}, 188 | "source": [ 189 | "Try to substitute those values of $x_1$, $x_2$ and $x_3$ into the original system of equations to check its consistency." 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "id": "applicable-senator", 195 | "metadata": {}, 196 | "source": [ 197 | "\n", 198 | "### 1.3 - Evaluating the Determinant of a Matrix\n", 199 | "\n", 200 | "Matrix $A$ corresponding to the linear system $(1)$ is a **square matrix** - it has the same number of rows and columns. In the case of a square matrix it is possible to calculate its determinant - a real number that characterizes some properties of the matrix. A linear system containing three equations with three unknown variables will have one solution if and only if the matrix $A$ has a non-zero determinant.\n", 201 | "\n", 202 | "Let's calculate the determinant using `np.linalg.det(A)` function:" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 5, 208 | "id": "median-potato", 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "name": "stdout", 213 | "output_type": "stream", 214 | "text": [ 215 | "Determinant of matrix A: -60.00\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "d = np.linalg.det(A)\n", 221 | "\n", 222 | "print(f\"Determinant of matrix A: {d:.2f}\")" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "id": "congressional-church", 228 | "metadata": {}, 229 | "source": [ 230 | "Please, note that its value is non-zero, as expected." 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "id": "stupid-directory", 236 | "metadata": {}, 237 | "source": [ 238 | "\n", 239 | "### 1.4 - What happens if the system has no unique solution?\n", 240 | "\n", 241 | "Let's explore what happens if we use `np.linalg.solve` in a system with no unique solution (no solution at all or infinitely many solutions).\n", 242 | "\n", 243 | "Given another system of linear equations:\n", 244 | "\n", 245 | "$$\\begin{cases} \n", 246 | "x_1+x_2+x_3=2, \\\\ x_2-3x_3=1, \\\\ 2x_1+x_2+5x_3=0, \\end{cases}\\tag{2}$$\n", 247 | "\n", 248 | "let's find the determinant of the corresponding matrix." 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 10, 254 | "id": "great-collector", 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "ename": "LinAlgError", 259 | "evalue": "Singular matrix", 260 | "output_type": "error", 261 | "traceback": [ 262 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 263 | "\u001b[0;31mLinAlgError\u001b[0m Traceback (most recent call last)", 264 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mb_2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA_2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb_2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 265 | "\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36msolve\u001b[0;34m(*args, **kwargs)\u001b[0m\n", 266 | "\u001b[0;32m/opt/conda/lib/python3.8/site-packages/numpy/linalg/linalg.py\u001b[0m in \u001b[0;36msolve\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[0msignature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'DD->D'\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misComplexType\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;34m'dd->d'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 392\u001b[0m \u001b[0mextobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_linalg_error_extobj\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_raise_linalgerror_singular\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 393\u001b[0;31m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgufunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msignature\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msignature\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mextobj\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mextobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 394\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 395\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult_t\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 267 | "\u001b[0;32m/opt/conda/lib/python3.8/site-packages/numpy/linalg/linalg.py\u001b[0m in \u001b[0;36m_raise_linalgerror_singular\u001b[0;34m(err, flag)\u001b[0m\n\u001b[1;32m 86\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_raise_linalgerror_singular\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflag\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 88\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mLinAlgError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Singular matrix\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 89\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 90\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_raise_linalgerror_nonposdef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflag\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 268 | "\u001b[0;31mLinAlgError\u001b[0m: Singular matrix" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "A_2= np.array([\n", 274 | " [1, 1, 1],\n", 275 | " [0, 1, -3],\n", 276 | " [2, 1, 5]\n", 277 | " ], dtype=np.dtype(float))\n", 278 | "\n", 279 | "b_2 = np.array([2, 1, 0], dtype=np.dtype(float))\n", 280 | "\n", 281 | "print(np.linalg.solve(A_2, b_2))" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "id": "false-assault", 287 | "metadata": {}, 288 | "source": [ 289 | "As you can see, it throws a `LinAlgError` because the matrix is singular. You can check it by yourself by computing the determinant of the matrix:" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 9, 295 | "id": "false-seeking", 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "name": "stdout", 300 | "output_type": "stream", 301 | "text": [ 302 | "Determinant of matrix A_2: -28.00\n" 303 | ] 304 | } 305 | ], 306 | "source": [ 307 | "d_2 = np.linalg.det(A_2)\n", 308 | "\n", 309 | "print(f\"Determinant of matrix A_2: {d_2:.2f}\")" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "id": "accredited-force", 315 | "metadata": {}, 316 | "source": [ 317 | "The sub-library np.linalg has several linear algebra functions to help linear algebra tasks and we have exhausted so far the functions you have learned in class. \n", 318 | "\n", 319 | "Once you learn more theory, the functions in this library will become clearer. You also will be using them in the assignments in week 3 and 4, but do not worry because you will be guided through them.\n", 320 | "\n", 321 | "Well done! You used the `NumPy` functions to solve a system of equations. As expected, using a predefined function is much easier, but gives much less insight into what is happening under the hood. **This is why the next assignment will be in Gaussian Elimination, a method to solve linear systems**. Remember that `np.linalg.solve` gives an error if there are no or infinitely many solutions, thus when implementing it you will have to think carefully so not to make your program crash!" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "id": "synthetic-parliament", 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [] 331 | } 332 | ], 333 | "metadata": { 334 | "kernelspec": { 335 | "display_name": "Python 3", 336 | "language": "python", 337 | "name": "python3" 338 | }, 339 | "language_info": { 340 | "codemirror_mode": { 341 | "name": "ipython", 342 | "version": 3 343 | }, 344 | "file_extension": ".py", 345 | "mimetype": "text/x-python", 346 | "name": "python", 347 | "nbconvert_exporter": "python", 348 | "pygments_lexer": "ipython3", 349 | "version": "3.8.8" 350 | } 351 | }, 352 | "nbformat": 4, 353 | "nbformat_minor": 5 354 | } 355 | -------------------------------------------------------------------------------- /01. Linear Algebra/Week 2/03. Row Echelon Form and Rank/01. Graded Quiz/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 2/03. Row Echelon Form and Rank/01. Graded Quiz/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 2/04. Graded Labs - Gaussian_Elimination/utils.py: -------------------------------------------------------------------------------- 1 | from sympy import symbols, Eq, Matrix, solve, sympify 2 | import numpy as np 3 | 4 | def string_to_augmented_matrix(equations): 5 | # Split the input string into individual equations 6 | equation_list = equations.split('\n') 7 | equation_list = [x for x in equation_list if x != ''] 8 | # Create a list to store the coefficients and constants 9 | coefficients = [] 10 | 11 | ss = '' 12 | for c in equations: 13 | if c in 'abcdefghijklmnopqrstuvwxyz': 14 | if c not in ss: 15 | ss += c + ' ' 16 | ss = ss[:-1] 17 | 18 | # Create symbols for variables x, y, z, etc. 19 | variables = symbols(ss) 20 | # Parse each equation and extract coefficients and constants 21 | for equation in equation_list: 22 | # Remove spaces and split into left and right sides 23 | sides = equation.replace(' ', '').split('=') 24 | 25 | # Parse the left side using SymPy's parser 26 | left_side = sympify(sides[0]) 27 | 28 | # Extract coefficients for variables 29 | coefficients.append([left_side.coeff(variable) for variable in variables]) 30 | 31 | # Append the constant term 32 | coefficients[-1].append(int(sides[1])) 33 | 34 | # Create a matrix from the coefficients 35 | augmented_matrix = Matrix(coefficients) 36 | augmented_matrix = np.array(augmented_matrix).astype("float64") 37 | 38 | A, B = augmented_matrix[:,:-1], augmented_matrix[:,-1].reshape(-1,1) 39 | 40 | return ss, A, B 41 | 42 | -------------------------------------------------------------------------------- /01. Linear Algebra/Week 2/04. Graded Labs - Gaussian_Elimination/w2_unittest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy import array 3 | 4 | def test_row_echelon_form(target_function): 5 | 6 | successful_cases = 0 7 | failed_cases = [] 8 | 9 | test_cases = [{'name': 'check_null_matrix', 10 | 'input': {'A': array([[0, 0, 0], 11 | [0, 0, 0], 12 | [0, 0, 0]]), 13 | 'B': array([[0], 14 | [0], 15 | [0]])}, 16 | 'expected': 'Singular system'}, 17 | {'name': 'check_identity_matrix', 18 | 'input': {'A': array([[1., 0., 0.], 19 | [0., 1., 0.], 20 | [0., 0., 1.]]), 21 | 'B': array([[0.], 22 | [0.], 23 | [0.]])}, 24 | 'expected': array([[1., 0., 0., 0.], 25 | [0., 1., 0., 0.], 26 | [0., 0., 1., 0.]])}, 27 | {'name': 'check_matrix_1', 28 | 'input': {'A': array([[1., 2., 3.], 29 | [5., 0., 2.], 30 | [1., 4., 5.]]), 31 | 'B': array([[4.], 32 | [3.], 33 | [6.]])}, 34 | 'expected': array([[ 1. , 2. , 3. , 4. ], 35 | [-0. , 1. , 1.3 , 1.7 ], 36 | [-0. , -0. , 1. , 2.33333333]])}, 37 | {'name': 'check_matrix_2', 38 | 'input': {'A': array([[ 1., 5., 6.], 39 | [ 3., 1., 4.], 40 | [ 2., -4., -2.]]), 41 | 'B': array([[ 9.], 42 | [ 4.], 43 | [-5.]])}, 44 | 'expected': 'Singular system'}] 45 | 46 | successful_cases = 0 47 | failed_cases = [] 48 | for test_case in test_cases: 49 | 50 | try: 51 | target_output = target_function(**test_case["input"]) 52 | except Exception as e: 53 | print("\033[91m", f'An exception was thrown while running your function: {e}.\nInput matrix:\n{test_case["input"]}') 54 | return 55 | 56 | 57 | try: 58 | if isinstance(test_case['expected'],str): 59 | assert isinstance(target_output, str) 60 | else: 61 | assert np.allclose(target_output, test_case['expected']) 62 | successful_cases += 1 63 | except: 64 | failed_cases.append( 65 | { 66 | "name": test_case["name"], 67 | "expected": test_case["expected"], 68 | "got": target_output, 69 | } 70 | ) 71 | print( 72 | f"Wrong output for test case {test_case['name']}. \n\tExpected:\n\t {failed_cases[-1].get('expected')}.\n\tGot:\n {failed_cases[-1].get('got')}." 73 | ) 74 | 75 | if len(failed_cases) == 0: 76 | print("\033[92m All tests passed") 77 | else: 78 | print("\033[92m", successful_cases, " Tests passed") 79 | print("\033[91m", len(failed_cases), " Tests failed") 80 | 81 | 82 | def test_back_substitution(target_function): 83 | 84 | successful_cases = 0 85 | failed_cases = [] 86 | 87 | test_cases = [ 88 | { 89 | "name": "check_null_matrix", 90 | "input": np.array( 91 | [ 92 | [1, 0, 0, 5], 93 | [0, 1, 0, 6], 94 | [0, 0, 1, 7] 95 | ] 96 | ), 97 | "expected": np.array([5, 6, 7]) 98 | }, 99 | { 100 | "name": "check_matrix_1", 101 | "input": np.array([ 102 | [ 1. , 2. , 3. , 4. ], 103 | [-0. , 1. , 1.3 , 1.7 ], 104 | [-0. , -0. , 1. , 2.33333333]]), 105 | "expected": np.array([-0.33333333, -1.33333333, 2.33333333]) 106 | }, 107 | 108 | { 109 | "name": "check_matrix_2", 110 | "input": np.array([ 111 | [ 1. , 5. , 6. , 9. ], 112 | [-0. , 1. , 1. , 1.64285714], 113 | [ 0. , 0. , 1. , 0. ]]), 114 | "expected": np.array([0.7857143 , 1.64285714, 0. ]) 115 | }, 116 | { 117 | "name": "check_matrix_3", 118 | "input": np.array([ 119 | [ 1. , 8. , 6. , 9. ], 120 | [0. , 1 , 8 , 6], 121 | [ 0. , 0. , 1. , 1. ]]), 122 | "expected": np.array([19., -2., 1.]) 123 | } 124 | ] 125 | 126 | 127 | for test_case in test_cases: 128 | 129 | try: 130 | target_output = target_function(test_case["input"]) 131 | except Exception as e: 132 | print("\033[91m", f'An exception was thrown while running your function: {e}.\nInput matrix:\n{test_case["input"]}') 133 | return 134 | 135 | 136 | try: 137 | assert np.allclose(target_output, test_case['expected']) 138 | successful_cases += 1 139 | except: 140 | failed_cases.append( 141 | { 142 | "name": test_case["name"], 143 | "expected": test_case["expected"], 144 | "got": target_output, 145 | } 146 | ) 147 | print( 148 | f"Wrong output for test case {test_case['name']}. \n\tExpected:\n\t {failed_cases[-1].get('expected')}.\n\tGot:\n {failed_cases[-1].get('got')}." 149 | ) 150 | 151 | if len(failed_cases) == 0: 152 | print("\033[92m All tests passed") 153 | else: 154 | print("\033[92m", successful_cases, " Tests passed") 155 | print("\033[91m", len(failed_cases), " Tests failed") 156 | 157 | def test_gaussian_elimination(target_function): 158 | 159 | successful_cases = 0 160 | failed_cases = [] 161 | 162 | test_cases = [{'name': 'test_matrix_0', 163 | 'input': {'A': array([[-9, -1, -5], 164 | [-9, -9, -8], 165 | [ 0, -3, -8]]), 166 | 'B': array([[1], 167 | [4], 168 | [8]])}, 169 | 'expected': array([ 0.44444444, 0. , -1. ])}, 170 | {'name': 'test_matrix_1', 171 | 'input': {'A': array([[ 1, 7, -5, -8], 172 | [ 0, -5, -1, 9], 173 | [ 6, -6, 0, 5], 174 | [ 7, -6, 8, 9]]), 175 | 'B': array([[-8], 176 | [ 4], 177 | [ 9], 178 | [ 5]])}, 179 | 'expected': array([ 0.0279965 , -2.07567804, -0.14129484, -0.72440945])}, 180 | {'name': 'test_matrix_2', 181 | 'input': {'A': array([[ -9, -9, -2, 3, -10], 182 | [ 6, 5, 6, 2, 7], 183 | [ -6, -2, 5, 5, 7], 184 | [ -4, -5, -6, -9, 4], 185 | [ 3, -6, -2, -8, -8]]), 186 | 'B': array([[ 3], 187 | [-4], 188 | [ 1], 189 | [-2], 190 | [-3]])}, 191 | 'expected': array([-2.24012291, 2.29784899, 1.39727831, -1.46641791, -1.0713345 ])}, 192 | {'name': 'test_matrix_3', 193 | 'input': {'A': array([[ -8, -9, -10, -9, 7, -4], 194 | [ 2, 8, 8, 3, -8, -2], 195 | [ -4, 0, 3, 0, 2, 9], 196 | [ 2, -6, -8, -2, -8, 9], 197 | [ -2, -1, -3, 3, 4, -3], 198 | [-10, 1, -5, -6, 3, -4]]), 199 | 'B': array([[ 1], 200 | [-6], 201 | [-4], 202 | [-1], 203 | [-1], 204 | [ 9]])}, 205 | 'expected': array([ 1.66947817, 3.32226171, -2.22881748, -1.60512025, 1.48105316, 206 | 0.71136209])}, 207 | {'name': 'test_matrix_4', 208 | 'input': {'A': array([[-10, 8, -8, -6, 4, 2, -2], 209 | [ 3, 1, 6, 2, -3, -5, -10], 210 | [ 4, 9, -9, 1, 2, -9, -7], 211 | [ -2, -9, 1, 9, 6, -2, -2], 212 | [ -3, -5, -10, 9, 4, 1, -10], 213 | [ 6, 8, 7, -3, 6, -6, 8], 214 | [ 4, -8, 5, -4, 1, 3, 6]]), 215 | 'B': array([[ 0], 216 | [-5], 217 | [ 2], 218 | [-6], 219 | [ 5], 220 | [ 4], 221 | [-2]])}, 222 | 'expected': array([ 1.28049215, 1.12547489, -0.17505018, 0.74172351, 0.64510371, 223 | 1.98786815, -0.14745546])}] 224 | 225 | for test_case in test_cases: 226 | 227 | try: 228 | target_output = target_function(**test_case["input"]) 229 | except Exception as e: 230 | print("\033[91m", f'An exception was thrown while running your function: {e}.\nInput matrix:\n{test_case["input"]}') 231 | return 232 | 233 | 234 | try: 235 | assert np.allclose(target_output, test_case['expected']) 236 | successful_cases += 1 237 | except: 238 | failed_cases.append( 239 | { 240 | "name": test_case["name"], 241 | "expected": test_case["expected"], 242 | "got": target_output, 243 | } 244 | ) 245 | print( 246 | f"Wrong output for test case {test_case['name']}. \n\tExpected:\n\t {failed_cases[-1].get('expected')}.\n\tGot:\n {failed_cases[-1].get('got')}." 247 | ) 248 | 249 | if len(failed_cases) == 0: 250 | print("\033[92m All tests passed") 251 | else: 252 | print("\033[92m", successful_cases, " Tests passed") 253 | print("\033[91m", len(failed_cases), " Tests failed") -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/00. Notes/Week_3_Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 3/00. Notes/Week_3_Notes.pdf -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/01. Vector Algebra/01. Practice Quiz 1/Practice Quiz 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 3/01. Vector Algebra/01. Practice Quiz 1/Practice Quiz 1.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/02. Linear Transformation/01. Graded Quiz/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 3/02. Linear Transformation/01. Graded Quiz/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/03. Ungraded Labs/C1W3_UGL_2_matrix_multiplication.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "48446b1e", 6 | "metadata": {}, 7 | "source": [ 8 | "# Matrix Multiplication" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "4b6934ea", 14 | "metadata": {}, 15 | "source": [ 16 | "In this lab you will use `NumPy` functions to perform matrix multiplication and see how it can be used in the Machine Learning applications. " 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "4439a0e9", 22 | "metadata": {}, 23 | "source": [ 24 | "# Table of Contents\n", 25 | "\n", 26 | "- [ 1 - Definition of Matrix Multiplication](#1)\n", 27 | "- [ 2 - Matrix Multiplication using Python](#2)\n", 28 | "- [ 3 - Matrix Convention and Broadcasting](#3)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "id": "c2743d75", 34 | "metadata": {}, 35 | "source": [ 36 | "## Packages\n", 37 | "\n", 38 | "Load the `NumPy` package to access its functions." 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 1, 44 | "id": "b463e5b7", 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "import numpy as np" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "5f9be5bb", 54 | "metadata": {}, 55 | "source": [ 56 | "\n", 57 | "## 1 - Definition of Matrix Multiplication\n", 58 | "\n", 59 | "If $A$ is an $m \\times n$ matrix and $B$ is an $n \\times p$ matrix, the matrix product $C = AB$ (denoted without multiplication signs or dots) is defined to be the $m \\times p$ matrix such that \n", 60 | "$c_{ij}=a_{i1}b_{1j}+a_{i2}b_{2j}+\\ldots+a_{in}b_{nj}=\\sum_{k=1}^{n} a_{ik}b_{kj}, \\tag{4}$\n", 61 | "\n", 62 | "where $a_{ik}$ are the elements of matrix $A$, $b_{kj}$ are the elements of matrix $B$, and $i = 1, \\ldots, m$, $k=1, \\ldots, n$, $j = 1, \\ldots, p$. In other words, $c_{ij}$ is the dot product of the $i$-th row of $A$ and the $j$-th column of $B$." 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "ecd63af9", 68 | "metadata": {}, 69 | "source": [ 70 | "\n", 71 | "## 2 - Matrix Multiplication using Python\n", 72 | "\n", 73 | "Like with the dot product, there are a few ways to perform matrix multiplication in Python. As discussed in the previous lab, the calculations are more efficient in the vectorized form. Let's discuss the most commonly used functions in the vectorized form. First, define two matrices:" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 2, 79 | "id": "8b0f59f5", 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "Matrix A (3 by 3):\n", 87 | " [[4 9 9]\n", 88 | " [9 1 6]\n", 89 | " [9 2 3]]\n", 90 | "Matrix B (3 by 2):\n", 91 | " [[2 2]\n", 92 | " [5 7]\n", 93 | " [4 4]]\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "A = np.array([[4, 9, 9], [9, 1, 6], [9, 2, 3]])\n", 99 | "print(\"Matrix A (3 by 3):\\n\", A)\n", 100 | "\n", 101 | "B = np.array([[2, 2], [5, 7], [4, 4]])\n", 102 | "print(\"Matrix B (3 by 2):\\n\", B)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "id": "cdf047c9", 108 | "metadata": {}, 109 | "source": [ 110 | "You can multiply matrices $A$ and $B$ using `NumPy` package function `np.matmul()`:" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 3, 116 | "id": "43452598", 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "array([[ 89, 107],\n", 123 | " [ 47, 49],\n", 124 | " [ 40, 44]])" 125 | ] 126 | }, 127 | "execution_count": 3, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "np.matmul(A, B)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "id": "7be5d42a", 139 | "metadata": {}, 140 | "source": [ 141 | "Which will output $3 \\times 2$ matrix as a `np.array`. Python operator `@` will also work here giving the same result:" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 4, 147 | "id": "bb36ba42", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "array([[ 89, 107],\n", 154 | " [ 47, 49],\n", 155 | " [ 40, 44]])" 156 | ] 157 | }, 158 | "execution_count": 4, 159 | "metadata": {}, 160 | "output_type": "execute_result" 161 | } 162 | ], 163 | "source": [ 164 | "A @ B" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "id": "0186638b", 170 | "metadata": {}, 171 | "source": [ 172 | "\n", 173 | "## 3 - Matrix Convention and Broadcasting\n", 174 | "\n", 175 | "Mathematically, matrix multiplication is defined only if number of the columns of matrix $A$ is equal to the number of the rows of matrix $B$ (you can check again the definition in the secition [1](#1) and see that otherwise the dot products between rows and columns will not be defined). \n", 176 | "\n", 177 | "Thus, in the example above ([2](#2)), changing the order of matrices when performing the multiplication $BA$ will not work as the above rule does not hold anymore. You can check it by running the cells below - both of them will give errors." 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 5, 183 | "id": "3ecc05e5", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 2)\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "try:\n", 196 | " np.matmul(B, A)\n", 197 | "except ValueError as err:\n", 198 | " print(err)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 6, 204 | "id": "ea9c6d13", 205 | "metadata": {}, 206 | "outputs": [ 207 | { 208 | "name": "stdout", 209 | "output_type": "stream", 210 | "text": [ 211 | "matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 2)\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "try:\n", 217 | " B @ A\n", 218 | "except ValueError as err:\n", 219 | " print(err)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "id": "05d9a674", 225 | "metadata": {}, 226 | "source": [ 227 | "So when using matrix multiplication you will need to be very careful about the dimensions - the number of the columns in the first matrix should match the number of the rows in the second matrix. This is very important for your future understanding of Neural Networks and how they work. \n", 228 | "\n", 229 | "However, for multiplying of the vectors, `NumPy` has a shortcut. You can define two vectors $x$ and $y$ of the same size (which one can understand as two $3 \\times 1$ matrices). If you check the shape of the vector $x$, you can see that :" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 7, 235 | "id": "fab77ce6", 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "Shape of vector x: (3,)\n", 243 | "Number of dimensions of vector x: 1\n", 244 | "Shape of vector x, reshaped to a matrix: (3, 1)\n", 245 | "Number of dimensions of vector x, reshaped to a matrix: 2\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "x = np.array([1, -2, -5])\n", 251 | "y = np.array([4, 3, -1])\n", 252 | "\n", 253 | "print(\"Shape of vector x:\", x.shape)\n", 254 | "print(\"Number of dimensions of vector x:\", x.ndim)\n", 255 | "print(\"Shape of vector x, reshaped to a matrix:\", x.reshape((3, 1)).shape)\n", 256 | "print(\"Number of dimensions of vector x, reshaped to a matrix:\", x.reshape((3, 1)).ndim)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "id": "5bd337df", 262 | "metadata": {}, 263 | "source": [ 264 | "Following the matrix convention, multiplication of matrices $3 \\times 1$ and $3 \\times 1$ is not defined. For matrix multiplication you would expect an error in the following cell, but let's check the output:" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 8, 270 | "id": "f655677c", 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "data": { 275 | "text/plain": [ 276 | "3" 277 | ] 278 | }, 279 | "execution_count": 8, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "np.matmul(x,y)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "id": "2fc01d74", 291 | "metadata": {}, 292 | "source": [ 293 | "You can see that there is no error and that the result is actually a dot product $x \\cdot y\\,$! So, vector $x$ was automatically transposed into the vector $1 \\times 3$ and matrix multiplication $x^Ty$ was calculated. While this is very convenient, you need to keep in mind such functionality in Python and pay attention to not use it in a wrong way. The following cell will return an error:" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 9, 299 | "id": "d92006f1", 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "name": "stdout", 304 | "output_type": "stream", 305 | "text": [ 306 | "matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 1)\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "try:\n", 312 | " np.matmul(x.reshape((3, 1)), y.reshape((3, 1)))\n", 313 | "except ValueError as err:\n", 314 | " print(err)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "id": "ace12c7d", 320 | "metadata": {}, 321 | "source": [ 322 | "You might have a question in you mind: does `np.dot()` function also work for matrix multiplication? Let's try it:" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 10, 328 | "id": "f296e528", 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "data": { 333 | "text/plain": [ 334 | "array([[ 89, 107],\n", 335 | " [ 47, 49],\n", 336 | " [ 40, 44]])" 337 | ] 338 | }, 339 | "execution_count": 10, 340 | "metadata": {}, 341 | "output_type": "execute_result" 342 | } 343 | ], 344 | "source": [ 345 | "np.dot(A, B)" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "id": "8dbbdc0f", 351 | "metadata": {}, 352 | "source": [ 353 | "Yes, it works! What actually happens is what is called **broadcasting** in Python: `NumPy` broadcasts this dot product operation to all rows and all columns, you get the resultant product matrix. Broadcasting also works in other cases, for example:" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 11, 359 | "id": "68ded501", 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "data": { 364 | "text/plain": [ 365 | "array([[ 2, 7, 7],\n", 366 | " [ 7, -1, 4],\n", 367 | " [ 7, 0, 1]])" 368 | ] 369 | }, 370 | "execution_count": 11, 371 | "metadata": {}, 372 | "output_type": "execute_result" 373 | } 374 | ], 375 | "source": [ 376 | "A - 2" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "id": "eec1d0d2", 382 | "metadata": {}, 383 | "source": [ 384 | "Mathematically, subtraction of the $3 \\times 3$ matrix $A$ and a scalar is not defined, but Python broadcasts the scalar, creating a $3 \\times 3$ `np.array` and performing subtraction element by element. A practical example of matrix multiplication can be seen in a linear regression model. You will implement it in this week's assignment!" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "id": "86605d6f", 390 | "metadata": {}, 391 | "source": [ 392 | "Congratulations on finishing this lab!" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "id": "76db64ac", 399 | "metadata": {}, 400 | "outputs": [], 401 | "source": [] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "id": "enclosed-lounge", 407 | "metadata": {}, 408 | "outputs": [], 409 | "source": [] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": null, 414 | "id": "desperate-desert", 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [] 418 | } 419 | ], 420 | "metadata": { 421 | "kernelspec": { 422 | "display_name": "Python 3", 423 | "language": "python", 424 | "name": "python3" 425 | }, 426 | "language_info": { 427 | "codemirror_mode": { 428 | "name": "ipython", 429 | "version": 3 430 | }, 431 | "file_extension": ".py", 432 | "mimetype": "text/x-python", 433 | "name": "python", 434 | "nbconvert_exporter": "python", 435 | "pygments_lexer": "ipython3", 436 | "version": "3.8.8" 437 | } 438 | }, 439 | "nbformat": 4, 440 | "nbformat_minor": 5 441 | } 442 | -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/04. Graded Lab/images/nn_model_linear_regression_multiple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 3/04. Graded Lab/images/nn_model_linear_regression_multiple.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/04. Graded Lab/images/nn_model_linear_regression_simple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 3/04. Graded Lab/images/nn_model_linear_regression_simple.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 3/04. Graded Lab/utils.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | def plot_transformation(T, e1, e2): 5 | color_original = "#129cab" 6 | color_transformed = "#cc8933" 7 | 8 | _, ax = plt.subplots(figsize=(7, 7)) 9 | ax.tick_params(axis='x', labelsize=14) 10 | ax.tick_params(axis='y', labelsize=14) 11 | ax.set_xticks(np.arange(-5, 5)) 12 | ax.set_yticks(np.arange(-5, 5)) 13 | 14 | plt.axis([-5, 5, -5, 5]) 15 | plt.quiver([0, 0],[0, 0], [e1[0], e2[0]], [e1[1], e2[1]], color=color_original, angles='xy', scale_units='xy', scale=1) 16 | plt.plot([0, e2[0][0], e1[0][0], e1[0][0]], 17 | [0, e2[1][0], e2[1][0], e1[1][0]], 18 | color=color_original) 19 | e1_sgn = 0.4 * np.array([[1] if i==0 else [i[0]] for i in np.sign(e1)]) 20 | ax.text(e1[0]-0.2+e1_sgn[0], e1[1]-0.2+e1_sgn[1], f'$e_1$', fontsize=14, color=color_original) 21 | e2_sgn = 0.4 * np.array([[1] if i==0 else [i[0]] for i in np.sign(e2)]) 22 | ax.text(e2[0]-0.2+e2_sgn[0], e2[1]-0.2+e2_sgn[1], f'$e_2$', fontsize=14, color=color_original) 23 | 24 | e1_transformed = T(e1) 25 | e2_transformed = T(e2) 26 | 27 | plt.quiver([0, 0],[0, 0], [e1_transformed[0], e2_transformed[0]], [e1_transformed[1], e2_transformed[1]], 28 | color=color_transformed, angles='xy', scale_units='xy', scale=1) 29 | plt.plot([0,e2_transformed[0][0], e1_transformed[0][0]+e2_transformed[0][0], e1_transformed[0][0]], 30 | [0,e2_transformed[1][0], e1_transformed[1][0]+e2_transformed[1][0], e1_transformed[1][0]], 31 | color=color_transformed) 32 | e1_transformed_sgn = 0.4 * np.array([[1] if i==0 else [i[0]] for i in np.sign(e1_transformed)]) 33 | ax.text(e1_transformed[0][0]-0.2+e1_transformed_sgn[0][0], e1_transformed[1][0]-e1_transformed_sgn[1][0], 34 | f'$T(e_1)$', fontsize=14, color=color_transformed) 35 | e2_transformed_sgn = 0.4 * np.array([[1] if i==0 else [i[0]] for i in np.sign(e2_transformed)]) 36 | ax.text(e2_transformed[0][0]-0.2+e2_transformed_sgn[0][0], e2_transformed[1][0]-e2_transformed_sgn[1][0], 37 | f'$T(e_2)$', fontsize=14, color=color_transformed) 38 | 39 | plt.gca().set_aspect("equal") 40 | plt.show() 41 | 42 | def initialize_parameters(n_x): 43 | """ 44 | Returns: 45 | params -- python dictionary containing your parameters: 46 | W -- weight matrix of shape (n_y, n_x) 47 | b -- bias value set as a vector of shape (n_y, 1) 48 | """ 49 | 50 | ### START CODE HERE ### (~ 2 lines of code) 51 | W = np.random.randn(1, n_x) * 0.01 # @REPLACE EQUALS None 52 | b = np.zeros((1, 1)) # @REPLACE EQUALS None 53 | ### END CODE HERE ### 54 | 55 | assert (W.shape == (1, n_x)) 56 | assert (b.shape == (1, 1)) 57 | 58 | parameters = {"W": W, 59 | "b": b} 60 | 61 | return parameters 62 | 63 | def compute_cost(Y_hat, Y): 64 | """ 65 | Computes the cost function as a sum of squares 66 | 67 | Arguments: 68 | Y_hat -- The output of the neural network of shape (n_y, number of examples) 69 | Y -- "true" labels vector of shape (n_y, number of examples) 70 | 71 | Returns: 72 | cost -- sum of squares scaled by 1/(2*number of examples) 73 | 74 | """ 75 | # Number of examples. 76 | m = Y.shape[1] 77 | 78 | # Compute the cost function. 79 | cost = np.sum((Y_hat - Y)**2)/(2*m) 80 | 81 | return cost 82 | 83 | 84 | def backward_propagation(A, X, Y): 85 | """ 86 | Implements the backward propagation, calculating gradients 87 | 88 | Arguments: 89 | parameters -- python dictionary containing our parameters 90 | A -- the output of the neural network of shape (1, number of examples) 91 | X -- input data of shape (n_x, number of examples) 92 | Y -- "true" labels vector of shape (n_y, number of examples) 93 | 94 | Returns: 95 | grads -- python dictionary containing gradients with respect to different parameters 96 | """ 97 | m = X.shape[1] 98 | 99 | # Backward propagation: calculate dW, db. 100 | dZ = A - Y 101 | dW = 1/m * np.matmul(dZ, X.T) 102 | db = 1/m * np.sum(dZ, axis = 1, keepdims = True) 103 | 104 | grads = {"dW": dW, 105 | "db": db} 106 | 107 | return grads 108 | 109 | def update_parameters(parameters, grads, learning_rate = 1.2): 110 | """ 111 | Updates parameters using the gradient descent update rule 112 | 113 | Arguments: 114 | parameters -- python dictionary containing parameters 115 | grads -- python dictionary containing gradients 116 | 117 | Returns: 118 | parameters -- python dictionary containing updated parameters 119 | """ 120 | # Retrieve each parameter from the dictionary "parameters". 121 | W = parameters["W"] 122 | b = parameters["b"] 123 | 124 | # Retrieve each gradient from the dictionary "grads". 125 | dW = grads["dW"] 126 | db = grads["db"] 127 | 128 | # Update rule for each parameter. 129 | W = W - learning_rate * dW 130 | b = b - learning_rate * db 131 | 132 | parameters = {"W": W, 133 | "b": b} 134 | 135 | return parameters 136 | 137 | def train_nn(parameters, A, X, Y, learning_rate = 0.01): 138 | # Backpropagation. Inputs: "A, X, Y". Outputs: "grads". 139 | grads = backward_propagation(A, X, Y) 140 | 141 | # Gradient descent parameter update. Inputs: "parameters, grads". Outputs: "parameters". 142 | parameters = update_parameters(parameters, grads, learning_rate) 143 | 144 | return parameters 145 | -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/00. Notes/Week_4_Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/00. Notes/Week_4_Notes.pdf -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/01. Determinants-in-Depth/01. Practice Quiz/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/01. Determinants-in-Depth/01. Practice Quiz/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/02. EigenValues and EigenVectors/01. Graded Quiz/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/02. EigenValues and EigenVectors/01. Graded Quiz/01.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/images/reflection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/images/reflection.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/images/rotation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/images/rotation.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/images/shear_x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/images/shear_x.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/01. Ungraded Lab/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | def plot_transformation(T,v1,v2, vector_name='v'): 5 | color_original = "#129cab" 6 | color_transformed = "#cc8933" 7 | 8 | v1_transformed = T @ v1 9 | v2_transformed = T @ v2 10 | 11 | fig, ax = plt.subplots(figsize=(7, 7)) 12 | ax.tick_params(axis='x', labelsize=14) 13 | ax.tick_params(axis='y', labelsize=14) 14 | vmin = np.floor(np.min([v1,v2, v1_transformed, v2_transformed, (v1_transformed + v2_transformed)]) - 0.5) 15 | vmax = np.ceil(np.max([v1,v2, v1_transformed, v2_transformed, (v1_transformed + v2_transformed)]) + 0.5) 16 | ax.set_xticks(np.arange(vmin, vmax)) 17 | ax.set_yticks(np.arange(vmin, vmax)) 18 | plt.axis([vmin, vmax, vmin, vmax]) 19 | 20 | plt.quiver([0, 0],[0, 0], [v1[0], v2[0]], [v1[1], v2[1]], color=color_original, angles='xy', scale_units='xy', scale=1) 21 | plt.plot([0,v2[0],v1[0]+v2[0],v1[0]], 22 | [0,v2[1],v1[1]+v2[1],v1[1]], 23 | color=color_original) 24 | 25 | v1_sgn = 0.02 * (vmax-vmin) * np.array([[1] if i==0 else [i] for i in np.sign(v1)]) 26 | ax.text(v1[0] + v1_sgn[0], v1[1], f'${vector_name}_1$', fontsize=14, color=color_original) 27 | 28 | v2_sgn = 0.02 * (vmax-vmin) * np.array([[1] if i==0 else [i] for i in np.sign(v2)]) 29 | ax.text(v2[0], v2[1] + v2_sgn[1], f'${vector_name}_2$', fontsize=14, color=color_original) 30 | 31 | 32 | plt.quiver([0, 0],[0, 0], [v1_transformed[0], v2_transformed[0]], [v1_transformed[1], v2_transformed[1]], 33 | color=color_transformed, angles='xy', scale_units='xy', scale=1) 34 | plt.plot([0,v2_transformed[0],v1_transformed[0]+v2_transformed[0],v1_transformed[0]], 35 | [0,v2_transformed[1],v1_transformed[1]+v2_transformed[1],v1_transformed[1]], 36 | color=color_transformed) 37 | 38 | v1_transformed_sgn = 0.04 * (vmax-vmin) * np.array([[1] if i==0 else [i] for i in np.sign(v1_transformed)]) 39 | ax.text(v1_transformed[0] + v1_transformed_sgn[0] - 0.1 * (1 if v1_transformed[0]<0 else 0), 40 | v1_transformed[1] - v1_transformed_sgn[1] - 0.05 * (1 if v1_transformed[1]<0 else 0), 41 | f'$T({vector_name}_1)$', fontsize=14, color=color_transformed) 42 | 43 | v2_transformed_sgn = 0.04 * (vmax-vmin) *np.array([[1] if i==0 else [i] for i in np.sign(v2_transformed)]) 44 | ax.text(v2_transformed[0] + v2_transformed_sgn[0] - 0.1 * (1 if v1_transformed[0]<0 else 0), 45 | v2_transformed[1] - v2_transformed_sgn[1] - 0.05 * (1 if v1_transformed[1]<0 else 0), 46 | f'$T({vector_name}_2)$', fontsize=14, color=color_transformed) 47 | 48 | 49 | plt.gca().set_aspect("equal") 50 | plt.show() 51 | return fig -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (1).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (1).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (10).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (10).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (11).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (11).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (12).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (12).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (13).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (13).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (14).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (14).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (15).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (15).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (16).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (16).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (17).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (17).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (18).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (18).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (19).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (19).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (2).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (2).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (20).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (20).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (21).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (21).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (22).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (22).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (23).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (23).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (24).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (24).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (25).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (25).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (26).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (26).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (27).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (27).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (28).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (28).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (29).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (29).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (3).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (3).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (30).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (30).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (31).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (31).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (32).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (32).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (33).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (33).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (34).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (34).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (35).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (35).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (36).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (36).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (37).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (37).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (38).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (38).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (39).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (39).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (4).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (4).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (40).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (40).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (41).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (41).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (42).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (42).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (43).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (43).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (44).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (44).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (45).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (45).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (46).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (46).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (47).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (47).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (48).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (48).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (49).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (49).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (5).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (5).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (50).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (50).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (51).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (51).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (52).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (52).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (53).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (53).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (54).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (54).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (55).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (55).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (6).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (6).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (7).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (7).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (8).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (8).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (9).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/data/cat (9).jpg -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/images/shear_transformation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/images/shear_transformation.png -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_centered_data.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_centered_data.npy -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_eigvals.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_eigvals.npy -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_eigvecs.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_eigvecs.npy -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_pca12.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_pca12.npy -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_pca2.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/expected_pca2.npy -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/imgs_flatten.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/support_files/imgs_flatten.npy -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import glob 3 | from matplotlib import image 4 | import cv2 5 | import matplotlib.pyplot as plt 6 | 7 | def load_images(directory): 8 | images = [] 9 | for filename in glob.glob(directory+'*.jpg'): 10 | img = np.array(image.imread(filename)) 11 | gimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | images.append(gimg) 13 | 14 | height, width = gimg.shape 15 | 16 | return images 17 | 18 | def plot_reduced_data(X): 19 | plt.figure(figsize=(12,12)) 20 | plt.scatter(X[:,0], X[:,1], s=60, alpha=.5) 21 | for i in range(len(X)): 22 | plt.text(X[i,0], X[i,1], str(i),size=15) 23 | plt.show() -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/03. Programming_Assignment/02. Graded Lab/w4_unittest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def test_matrix(target_P, target_X0, target_X1): 4 | successful_cases = 0 5 | failed_cases = [] 6 | 7 | test_cases = [ 8 | { 9 | "name": "default_check", 10 | "expected": { 11 | "P": np.array( 12 | [ 13 | [0, 0.75, 0.35, 0.25, 0.85], 14 | [0.15, 0, 0.35, 0.25, 0.05], 15 | [0.15, 0.15, 0, 0.25, 0.05], 16 | [0.15, 0.05, 0.05, 0, 0.05], 17 | [0.55, 0.05, 0.25, 0.25, 0], 18 | ] 19 | ), 20 | "X0": np.array([[0], [0], [0], [1], [0]]), 21 | }, 22 | }, 23 | ] 24 | 25 | for test_case in test_cases: 26 | 27 | try: 28 | assert target_P.shape == test_case["expected"]["P"].shape 29 | successful_cases += 1 30 | except: 31 | failed_cases.append( 32 | { 33 | "name": test_case["name"], 34 | "expected": test_case["expected"]["P"].shape, 35 | "got": target_P.shape, 36 | } 37 | ) 38 | print( 39 | f"Wrong shape of matrix P. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 40 | ) 41 | break 42 | 43 | try: 44 | assert np.allclose(np.diagonal(target_P), np.diagonal(test_case["expected"]["P"])) 45 | successful_cases += 1 46 | except: 47 | failed_cases.append( 48 | { 49 | "name": test_case["name"], 50 | "expected": np.nonzero( 51 | np.logical_not(np.isclose(np.diagonal(target_P), np.diagonal(test_case["expected"]["P"]))) 52 | ), 53 | "got": sum(target_P), 54 | } 55 | ) 56 | print( 57 | f"Wrong matrix P. \nCheck the diagonal elements." 58 | ) 59 | 60 | try: 61 | assert np.allclose(sum(target_P), sum(test_case["expected"]["P"])) 62 | successful_cases += 1 63 | except: 64 | failed_cases.append( 65 | { 66 | "name": test_case["name"], 67 | "expected": np.nonzero( 68 | np.logical_not(np.isclose(sum(target_P), sum(test_case["expected"]["P"]))) 69 | ), 70 | "got": sum(target_P), 71 | } 72 | ) 73 | print( 74 | f"Wrong matrix P. \nCheck the elements in the column {failed_cases[-1].get('expected')[0][0] + 1}." 75 | ) 76 | 77 | try: 78 | assert target_X0.shape == test_case["expected"]["X0"].shape 79 | successful_cases += 1 80 | except: 81 | failed_cases.append( 82 | { 83 | "name": test_case["name"], 84 | "expected": test_case["expected"]["X0"].shape, 85 | "got": target_X0.shape, 86 | } 87 | ) 88 | print( 89 | f"Wrong shape of vector X0. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 90 | ) 91 | break 92 | 93 | try: 94 | assert np.allclose(target_X0, test_case["expected"]["X0"]) 95 | successful_cases += 1 96 | except: 97 | failed_cases.append( 98 | { 99 | "name": test_case["name"], 100 | "expected": np.nonzero( 101 | np.logical_not( 102 | np.isclose(target_X0, test_case["expected"]["X0"]) 103 | ) 104 | ), 105 | "got": target_X0, 106 | } 107 | ) 108 | print( 109 | f"Wrong array X0.\nCheck element {failed_cases[-1].get('expected')[0][0] + 1} in the vector X0." 110 | ) 111 | 112 | expected_X1 = np.matmul(target_P, target_X0) 113 | 114 | try: 115 | assert target_X1.shape == expected_X1.shape 116 | successful_cases += 1 117 | except: 118 | failed_cases.append( 119 | { 120 | "name": test_case["name"], 121 | "expected": expected_X1.shape, 122 | "got": target_X1.shape, 123 | } 124 | ) 125 | print( 126 | f"Wrong shape of vector X1. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 127 | ) 128 | break 129 | 130 | try: 131 | assert np.allclose(target_X1, expected_X1) 132 | successful_cases += 1 133 | except: 134 | failed_cases.append( 135 | { 136 | "name": test_case["name"], 137 | "expected": expected_X1, 138 | "got": target_X1, 139 | } 140 | ) 141 | print( 142 | f"Wrong vector X1. Check if matrix multiplication was performed correctly." 143 | ) 144 | 145 | if len(failed_cases) == 0: 146 | print("\033[92m All tests passed") 147 | else: 148 | print("\033[92m", successful_cases, " Tests passed") 149 | print("\033[91m", len(failed_cases), " Tests failed") 150 | 151 | 152 | def test_check_eigenvector(target_T): 153 | successful_cases = 0 154 | failed_cases = [] 155 | 156 | test_cases = [ 157 | { 158 | "name": "default_check", 159 | "input": { 160 | "P": np.array( 161 | [ 162 | [0, 0.75, 0.35, 0.25, 0.85], 163 | [0.15, 0, 0.35, 0.25, 0.05], 164 | [0.15, 0.15, 0, 0.25, 0.05], 165 | [0.15, 0.05, 0.05, 0, 0.05], 166 | [0.55, 0.05, 0.25, 0.25, 0], 167 | ] 168 | ), 169 | }, 170 | }, 171 | {"name": "extra_check", "input": {"P": np.array([[2, 3], [2, 1],]),},}, 172 | ] 173 | 174 | for test_case in test_cases: 175 | 176 | X_inf = np.linalg.eig(test_case["input"]["P"])[1][:, 0] 177 | 178 | target_X_check = target_T(test_case["input"]["P"], X_inf) 179 | expected_X_check = test_case["input"]["P"] @ X_inf 180 | 181 | try: 182 | assert target_X_check.shape == expected_X_check.shape 183 | successful_cases += 1 184 | except: 185 | failed_cases.append( 186 | { 187 | "name": test_case["name"], 188 | "expected": expected_X_check.shape, 189 | "got": target_X_check.shape, 190 | } 191 | ) 192 | print( 193 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong shape of output matrix in the check_eigenvector function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 194 | ) 195 | break 196 | 197 | try: 198 | assert np.allclose(target_X_check, expected_X_check) 199 | successful_cases += 1 200 | except: 201 | failed_cases.append( 202 | { 203 | "name": test_case["name"], 204 | "expected": expected_X_check, 205 | "got": target_X_check, 206 | } 207 | ) 208 | print( 209 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output matrix in the check_eigenvector function. Check if matrix multiplication was performed correctly." 210 | ) 211 | 212 | if len(failed_cases) == 0: 213 | print("\033[92m All tests passed") 214 | else: 215 | print("\033[92m", successful_cases, " Tests passed") 216 | print("\033[91m", len(failed_cases), " Tests failed") 217 | 218 | def test_center_data(target_center_data): 219 | successful_cases = 0 220 | failed_cases = [] 221 | 222 | test_cases = [{'name': 'default_check', 223 | 'input': np.load('./support_files/imgs_flatten.npy'), 224 | 'expected': np.load('./support_files/expected_centered_data.npy') 225 | }, 226 | ] 227 | 228 | for test_case in test_cases: 229 | try: 230 | target_result = target_center_data(test_case['input']) 231 | 232 | except Exception as e: 233 | print( 234 | f"There was an error evaluating the function. \nError: {e}") 235 | return 236 | 237 | try: 238 | assert isinstance(target_result, np.ndarray) 239 | successful_cases += 1 240 | except: 241 | failed_cases.append( 242 | { 243 | "name": test_case["name"], 244 | "expected": np.ndarray, 245 | "got": type(target_result), 246 | } 247 | ) 248 | print( 249 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output type on the center_data function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 250 | ) 251 | break 252 | 253 | 254 | try: 255 | assert np.allclose(target_result, test_case['expected'], atol=1e-2) 256 | successful_cases += 1 257 | except: 258 | failed_cases.append( 259 | { 260 | "name": test_case["name"], 261 | "expected": test_case['expected'], 262 | "got": target_result, 263 | }) 264 | print( 265 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output on the center_data function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 266 | ) 267 | if len(failed_cases) == 0: 268 | print("\033[92m All tests passed") 269 | else: 270 | print("\033[92m", successful_cases, " Tests passed") 271 | print("\033[91m", len(failed_cases), " Tests failed") 272 | 273 | def test_cov_matrix(target_cov_mat): 274 | successful_cases = 0 275 | failed_cases = [] 276 | test_cases = [{'name': 'default_check', 277 | 'input': np.load('./support_files/expected_centered_data.npy'), 278 | 'expected': np.load('./support_files/expected_cov_mat.npy') 279 | }, 280 | ] 281 | 282 | for test_case in test_cases: 283 | try: 284 | target_result = target_cov_mat(test_case['input']) 285 | except Exception as e: 286 | print(f"There was an error evaluating the function. \nError: {e}") 287 | return 288 | 289 | try: 290 | assert isinstance(target_result, np.ndarray) 291 | successful_cases += 1 292 | except: 293 | 294 | failed_cases.append( 295 | { 296 | 'name': test_case["name"], 297 | 'expected': np.ndarray, 298 | 'got': type(target_result) 299 | 300 | } 301 | ) 302 | print( 303 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output type on the get_cov_matrix function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}.") 304 | break 305 | 306 | try: 307 | assert target_result.shape == test_case['expected'].shape 308 | successful_cases += 1 309 | except: 310 | failed_cases.append( 311 | { 312 | 'name': test_case["name"], 313 | 'expected': test_case['expected'].shape, 314 | 'got': target_result.shape 315 | } 316 | ) 317 | print( 318 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output shape on the get_cov_matrix function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}.") 319 | 320 | try: 321 | assert np.allclose(target_result, test_case['expected']) 322 | successful_cases += 1 323 | except: 324 | failed_cases.append( 325 | { 326 | 'name': test_case["name"], 327 | 'expected': test_case['expected'], 328 | 'got': target_result 329 | } 330 | ) 331 | print( 332 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output on the get_cov_matrix function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}.") 333 | if len(failed_cases) == 0: 334 | print("\033[92m All tests passed") 335 | else: 336 | print("\033[92m", successful_cases, " Tests passed") 337 | print("\033[91m", len(failed_cases), " Tests failed") 338 | 339 | 340 | def test_check_PCA(target_pca): 341 | successful_cases = 0 342 | failed_cases = [] 343 | test_cases = [{'name':'default_check_2', 344 | 'input': {'X':np.load('./support_files/expected_centered_data.npy'), 345 | 'eigenvecs': np.load('./support_files/expected_eigvecs.npy'), 346 | 'k': 2}, 347 | 'expected': np.load('./support_files/expected_pca2.npy') 348 | }, 349 | 350 | {'name': 'default_check_12', 351 | 'input': {'X':np.load('./support_files/expected_centered_data.npy'), 352 | 'eigenvecs': np.load('./support_files/expected_eigvecs.npy'), 353 | 'k': 12}, 354 | 'expected': np.load('./support_files/expected_pca12.npy') 355 | },] 356 | 357 | 358 | for test_case in test_cases: 359 | try: 360 | target_result = target_pca(**test_case['input']) 361 | except Exception as e: 362 | print(f"There was an error evaluating the function. \nError: {e}") 363 | return 364 | 365 | try: 366 | assert isinstance(target_result, np.ndarray) 367 | successful_cases += 1 368 | except: 369 | failed_cases.append( 370 | { 371 | 'name': test_case['name'], 372 | 'expected': np.ndarray, 373 | 'got': type(target_result), 374 | } 375 | ) 376 | print( 377 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output type on the perform_PCA function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}.") 378 | 379 | try: 380 | assert target_result.shape == test_case['expected'].shape 381 | successful_cases += 1 382 | except: 383 | failed_cases.append( 384 | { 385 | 'name': test_case['name'], 386 | 'expected': test_case['expected'].shape, 387 | 'got': target_result.shape 388 | } 389 | ) 390 | print( 391 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output shape on the perform_PCA function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}.") 392 | 393 | try: 394 | assert np.allclose(target_result, test_case['expected'], atol=1e-8) 395 | successful_cases += 1 396 | except: 397 | failed_cases.append( 398 | { 399 | 'name': test_case['name'], 400 | 'expected': test_case['expected'], 401 | 'got': target_result 402 | } 403 | ) 404 | print( 405 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output on the perform_PCA function. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}.") 406 | 407 | if len(failed_cases) == 0: 408 | print("\033[92m All tests passed") 409 | else: 410 | print("\033[92m", successful_cases, " Tests passed") 411 | print("\033[91m", len(failed_cases), " Tests failed") 412 | -------------------------------------------------------------------------------- /01. Linear Algebra/Week 4/README.md: -------------------------------------------------------------------------------- 1 | 01. Linear Algebra for Machine Learning and Data Science/04. Week 4/04. Graded Lab/support_files/expected_cov_mat.npy 2 | 3 | IGNORED DUE TO >100 MB 4 | -------------------------------------------------------------------------------- /02. Calculus/Week 1/00. Notes/Week_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 1/00. Notes/Week_1.pdf -------------------------------------------------------------------------------- /02. Calculus/Week 1/01. Derivatives/01. Practice Quiz 1/01. Ungraded_Quiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 1/01. Derivatives/01. Practice Quiz 1/01. Ungraded_Quiz.png -------------------------------------------------------------------------------- /02. Calculus/Week 1/03. Optimization/01. Graded Quiz/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 1/03. Optimization/01. Graded Quiz/01.png -------------------------------------------------------------------------------- /02. Calculus/Week 1/04. Programming Assignment/01. Graded_Labs/data/prices.csv: -------------------------------------------------------------------------------- 1 | date,price_supplier_a_dollars_per_item,price_supplier_b_dollars_per_item 2 | 1/02/2016,104,76 3 | 1/03/2016,108,76 4 | 1/04/2016,101,84 5 | 1/05/2016,104,79 6 | 1/06/2016,102,81 7 | 1/07/2016,105,84 8 | 1/08/2016,114,90 9 | 1/09/2016,102,93 10 | 1/10/2016,105,93 11 | 1/11/2016,101,99 12 | 1/12/2016,109,98 13 | 1/01/2017,103,96 14 | 1/02/2017,93,94 15 | 1/03/2017,98,104 16 | 1/04/2017,92,101 17 | 1/05/2017,97,102 18 | 1/06/2017,96,104 19 | 1/07/2017,94,106 20 | 1/08/2017,97,105 21 | 1/09/2017,93,103 22 | 1/10/2017,99,106 23 | 1/11/2017,93,104 24 | 1/12/2017,98,113 25 | 1/01/2018,94,115 26 | 1/02/2018,93,114 27 | 1/03/2018,92,124 28 | 1/04/2018,96,119 29 | 1/05/2018,98,115 30 | 1/06/2018,98,112 31 | 1/07/2018,93,111 32 | 1/08/2018,97,106 33 | 1/09/2018,102,107 34 | 1/10/2018,103,108 35 | 1/11/2018,100,108 36 | 1/12/2018,100,102 37 | 1/01/2019,104,104 38 | 1/02/2019,100,101 39 | 1/03/2019,103,101 40 | 1/04/2019,104,100 41 | 1/05/2019,101,103 42 | 1/06/2019,102,106 43 | 1/07/2019,100,100 44 | 1/08/2019,102,97 45 | 1/09/2019,108,98 46 | 1/10/2019,107,90 47 | 1/11/2019,107,92 48 | 1/12/2019,103,92 49 | 1/01/2020,109,99 50 | 1/02/2020,108,94 51 | 1/03/2020,108,91 52 | -------------------------------------------------------------------------------- /02. Calculus/Week 1/04. Programming Assignment/01. Graded_Labs/w1_unittest.py: -------------------------------------------------------------------------------- 1 | # + 2 | import jax.numpy as np 3 | from math import isclose 4 | 5 | # Variables for the default_check test cases. 6 | prices_A = np.array([ 7 | 104., 108., 101., 104., 102., 105., 114., 102., 105., 101., 109., 103., 93., 98., 92., 97., 96., 8 | 94., 97., 93., 99., 93., 98., 94., 93., 92., 96., 98., 98., 93., 97., 102., 103., 100., 100., 104., 9 | 100., 103., 104., 101., 102., 100., 102., 108., 107., 107., 103., 109., 108., 108., 10 | ]) 11 | prices_B = np.array([ 12 | 76., 76., 84., 79., 81., 84., 90., 93., 93., 99., 98., 96., 94., 104., 101., 102., 104., 106., 105., 13 | 103., 106., 104., 113., 115., 114., 124., 119., 115., 112., 111., 106., 107., 108., 108., 102., 104., 14 | 101., 101., 100., 103., 106., 100., 97., 98., 90., 92., 92., 99., 94., 91. 15 | ]) 16 | 17 | 18 | # - 19 | 20 | def test_load_and_convert_data(target_A, target_B): 21 | successful_cases = 0 22 | failed_cases = [] 23 | 24 | test_cases = [ 25 | { 26 | "name": "default_check", 27 | "expected": {"prices_A": prices_A, 28 | "prices_B": prices_B,}, 29 | }, 30 | ] 31 | 32 | for test_case in test_cases: 33 | 34 | try: 35 | assert type(target_A) == type(test_case["expected"]["prices_A"]) 36 | successful_cases += 1 37 | except: 38 | failed_cases.append( 39 | { 40 | "name": test_case["name"], 41 | "expected": type(test_case["expected"]["prices_A"]), 42 | "got": type(target_A), 43 | } 44 | ) 45 | print( 46 | f"prices_A has incorrect type. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 47 | ) 48 | break 49 | 50 | try: 51 | assert type(target_B) == type(test_case["expected"]["prices_B"]) 52 | successful_cases += 1 53 | except: 54 | failed_cases.append( 55 | { 56 | "name": test_case["name"], 57 | "expected": type(test_case["expected"]["prices_B"]), 58 | "got": type(target_B), 59 | } 60 | ) 61 | print( 62 | f"prices_B has incorrect type. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 63 | ) 64 | break 65 | 66 | try: 67 | # Check only one element - no need to check all array. 68 | assert type(target_A[0].item()) == type(test_case["expected"]["prices_A"][0].item()) 69 | successful_cases += 1 70 | except: 71 | failed_cases.append( 72 | { 73 | "name": test_case["name"], 74 | "expected": type(test_case["expected"]["prices_A"][0].item()), 75 | "got": type(target_A[0].item()), 76 | } 77 | ) 78 | print( 79 | f"Elements of prices_A array have incorrect type. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 80 | ) 81 | 82 | try: 83 | # Check only one element - no need to check all array. 84 | assert type(target_B[0].item()) == type(test_case["expected"]["prices_B"][0].item()) 85 | successful_cases += 1 86 | except: 87 | failed_cases.append( 88 | { 89 | "name": test_case["name"], 90 | "expected": type(test_case["expected"]["prices_B"][0].item()), 91 | "got": type(target_B[0].item()), 92 | } 93 | ) 94 | print( 95 | f"Elements of prices_B array have incorrect type. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 96 | ) 97 | 98 | try: 99 | assert target_A.shape == test_case["expected"]["prices_A"].shape 100 | successful_cases += 1 101 | except: 102 | failed_cases.append( 103 | { 104 | "name": test_case["name"], 105 | "expected": test_case["expected"]["prices_A"].shape, 106 | "got": target_A.shape, 107 | } 108 | ) 109 | print( 110 | f"Wrong shape of prices_A array. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 111 | ) 112 | break 113 | 114 | try: 115 | assert target_B.shape == test_case["expected"]["prices_B"].shape 116 | successful_cases += 1 117 | except: 118 | failed_cases.append( 119 | { 120 | "name": test_case["name"], 121 | "expected": test_case["expected"]["prices_B"].shape, 122 | "got": target_B.shape, 123 | } 124 | ) 125 | print( 126 | f"Wrong shape of prices_B array. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 127 | ) 128 | break 129 | 130 | try: 131 | assert np.allclose(target_A, test_case["expected"]["prices_A"]) 132 | successful_cases += 1 133 | except: 134 | failed_cases.append( 135 | { 136 | "name": test_case["name"], 137 | "expected": test_case["expected"]["prices_A"], 138 | "got": target_A, 139 | } 140 | ) 141 | print( 142 | f"Wrong array prices_A. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 143 | ) 144 | 145 | try: 146 | assert np.allclose(target_B, test_case["expected"]["prices_B"]) 147 | successful_cases += 1 148 | except: 149 | failed_cases.append( 150 | { 151 | "name": test_case["name"], 152 | "expected": test_case["expected"]["prices_B"], 153 | "got": target_B, 154 | } 155 | ) 156 | print( 157 | f"Wrong array prices_B. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 158 | ) 159 | 160 | if len(failed_cases) == 0: 161 | print("\033[92m All tests passed") 162 | else: 163 | print("\033[92m", successful_cases, " Tests passed") 164 | print("\033[91m", len(failed_cases), " Tests failed") 165 | 166 | 167 | def test_f_of_omega(target_f_of_omega): 168 | successful_cases = 0 169 | failed_cases = [] 170 | 171 | test_cases = [ 172 | { 173 | "name": "default_check", 174 | "input": {"omega": 0, "pA": prices_A, "pB": prices_B}, 175 | "expected": {"f_of_omega": prices_B,}, 176 | }, 177 | { 178 | "name": "extra_check_1", 179 | "input": {"omega": 0.2, "pA": prices_A, "pB": prices_B}, 180 | "expected": {"f_of_omega": prices_A * 0.2 + prices_B * (1-0.2),}, 181 | }, 182 | { 183 | "name": "extra_check_2", 184 | "input": {"omega": 0.8, "pA": prices_A, "pB": prices_B}, 185 | "expected": {"f_of_omega": prices_A * 0.8 + prices_B * (1-0.8),}, 186 | }, 187 | { 188 | "name": "extra_check_3", 189 | "input": {"omega": 1, "pA": prices_A, "pB": prices_B}, 190 | "expected": {"f_of_omega": prices_A,}, 191 | }, 192 | ] 193 | 194 | for test_case in test_cases: 195 | result = target_f_of_omega(test_case["input"]["omega"], test_case["input"]["pA"], test_case["input"]["pB"]) 196 | 197 | try: 198 | assert result.shape == test_case["expected"]["f_of_omega"].shape 199 | successful_cases += 1 200 | except: 201 | failed_cases.append( 202 | { 203 | "name": test_case["name"], 204 | "expected": test_case["expected"]["f_of_omega"].shape, 205 | "got": result.shape, 206 | } 207 | ) 208 | print( 209 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong shape of f_of_omega output for omega = {test_case['input']['omega']}. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 210 | ) 211 | 212 | try: 213 | assert np.allclose(result, test_case["expected"]["f_of_omega"]) 214 | successful_cases += 1 215 | 216 | except: 217 | failed_cases.append( 218 | { 219 | "name": test_case["name"], 220 | "expected": test_case["expected"]["f_of_omega"], 221 | "got": result, 222 | } 223 | ) 224 | print( 225 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output of f_of_omega for omega = {test_case['input']['omega']}. \n\tExpected: \n{failed_cases[-1].get('expected')}\n\tGot: \n{failed_cases[-1].get('got')}" 226 | ) 227 | 228 | if len(failed_cases) == 0: 229 | print("\033[92m All tests passed") 230 | else: 231 | print("\033[92m", successful_cases, " Tests passed") 232 | print("\033[91m", len(failed_cases), " Tests failed") 233 | 234 | 235 | def test_L_of_omega_array(target_L_of_omega_array): 236 | successful_cases = 0 237 | failed_cases = [] 238 | 239 | # Not all of the values of the output array will be checked - only some of them. 240 | # In graders all of the output array gets checked. 241 | test_cases = [ 242 | { 243 | "name": "default_check", 244 | "input": {"omega_array": np.linspace(0, 1, 1001, endpoint=True), "pA": prices_A, "pB": prices_B}, 245 | "expected": {"shape": (1001,), 246 | "L_of_omega_array": [ 247 | {"i": 0, "L_of_omega": 110.72,}, 248 | {"i": 1000, "L_of_omega": 27.48,}, 249 | {"i": 400, "L_of_omega": 28.051199,}, 250 | ],} 251 | }, 252 | { 253 | "name": "extra_check", 254 | "input": {"omega_array": np.linspace(0, 1, 11, endpoint=True), "pA": prices_A, "pB": prices_B}, 255 | "expected": {"shape": (11,), 256 | "L_of_omega_array": [ 257 | {"i": 0, "L_of_omega": 110.72,}, 258 | {"i": 11, "L_of_omega": 27.48,}, 259 | {"i": 5, "L_of_omega": 17.67,}, 260 | ],} 261 | }, 262 | ] 263 | 264 | for test_case in test_cases: 265 | result = target_L_of_omega_array( 266 | test_case["input"]["omega_array"], 267 | test_case["input"]["pA"], 268 | test_case["input"]["pB"] 269 | ) 270 | 271 | try: 272 | assert result.shape == test_case["expected"]["shape"] 273 | successful_cases += 1 274 | except: 275 | failed_cases.append( 276 | { 277 | "name": test_case["name"], 278 | "expected": test_case["expected"]["shape"], 279 | "got": result.shape, 280 | } 281 | ) 282 | print( 283 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong shape of L_of_omega_array output. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 284 | ) 285 | 286 | for test_case_i in test_case["expected"]["L_of_omega_array"]: 287 | i = test_case_i["i"] 288 | 289 | try: 290 | assert isclose(result[i], test_case_i["L_of_omega"], abs_tol=1e-5) 291 | successful_cases += 1 292 | 293 | except: 294 | failed_cases.append( 295 | { 296 | "name": test_case["name"], 297 | "expected": test_case_i["L_of_omega"], 298 | "got": result[i], 299 | } 300 | ) 301 | print( 302 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output of L_of_omega_array for omega_array = \n{test_case['input']['omega_array']}\nTest for index i = {i}. \n\tExpected: \n{failed_cases[-1].get('expected')}\n\tGot: \n{failed_cases[-1].get('got')}" 303 | ) 304 | 305 | if len(failed_cases) == 0: 306 | print("\033[92m All tests passed") 307 | else: 308 | print("\033[92m", successful_cases, " Tests passed") 309 | print("\033[91m", len(failed_cases), " Tests failed") 310 | 311 | 312 | def test_dLdOmega_of_omega_array(target_dLdOmega_of_omega_array): 313 | successful_cases = 0 314 | failed_cases = [] 315 | 316 | # Not all of the values of the output array will be checked - only some of them. 317 | # In graders all of the output array gets checked. 318 | test_cases = [ 319 | { 320 | "name": "default_check", 321 | "input": {"omega_array": np.linspace(0, 1, 1001, endpoint=True), "pA": prices_A, "pB": prices_B}, 322 | "expected": {"shape": (1001,), 323 | "dLdOmega_of_omega_array": [ 324 | {"i": 0, "dLdOmega_of_omega": -288.96,}, 325 | {"i": 1000, "dLdOmega_of_omega": 122.47999,}, 326 | {"i": 400, "dLdOmega_of_omega": -124.38398,}, 327 | ],} 328 | }, 329 | { 330 | "name": "extra_check", 331 | "input": {"omega_array": np.linspace(0, 1, 11, endpoint=True), "pA": prices_A, "pB": prices_B}, 332 | "expected": {"shape": (11,), 333 | "dLdOmega_of_omega_array": [ 334 | {"i": 0, "dLdOmega_of_omega": -288.96,}, 335 | {"i": 11, "dLdOmega_of_omega": 122.47999,}, 336 | {"i": 5, "dLdOmega_of_omega": -83.240036,}, 337 | ],} 338 | }, 339 | ] 340 | 341 | for test_case in test_cases: 342 | result = target_dLdOmega_of_omega_array( 343 | test_case["input"]["omega_array"], 344 | test_case["input"]["pA"], 345 | test_case["input"]["pB"]) 346 | 347 | try: 348 | assert result.shape == test_case["expected"]["shape"] 349 | successful_cases += 1 350 | except: 351 | failed_cases.append( 352 | { 353 | "name": test_case["name"], 354 | "expected": test_case["expected"]["shape"], 355 | "got": result.shape, 356 | } 357 | ) 358 | print( 359 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong shape of dLdOmega_of_omega_array output. \n\tExpected: {failed_cases[-1].get('expected')}.\n\tGot: {failed_cases[-1].get('got')}." 360 | ) 361 | 362 | for test_case_i in test_case["expected"]["dLdOmega_of_omega_array"]: 363 | i = test_case_i["i"] 364 | 365 | try: 366 | assert isclose(result[i], test_case_i["dLdOmega_of_omega"], abs_tol=1e-5) 367 | successful_cases += 1 368 | 369 | except: 370 | failed_cases.append( 371 | { 372 | "name": test_case["name"], 373 | "expected": test_case_i["dLdOmega_of_omega"], 374 | "got": result[i], 375 | } 376 | ) 377 | print( 378 | f"Test case \"{failed_cases[-1].get('name')}\". Wrong output of dLdOmega_of_omega_array for omega_array = \n{test_case['input']['omega_array']}\nTest for index i = {i}. \n\tExpected: \n{failed_cases[-1].get('expected')}\n\tGot: \n{failed_cases[-1].get('got')}" 379 | ) 380 | 381 | if len(failed_cases) == 0: 382 | print("\033[92m All tests passed") 383 | else: 384 | print("\033[92m", successful_cases, " Tests passed") 385 | print("\033[91m", len(failed_cases), " Tests failed") 386 | -------------------------------------------------------------------------------- /02. Calculus/Week 2/00. Notes/Week_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 2/00. Notes/Week_2.pdf -------------------------------------------------------------------------------- /02. Calculus/Week 2/01. Gradients/01. Practice Quiz 1/01. Partial_Derivatives_and_Gradient.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 2/01. Gradients/01. Practice Quiz 1/01. Partial_Derivatives_and_Gradient.png -------------------------------------------------------------------------------- /02. Calculus/Week 2/02. Gradient Descent/01. Ungraded_Labs/w2_tools.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from matplotlib.widgets import Button 5 | from matplotlib.patches import FancyArrowPatch 6 | from matplotlib.gridspec import GridSpec 7 | from IPython.display import display, clear_output 8 | 9 | 10 | def plot_f(x_range, y_range, f, ox_position): 11 | x = np.linspace(*x_range, 100) 12 | fig, ax = plt.subplots(1,1,figsize=(8,4)) 13 | 14 | fig.canvas.toolbar_visible = False 15 | fig.canvas.header_visible = False 16 | fig.canvas.footer_visible = False 17 | 18 | ax.set_ylim(*y_range) 19 | ax.set_xlim(*x_range) 20 | ax.set_ylabel('$f$') 21 | ax.set_xlabel('$x$') 22 | ax.spines['left'].set_position('zero') 23 | ax.spines['bottom'].set_position(('data', ox_position)) 24 | ax.spines['right'].set_color('none') 25 | ax.spines['top'].set_color('none') 26 | ax.xaxis.set_ticks_position('bottom') 27 | ax.yaxis.set_ticks_position('left') 28 | ax.autoscale(enable=False) 29 | 30 | pf = ax.plot(x, f(x), 'k') 31 | 32 | return fig, ax 33 | 34 | 35 | class gradient_descent_one_variable: 36 | """ class to run one interactive plot """ 37 | def __init__(self, x_range, y_range, f, dfdx, gd, n_it, lr, x_0, ox_position, t_position): 38 | x = np.linspace(*x_range, 100) 39 | fig, ax = plot_f(x_range, y_range, f, ox_position) 40 | 41 | # Initialize plot. 42 | self.fig = fig 43 | self.ax = ax 44 | self.x = x 45 | self.f = f 46 | self.dfdx = dfdx 47 | self.gd = gd 48 | self.n_it = n_it 49 | self.lr = lr 50 | self.x_0 = x_0 51 | self.x_range = x_range 52 | self.i = 0 53 | self.ox_position = ox_position 54 | self.t_position = t_position 55 | 56 | self.update_plot_point(firsttime=True) 57 | self.path = path(self.x_0, self.ax, self.ox_position) # initialize an empty path, avoids existance check 58 | 59 | time.sleep(0.2) 60 | clear_output(wait=True) 61 | display(self.fig) 62 | 63 | self.run_gd() 64 | self.cpoint = self.fig.canvas.mpl_connect('button_press_event', self.click_plot) 65 | 66 | def click_plot(self, event): 67 | ''' Called when click in plot ''' 68 | if (event.xdata <= max(self.x) and event.xdata >= min(self.x)): 69 | self.x_0 = event.xdata 70 | self.i = 0 71 | self.path.re_init(self.x_0) 72 | self.update_plot_point() 73 | time.sleep(0.2) 74 | self.run_gd() 75 | 76 | def update_plot_point(self, firsttime=False): 77 | 78 | # Remove items and re-add them on plot. 79 | if not firsttime: 80 | for artist in self.p_items: 81 | artist.remove() 82 | 83 | a = self.ax.scatter(self.x_0, self.f(self.x_0), marker='o', s=100, color='r', zorder=10) 84 | b = self.ax.scatter(self.x_0, self.ox_position, marker='o', s=100, color='k', zorder=10) 85 | c = self.ax.hlines(self.f(self.x_0), 0, self.x_0, lw=2, ls='dotted', color='k') 86 | d = self.ax.vlines(self.x_0, self.ox_position, self.f(self.x_0), lw=2, ls='dotted', color='k') 87 | t_it = self.ax.annotate(f"Iteration #${self.i}$", xy=(self.t_position[0], self.t_position[1]), 88 | xytext=(4,4), textcoords='offset points', size=10) 89 | t_x_0 = self.ax.annotate(f"$x_0 = {self.x_0:0.4f}$", xy=(self.t_position[0], self.t_position[1]-1), 90 | xytext=(4,4), textcoords='offset points', size=10) 91 | t_f = self.ax.annotate(f"$f\\,\\left(x_0\\right) = {self.f(self.x_0):0.2f}$", 92 | xy=(self.t_position[0], self.t_position[1]-2), xytext=(4,4), 93 | textcoords='offset points', size=10) 94 | t_dfdx = self.ax.annotate(f"$f\\,'\\left(x_0\\right) = {self.dfdx(self.x_0):0.4f}$", 95 | xy=(self.t_position[0], self.t_position[1]-3), 96 | xytext=(4,4), textcoords='offset points', size=10) 97 | 98 | self.p_items = [a, b, c, d, t_it, t_x_0, t_f, t_dfdx] 99 | self.fig.canvas.draw() 100 | 101 | def run_gd(self): 102 | self.i = 1 103 | x_0_new = self.gd(self.dfdx, self.x_0, self.lr, 1) 104 | while (self.i <= self.n_it and abs(self.dfdx(x_0_new)) >= 0.00001 and x_0_new >= self.x_range[0]): 105 | x_0_new = self.gd(self.dfdx, self.x_0, self.lr, 1) 106 | self.path.add_path_item(x_0_new, self.f) 107 | self.x_0 = x_0_new 108 | time.sleep(0.05) 109 | self.update_plot_point() 110 | clear_output(wait=True) 111 | display(self.fig) 112 | self.i += 1 113 | 114 | if abs(self.dfdx(self.x_0)) >= 0.00001 or self.x_0 < self.x_range[0] or self.x_0 < self.x_range[0]: 115 | t_res = self.ax.annotate("Has Not Converged", xy=(self.t_position[0], self.t_position[1]-4), 116 | xytext=(4,4), textcoords='offset points', size=10) 117 | else: 118 | t_res = self.ax.annotate("Converged", xy=(self.t_position[0], self.t_position[1]-4), 119 | xytext=(4,4), textcoords='offset points', size=10) 120 | t_instruction = self.ax.text(0.3,0.95,"[Click on the plot to choose initial point]", 121 | size=10, color="r", transform=self.ax.transAxes) 122 | self.p_items.append(t_res) 123 | self.p_items.append(t_instruction) 124 | # Clear last time at the end, so there is no duplicate with the cell output. 125 | clear_output(wait=True) 126 | # plt.close() 127 | 128 | 129 | class path: 130 | ''' tracks paths during gradient descent on the plot ''' 131 | def __init__(self, x_0, ax, ox_position): 132 | ''' x_0 at start of path ''' 133 | self.path_items = [] 134 | self.x_0 = x_0 135 | self.ax = ax 136 | self.ox_position = ox_position 137 | 138 | def re_init(self, x_0): 139 | for artist in self.path_items: 140 | artist.remove() 141 | self.path_items = [] 142 | self.x_0 = x_0 143 | 144 | def add_path_item(self, x_0, f): 145 | a = FancyArrowPatch( 146 | posA=(self.x_0, self.ox_position), posB=(x_0, self.ox_position), color='r', 147 | arrowstyle='simple, head_width=5, head_length=10, tail_width=1.0', 148 | ) 149 | b = self.ax.scatter(self.x_0, f(self.x_0), facecolors='none', edgecolors='r', ls='dotted', s=100, zorder=10) 150 | self.ax.add_artist(a) 151 | self.path_items.append(a) 152 | self.path_items.append(b) 153 | self.x_0 = x_0 154 | 155 | 156 | # + 157 | def f_example_2(x): 158 | return (np.exp(x) - np.log(x))*np.sin(np.pi*x*2) 159 | 160 | def dfdx_example_2(x): 161 | return (np.exp(x) - 1/x)*np.sin(np.pi*x*2) + (np.exp(x) - \ 162 | np.log(x))*np.cos(np.pi*x*2)*2*np.pi 163 | 164 | 165 | # + 166 | def f_example_3(x,y): 167 | return (85+ 0.1*(- 1/9*(x-6)*x**2*y**3 + 2/3*(x-6)*x**2*y**2)) 168 | 169 | def dfdx_example_3(x,y): 170 | return 0.1/3*x*y**2*(2-y/3)*(3*x-12) 171 | 172 | def dfdy_example_3(x,y): 173 | return 0.1/3*(x-6)*x**2*y*(4-y) 174 | 175 | 176 | # + 177 | def f_example_4(x,y): 178 | return -(10/(3+3*(x-.5)**2+3*(y-.5)**2) + \ 179 | 2/(1+2*((x-3)**2)+2*(y-1.5)**2) + \ 180 | 3/(1+.5*((x-3.5)**2)+0.5*(y-4)**2))+10 181 | 182 | def dfdx_example_4(x,y): 183 | return -(-2*3*(x-0.5)*10/(3+3*(x-0.5)**2+3*(y-0.5)**2)**2 + \ 184 | -2*2*(x-3)*2/(1+2*((x-3)**2)+2*(y-1.5)**2)**2 +\ 185 | -2*0.5*(x-3.5)*3/(1+.5*((x-3.5)**2)+0.5*(y-4)**2)**2) 186 | 187 | def dfdy_example_4(x,y): 188 | return -(-2*3*(y-0.5)*10/(3+3*(x-0.5)**2+3*(y-0.5)**2)**2 + \ 189 | -2*2*(y-1.5)*2/(1+2*((x-3)**2)+2*(y-1.5)**2)**2 +\ 190 | -0.5*2*(y-4)*3/(1+.5*((x-3.5)**2)+0.5*(y-4)**2)**2) 191 | 192 | 193 | # - 194 | 195 | def plot_f_cont_and_surf(x_range, y_range, z_range, f, cmap, view): 196 | 197 | fig = plt.figure( figsize=(10,5)) 198 | fig.canvas.toolbar_visible = False 199 | fig.canvas.header_visible = False 200 | fig.canvas.footer_visible = False 201 | fig.set_facecolor('#ffffff') #white 202 | gs = GridSpec(1, 2, figure=fig) 203 | axc = fig.add_subplot(gs[0, 0]) 204 | axs = fig.add_subplot(gs[0, 1], projection='3d') 205 | 206 | x = np.linspace(*x_range, 51) 207 | y = np.linspace(*y_range, 51) 208 | X,Y = np.meshgrid(x,y) 209 | 210 | cont = axc.contour(X, Y, f(X, Y), cmap=cmap, levels=18, linewidths=2, alpha=0.7) 211 | axc.set_xlabel('$x$') 212 | axc.set_ylabel('$y$') 213 | axc.set_xlim(*x_range) 214 | axc.set_ylim(*y_range) 215 | axc.set_aspect("equal") 216 | axc.autoscale(enable=False) 217 | 218 | surf = axs.plot_surface(X,Y, f(X,Y), cmap=cmap, 219 | antialiased=True, cstride=1, rstride=1, alpha=0.69) 220 | axs.set_xlabel('$x$') 221 | axs.set_ylabel('$y$') 222 | axs.set_zlabel('$f$') 223 | axs.set_xlim(*x_range) 224 | axs.set_ylim(*y_range) 225 | axs.set_zlim(*z_range) 226 | axs.view_init(elev=view['elev'], azim=view['azim']) 227 | axs.autoscale(enable=False) 228 | 229 | return fig, axc, axs 230 | 231 | 232 | class gradient_descent_two_variables: 233 | """ class to run one interactive plot """ 234 | def __init__(self, x_range, y_range, z_range, f, dfdx, dfdy, gd, n_it, lr, x_0, y_0, 235 | t_position, t_space, instr_position, cmap, view): 236 | 237 | x = np.linspace(*x_range, 51) 238 | y = np.linspace(*y_range, 51) 239 | fig, axc, axs = plot_f_cont_and_surf(x_range, y_range, z_range, f, cmap, view) 240 | 241 | # Initialize plot. 242 | self.fig = fig 243 | self.axc = axc 244 | self.axs = axs 245 | self.x = x 246 | self.y = y 247 | self.f = f 248 | self.dfdx = dfdx 249 | self.dfdy = dfdy 250 | self.gd = gd 251 | self.n_it = n_it 252 | self.lr = lr 253 | self.x_0 = x_0 254 | self.y_0 = y_0 255 | self.x_range = x_range 256 | self.y_range = y_range 257 | self.i = 0 258 | self.t_position = t_position 259 | self.t_space = t_space 260 | self.instr_position = instr_position 261 | 262 | self.update_plot_point(firsttime=True) 263 | self.path = path_2(self.x_0, self.y_0, self.axc, self.axs) # initialize an empty path, avoids existance check 264 | 265 | time.sleep(0.2) 266 | clear_output(wait=True) 267 | display(self.fig) 268 | 269 | self.run_gd() 270 | self.cpoint = self.fig.canvas.mpl_connect('button_press_event', self.click_plot) 271 | 272 | def click_plot(self, event): 273 | ''' Called when click in plot ''' 274 | if (event.xdata <= max(self.x) and event.xdata >= min(self.x) and 275 | event.ydata <= max(self.y) and event.ydata >= min(self.y)): 276 | self.x_0 = event.xdata 277 | self.y_0 = event.ydata 278 | self.i = 0 279 | self.path.re_init(self.x_0, self.y_0) 280 | self.update_plot_point() 281 | time.sleep(0.2) 282 | self.run_gd() 283 | 284 | def update_plot_point(self, firsttime=False): 285 | 286 | # Remove items and re-add them on plot. 287 | if not firsttime: 288 | for artist in self.p_items: 289 | artist.remove() 290 | 291 | a = self.axc.scatter(self.x_0, self.y_0, marker='o', s=100, color='k', zorder=10) 292 | b = self.axc.hlines(self.y_0, self.axc.get_xlim()[0], self.x_0, lw=2, ls='dotted', color='k') 293 | c = self.axc.vlines(self.x_0, self.axc.get_ylim()[0], self.y_0, lw=2, ls='dotted', color='k') 294 | d = self.axs.scatter3D(self.x_0, self.y_0, self.f(self.x_0, self.y_0), s=100, color='r', zorder=10) 295 | t_it = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2], 296 | f"Iteration #${self.i}$", size=10, zorder=20) 297 | t_x_y = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2]-self.t_space, 298 | f"$x_0, y_0 = {self.x_0:0.2f}, {self.y_0:0.2f}$", size=10, zorder=20) 299 | t_f = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2]-self.t_space*2, 300 | f"$f\\,\\left(x_0, y_0\\right) = {self.f(self.x_0, self.y_0):0.2f}$", size=10, zorder=20) 301 | t_dfdx = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2]-self.t_space*3, 302 | f"$f\\,'_x\\left(x_0, y_0\\right) = {self.dfdx(self.x_0, self.y_0):0.2f}$", size=10, zorder=20) 303 | t_dfdy = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2]-self.t_space*4, 304 | f"$f\\,'_y\\left(x_0, y_0\\right) = {self.dfdy(self.x_0, self.y_0):0.2f}$", size=10, zorder=20) 305 | self.p_items = [a, b, c, d, t_it, t_x_y, t_f, t_dfdx, t_dfdy] 306 | self.fig.canvas.draw() 307 | 308 | def run_gd(self): 309 | self.i = 1 310 | x_0_new, y_0_new = self.gd(self.dfdx, self.dfdy, self.x_0, self.y_0, self.lr, 1) 311 | 312 | while (self.i <= self.n_it and 313 | (abs(self.dfdx(x_0_new, y_0_new)) >= 0.001 or abs(self.dfdy(x_0_new, y_0_new)) >= 0.001) and 314 | x_0_new >= self.x_range[0] and x_0_new <= self.x_range[1] and 315 | y_0_new >= self.y_range[0] and y_0_new <= self.y_range[1]): 316 | x_0_new, y_0_new = self.gd(self.dfdx, self.dfdy, self.x_0, self.y_0, self.lr, 1) 317 | self.path.add_path_item(x_0_new, y_0_new, self.f) 318 | self.x_0 = x_0_new 319 | self.y_0 = y_0_new 320 | time.sleep(0.05) 321 | self.update_plot_point() 322 | clear_output(wait=True) 323 | display(self.fig) 324 | self.i += 1 325 | 326 | if abs(self.dfdx(x_0_new, y_0_new)) >= 0.001 or abs(self.dfdy(x_0_new, y_0_new)) >= 0.001 or self.x_0 < self.x_range[0] or self.x_0 > self.x_range[1] or self.y_0 < self.y_range[0] or self.y_0 > self.y_range[1]: 327 | t_res = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2]-self.t_space*5, 328 | "Has Not Converged", size=10, zorder=20) 329 | else: 330 | t_res = self.axs.text(self.t_position[0], self.t_position[1], self.t_position[2]-self.t_space*5, 331 | "Converged", size=10, zorder=20) 332 | t_instruction = self.axs.text(*self.instr_position, "[Click on the contour plot to choose initial point]", 333 | size=10, color="r", transform=self.axs.transAxes) 334 | self.p_items.append(t_res) 335 | self.p_items.append(t_instruction) 336 | # Clear last time at the end, so there is no duplicate with the cell output. 337 | clear_output(wait=True) 338 | 339 | 340 | class path_2: 341 | ''' tracks paths during gradient descent on contour and surface plots ''' 342 | def __init__(self, x_0, y_0, axc, axs): 343 | ''' x_0, y_0 at start of path ''' 344 | self.path_items = [] 345 | self.x_0 = x_0 346 | self.y_0 = y_0 347 | self.axc = axc 348 | self.axs = axs 349 | 350 | def re_init(self, x_0, y_0): 351 | for artist in self.path_items: 352 | artist.remove() 353 | self.path_items = [] 354 | self.x_0 = x_0 355 | self.y_0 = y_0 356 | 357 | def add_path_item(self, x_0, y_0, f): 358 | a = FancyArrowPatch( 359 | posA=(self.x_0, self.y_0), posB=(x_0, y_0), color='r', 360 | arrowstyle='simple, head_width=5, head_length=10, tail_width=1.0', 361 | ) 362 | b = self.axs.scatter3D(self.x_0, self.y_0, f(self.x_0, self.y_0), 363 | facecolors='none', edgecolors='r', ls='dotted', s=100, zorder=10) 364 | self.axc.add_artist(a) 365 | self.path_items.append(a) 366 | self.path_items.append(b) 367 | self.x_0 = x_0 368 | self.y_0 = y_0 369 | -------------------------------------------------------------------------------- /02. Calculus/Week 2/02. Gradient Descent/02. Graded_Quiz/03.Partial_Derivatives_and_Gradient_Descent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 2/02. Gradient Descent/02. Graded_Quiz/03.Partial_Derivatives_and_Gradient_Descent.png -------------------------------------------------------------------------------- /02. Calculus/Week 2/02. Gradient Descent/03. Graded_Lab/data/tvmarketing.csv: -------------------------------------------------------------------------------- 1 | TV,Sales 2 | 230.1,22.1 3 | 44.5,10.4 4 | 17.2,9.3 5 | 151.5,18.5 6 | 180.8,12.9 7 | 8.7,7.2 8 | 57.5,11.8 9 | 120.2,13.2 10 | 8.6,4.8 11 | 199.8,10.6 12 | 66.1,8.6 13 | 214.7,17.4 14 | 23.8,9.2 15 | 97.5,9.7 16 | 204.1,19 17 | 195.4,22.4 18 | 67.8,12.5 19 | 281.4,24.4 20 | 69.2,11.3 21 | 147.3,14.6 22 | 218.4,18 23 | 237.4,12.5 24 | 13.2,5.6 25 | 228.3,15.5 26 | 62.3,9.7 27 | 262.9,12 28 | 142.9,15 29 | 240.1,15.9 30 | 248.8,18.9 31 | 70.6,10.5 32 | 292.9,21.4 33 | 112.9,11.9 34 | 97.2,9.6 35 | 265.6,17.4 36 | 95.7,9.5 37 | 290.7,12.8 38 | 266.9,25.4 39 | 74.7,14.7 40 | 43.1,10.1 41 | 228,21.5 42 | 202.5,16.6 43 | 177,17.1 44 | 293.6,20.7 45 | 206.9,12.9 46 | 25.1,8.5 47 | 175.1,14.9 48 | 89.7,10.6 49 | 239.9,23.2 50 | 227.2,14.8 51 | 66.9,9.7 52 | 199.8,11.4 53 | 100.4,10.7 54 | 216.4,22.6 55 | 182.6,21.2 56 | 262.7,20.2 57 | 198.9,23.7 58 | 7.3,5.5 59 | 136.2,13.2 60 | 210.8,23.8 61 | 210.7,18.4 62 | 53.5,8.1 63 | 261.3,24.2 64 | 239.3,15.7 65 | 102.7,14 66 | 131.1,18 67 | 69,9.3 68 | 31.5,9.5 69 | 139.3,13.4 70 | 237.4,18.9 71 | 216.8,22.3 72 | 199.1,18.3 73 | 109.8,12.4 74 | 26.8,8.8 75 | 129.4,11 76 | 213.4,17 77 | 16.9,8.7 78 | 27.5,6.9 79 | 120.5,14.2 80 | 5.4,5.3 81 | 116,11 82 | 76.4,11.8 83 | 239.8,12.3 84 | 75.3,11.3 85 | 68.4,13.6 86 | 213.5,21.7 87 | 193.2,15.2 88 | 76.3,12 89 | 110.7,16 90 | 88.3,12.9 91 | 109.8,16.7 92 | 134.3,11.2 93 | 28.6,7.3 94 | 217.7,19.4 95 | 250.9,22.2 96 | 107.4,11.5 97 | 163.3,16.9 98 | 197.6,11.7 99 | 184.9,15.5 100 | 289.7,25.4 101 | 135.2,17.2 102 | 222.4,11.7 103 | 296.4,23.8 104 | 280.2,14.8 105 | 187.9,14.7 106 | 238.2,20.7 107 | 137.9,19.2 108 | 25,7.2 109 | 90.4,8.7 110 | 13.1,5.3 111 | 255.4,19.8 112 | 225.8,13.4 113 | 241.7,21.8 114 | 175.7,14.1 115 | 209.6,15.9 116 | 78.2,14.6 117 | 75.1,12.6 118 | 139.2,12.2 119 | 76.4,9.4 120 | 125.7,15.9 121 | 19.4,6.6 122 | 141.3,15.5 123 | 18.8,7 124 | 224,11.6 125 | 123.1,15.2 126 | 229.5,19.7 127 | 87.2,10.6 128 | 7.8,6.6 129 | 80.2,8.8 130 | 220.3,24.7 131 | 59.6,9.7 132 | 0.7,1.6 133 | 265.2,12.7 134 | 8.4,5.7 135 | 219.8,19.6 136 | 36.9,10.8 137 | 48.3,11.6 138 | 25.6,9.5 139 | 273.7,20.8 140 | 43,9.6 141 | 184.9,20.7 142 | 73.4,10.9 143 | 193.7,19.2 144 | 220.5,20.1 145 | 104.6,10.4 146 | 96.2,11.4 147 | 140.3,10.3 148 | 240.1,13.2 149 | 243.2,25.4 150 | 38,10.9 151 | 44.7,10.1 152 | 280.7,16.1 153 | 121,11.6 154 | 197.6,16.6 155 | 171.3,19 156 | 187.8,15.6 157 | 4.1,3.2 158 | 93.9,15.3 159 | 149.8,10.1 160 | 11.7,7.3 161 | 131.7,12.9 162 | 172.5,14.4 163 | 85.7,13.3 164 | 188.4,14.9 165 | 163.5,18 166 | 117.2,11.9 167 | 234.5,11.9 168 | 17.9,8 169 | 206.8,12.2 170 | 215.4,17.1 171 | 284.3,15 172 | 50,8.4 173 | 164.5,14.5 174 | 19.6,7.6 175 | 168.4,11.7 176 | 222.4,11.5 177 | 276.9,27 178 | 248.4,20.2 179 | 170.2,11.7 180 | 276.7,11.8 181 | 165.6,12.6 182 | 156.6,10.5 183 | 218.5,12.2 184 | 56.2,8.7 185 | 287.6,26.2 186 | 253.8,17.6 187 | 205,22.6 188 | 139.5,10.3 189 | 191.1,17.3 190 | 286,15.9 191 | 18.7,6.7 192 | 39.5,10.8 193 | 75.5,9.9 194 | 17.2,5.9 195 | 166.8,19.6 196 | 149.7,17.3 197 | 38.2,7.6 198 | 94.2,9.7 199 | 177,12.8 200 | 283.6,25.5 201 | 232.1,13.4 202 | -------------------------------------------------------------------------------- /02. Calculus/Week 3/00. Notes/Week_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/00. Notes/Week_3.pdf -------------------------------------------------------------------------------- /02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/data/tvmarketing.csv: -------------------------------------------------------------------------------- 1 | TV,Sales 2 | 230.1,22.1 3 | 44.5,10.4 4 | 17.2,9.3 5 | 151.5,18.5 6 | 180.8,12.9 7 | 8.7,7.2 8 | 57.5,11.8 9 | 120.2,13.2 10 | 8.6,4.8 11 | 199.8,10.6 12 | 66.1,8.6 13 | 214.7,17.4 14 | 23.8,9.2 15 | 97.5,9.7 16 | 204.1,19 17 | 195.4,22.4 18 | 67.8,12.5 19 | 281.4,24.4 20 | 69.2,11.3 21 | 147.3,14.6 22 | 218.4,18 23 | 237.4,12.5 24 | 13.2,5.6 25 | 228.3,15.5 26 | 62.3,9.7 27 | 262.9,12 28 | 142.9,15 29 | 240.1,15.9 30 | 248.8,18.9 31 | 70.6,10.5 32 | 292.9,21.4 33 | 112.9,11.9 34 | 97.2,9.6 35 | 265.6,17.4 36 | 95.7,9.5 37 | 290.7,12.8 38 | 266.9,25.4 39 | 74.7,14.7 40 | 43.1,10.1 41 | 228,21.5 42 | 202.5,16.6 43 | 177,17.1 44 | 293.6,20.7 45 | 206.9,12.9 46 | 25.1,8.5 47 | 175.1,14.9 48 | 89.7,10.6 49 | 239.9,23.2 50 | 227.2,14.8 51 | 66.9,9.7 52 | 199.8,11.4 53 | 100.4,10.7 54 | 216.4,22.6 55 | 182.6,21.2 56 | 262.7,20.2 57 | 198.9,23.7 58 | 7.3,5.5 59 | 136.2,13.2 60 | 210.8,23.8 61 | 210.7,18.4 62 | 53.5,8.1 63 | 261.3,24.2 64 | 239.3,15.7 65 | 102.7,14 66 | 131.1,18 67 | 69,9.3 68 | 31.5,9.5 69 | 139.3,13.4 70 | 237.4,18.9 71 | 216.8,22.3 72 | 199.1,18.3 73 | 109.8,12.4 74 | 26.8,8.8 75 | 129.4,11 76 | 213.4,17 77 | 16.9,8.7 78 | 27.5,6.9 79 | 120.5,14.2 80 | 5.4,5.3 81 | 116,11 82 | 76.4,11.8 83 | 239.8,12.3 84 | 75.3,11.3 85 | 68.4,13.6 86 | 213.5,21.7 87 | 193.2,15.2 88 | 76.3,12 89 | 110.7,16 90 | 88.3,12.9 91 | 109.8,16.7 92 | 134.3,11.2 93 | 28.6,7.3 94 | 217.7,19.4 95 | 250.9,22.2 96 | 107.4,11.5 97 | 163.3,16.9 98 | 197.6,11.7 99 | 184.9,15.5 100 | 289.7,25.4 101 | 135.2,17.2 102 | 222.4,11.7 103 | 296.4,23.8 104 | 280.2,14.8 105 | 187.9,14.7 106 | 238.2,20.7 107 | 137.9,19.2 108 | 25,7.2 109 | 90.4,8.7 110 | 13.1,5.3 111 | 255.4,19.8 112 | 225.8,13.4 113 | 241.7,21.8 114 | 175.7,14.1 115 | 209.6,15.9 116 | 78.2,14.6 117 | 75.1,12.6 118 | 139.2,12.2 119 | 76.4,9.4 120 | 125.7,15.9 121 | 19.4,6.6 122 | 141.3,15.5 123 | 18.8,7 124 | 224,11.6 125 | 123.1,15.2 126 | 229.5,19.7 127 | 87.2,10.6 128 | 7.8,6.6 129 | 80.2,8.8 130 | 220.3,24.7 131 | 59.6,9.7 132 | 0.7,1.6 133 | 265.2,12.7 134 | 8.4,5.7 135 | 219.8,19.6 136 | 36.9,10.8 137 | 48.3,11.6 138 | 25.6,9.5 139 | 273.7,20.8 140 | 43,9.6 141 | 184.9,20.7 142 | 73.4,10.9 143 | 193.7,19.2 144 | 220.5,20.1 145 | 104.6,10.4 146 | 96.2,11.4 147 | 140.3,10.3 148 | 240.1,13.2 149 | 243.2,25.4 150 | 38,10.9 151 | 44.7,10.1 152 | 280.7,16.1 153 | 121,11.6 154 | 197.6,16.6 155 | 171.3,19 156 | 187.8,15.6 157 | 4.1,3.2 158 | 93.9,15.3 159 | 149.8,10.1 160 | 11.7,7.3 161 | 131.7,12.9 162 | 172.5,14.4 163 | 85.7,13.3 164 | 188.4,14.9 165 | 163.5,18 166 | 117.2,11.9 167 | 234.5,11.9 168 | 17.9,8 169 | 206.8,12.2 170 | 215.4,17.1 171 | 284.3,15 172 | 50,8.4 173 | 164.5,14.5 174 | 19.6,7.6 175 | 168.4,11.7 176 | 222.4,11.5 177 | 276.9,27 178 | 248.4,20.2 179 | 170.2,11.7 180 | 276.7,11.8 181 | 165.6,12.6 182 | 156.6,10.5 183 | 218.5,12.2 184 | 56.2,8.7 185 | 287.6,26.2 186 | 253.8,17.6 187 | 205,22.6 188 | 139.5,10.3 189 | 191.1,17.3 190 | 286,15.9 191 | 18.7,6.7 192 | 39.5,10.8 193 | 75.5,9.9 194 | 17.2,5.9 195 | 166.8,19.6 196 | 149.7,17.3 197 | 38.2,7.6 198 | 94.2,9.7 199 | 177,12.8 200 | 283.6,25.5 201 | 232.1,13.4 202 | -------------------------------------------------------------------------------- /02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/images/nn_model_classification_1_layer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/images/nn_model_classification_1_layer.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/images/nn_model_linear_regression_multiple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/images/nn_model_linear_regression_multiple.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/images/nn_model_linear_regression_simple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/01. Optimization_in_Neural_Network/01. Ungraded_Labs/images/nn_model_linear_regression_simple.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/01. Optimization_in_Neural_Network/02. Practice Quiz 1/01. Optimization_in_Neural_Networks_Ungraded_Quiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/01. Optimization_in_Neural_Network/02. Practice Quiz 1/01. Optimization_in_Neural_Networks_Ungraded_Quiz.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/data/tvmarketing.csv: -------------------------------------------------------------------------------- 1 | TV,Sales 2 | 230.1,22.1 3 | 44.5,10.4 4 | 17.2,9.3 5 | 151.5,18.5 6 | 180.8,12.9 7 | 8.7,7.2 8 | 57.5,11.8 9 | 120.2,13.2 10 | 8.6,4.8 11 | 199.8,10.6 12 | 66.1,8.6 13 | 214.7,17.4 14 | 23.8,9.2 15 | 97.5,9.7 16 | 204.1,19 17 | 195.4,22.4 18 | 67.8,12.5 19 | 281.4,24.4 20 | 69.2,11.3 21 | 147.3,14.6 22 | 218.4,18 23 | 237.4,12.5 24 | 13.2,5.6 25 | 228.3,15.5 26 | 62.3,9.7 27 | 262.9,12 28 | 142.9,15 29 | 240.1,15.9 30 | 248.8,18.9 31 | 70.6,10.5 32 | 292.9,21.4 33 | 112.9,11.9 34 | 97.2,9.6 35 | 265.6,17.4 36 | 95.7,9.5 37 | 290.7,12.8 38 | 266.9,25.4 39 | 74.7,14.7 40 | 43.1,10.1 41 | 228,21.5 42 | 202.5,16.6 43 | 177,17.1 44 | 293.6,20.7 45 | 206.9,12.9 46 | 25.1,8.5 47 | 175.1,14.9 48 | 89.7,10.6 49 | 239.9,23.2 50 | 227.2,14.8 51 | 66.9,9.7 52 | 199.8,11.4 53 | 100.4,10.7 54 | 216.4,22.6 55 | 182.6,21.2 56 | 262.7,20.2 57 | 198.9,23.7 58 | 7.3,5.5 59 | 136.2,13.2 60 | 210.8,23.8 61 | 210.7,18.4 62 | 53.5,8.1 63 | 261.3,24.2 64 | 239.3,15.7 65 | 102.7,14 66 | 131.1,18 67 | 69,9.3 68 | 31.5,9.5 69 | 139.3,13.4 70 | 237.4,18.9 71 | 216.8,22.3 72 | 199.1,18.3 73 | 109.8,12.4 74 | 26.8,8.8 75 | 129.4,11 76 | 213.4,17 77 | 16.9,8.7 78 | 27.5,6.9 79 | 120.5,14.2 80 | 5.4,5.3 81 | 116,11 82 | 76.4,11.8 83 | 239.8,12.3 84 | 75.3,11.3 85 | 68.4,13.6 86 | 213.5,21.7 87 | 193.2,15.2 88 | 76.3,12 89 | 110.7,16 90 | 88.3,12.9 91 | 109.8,16.7 92 | 134.3,11.2 93 | 28.6,7.3 94 | 217.7,19.4 95 | 250.9,22.2 96 | 107.4,11.5 97 | 163.3,16.9 98 | 197.6,11.7 99 | 184.9,15.5 100 | 289.7,25.4 101 | 135.2,17.2 102 | 222.4,11.7 103 | 296.4,23.8 104 | 280.2,14.8 105 | 187.9,14.7 106 | 238.2,20.7 107 | 137.9,19.2 108 | 25,7.2 109 | 90.4,8.7 110 | 13.1,5.3 111 | 255.4,19.8 112 | 225.8,13.4 113 | 241.7,21.8 114 | 175.7,14.1 115 | 209.6,15.9 116 | 78.2,14.6 117 | 75.1,12.6 118 | 139.2,12.2 119 | 76.4,9.4 120 | 125.7,15.9 121 | 19.4,6.6 122 | 141.3,15.5 123 | 18.8,7 124 | 224,11.6 125 | 123.1,15.2 126 | 229.5,19.7 127 | 87.2,10.6 128 | 7.8,6.6 129 | 80.2,8.8 130 | 220.3,24.7 131 | 59.6,9.7 132 | 0.7,1.6 133 | 265.2,12.7 134 | 8.4,5.7 135 | 219.8,19.6 136 | 36.9,10.8 137 | 48.3,11.6 138 | 25.6,9.5 139 | 273.7,20.8 140 | 43,9.6 141 | 184.9,20.7 142 | 73.4,10.9 143 | 193.7,19.2 144 | 220.5,20.1 145 | 104.6,10.4 146 | 96.2,11.4 147 | 140.3,10.3 148 | 240.1,13.2 149 | 243.2,25.4 150 | 38,10.9 151 | 44.7,10.1 152 | 280.7,16.1 153 | 121,11.6 154 | 197.6,16.6 155 | 171.3,19 156 | 187.8,15.6 157 | 4.1,3.2 158 | 93.9,15.3 159 | 149.8,10.1 160 | 11.7,7.3 161 | 131.7,12.9 162 | 172.5,14.4 163 | 85.7,13.3 164 | 188.4,14.9 165 | 163.5,18 166 | 117.2,11.9 167 | 234.5,11.9 168 | 17.9,8 169 | 206.8,12.2 170 | 215.4,17.1 171 | 284.3,15 172 | 50,8.4 173 | 164.5,14.5 174 | 19.6,7.6 175 | 168.4,11.7 176 | 222.4,11.5 177 | 276.9,27 178 | 248.4,20.2 179 | 170.2,11.7 180 | 276.7,11.8 181 | 165.6,12.6 182 | 156.6,10.5 183 | 218.5,12.2 184 | 56.2,8.7 185 | 287.6,26.2 186 | 253.8,17.6 187 | 205,22.6 188 | 139.5,10.3 189 | 191.1,17.3 190 | 286,15.9 191 | 18.7,6.7 192 | 39.5,10.8 193 | 75.5,9.9 194 | 17.2,5.9 195 | 166.8,19.6 196 | 149.7,17.3 197 | 38.2,7.6 198 | 94.2,9.7 199 | 177,12.8 200 | 283.6,25.5 201 | 232.1,13.4 202 | -------------------------------------------------------------------------------- /02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/images/nn_model_classification_1_layer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/images/nn_model_classification_1_layer.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/images/nn_model_linear_regression_multiple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/images/nn_model_linear_regression_multiple.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/images/nn_model_linear_regression_simple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/02. Newton's_Method/01. Ungraded_Lab/images/nn_model_linear_regression_simple.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/02. Newton's_Method/02. Graded_Quiz/01. Graded_Quiz_Optimization_in_Neural_Network_and_Newton_Method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/02. Newton's_Method/02. Graded_Quiz/01. Graded_Quiz_Optimization_in_Neural_Network_and_Newton_Method.png -------------------------------------------------------------------------------- /02. Calculus/Week 3/02. Newton's_Method/03. Graded_Lab/images/nn_model_2_layers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/02. Calculus/Week 3/02. Newton's_Method/03. Graded_Lab/images/nn_model_2_layers.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 1/00. Notes/Week1_Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 1/00. Notes/Week1_Notes.pdf -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 1/01. Introduction to Probability/01. Practice Week/Quiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 1/01. Introduction to Probability/01. Practice Week/Quiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 1/02. Probability Distribution/02. Graded Quiz/Quiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 1/02. Probability Distribution/02. Graded Quiz/Quiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 1/03. Programming Assignment/test_cases_get_word_frequency.joblib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 1/03. Programming Assignment/test_cases_get_word_frequency.joblib -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 1/03. Programming Assignment/w1_unittest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | import joblib 4 | 5 | 6 | 7 | 8 | def test_get_word_frequency(target): 9 | successful_cases = 0 10 | failed_cases = [] 11 | 12 | test_cases = joblib.load("test_cases_get_word_frequency.joblib") 13 | 14 | for test_case in test_cases: 15 | 16 | output = target(**test_case['input']) 17 | expected = test_case['expected'] 18 | 19 | ## Check same length 20 | if len(output) != len(expected): 21 | failed_cases.append({ 22 | "name":test_case["name"], 23 | "expected":len(expected), 24 | "got":len(output)}) 25 | print(f"Wrong output dictionary size for {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 26 | continue 27 | 28 | ## Check if keys are the same 29 | keys_truth = set(expected.keys()) 30 | keys_output = set(output.keys()) 31 | if not (keys_truth.issubset(keys_output) and keys_output.issubset(keys_truth)): 32 | failed_cases.append({ 33 | "name":test_case["name"], 34 | "expected":keys_truth, 35 | "got":keys_output}) 36 | print(f"Wrong output dictionary keys for {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 37 | continue 38 | 39 | ## Check if, for every key, the counting is the same 40 | for key in output.keys(): 41 | if len(output[key]) != len(expected[key]): 42 | failed_cases.append({ 43 | "name":test_case["name"], 44 | "expected":len(expected[key]), 45 | "got":len(output[key])}) 46 | print(f"Wrong output dictionary size for word {key} in {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 47 | break 48 | 49 | if output[key] != expected[key]: 50 | failed_cases.append({ 51 | "name":test_case["name"], 52 | "expected":expected[key], 53 | "got":output[key]}) 54 | print(f"Wrong counting for word {key} in {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 55 | break 56 | successful_cases+=1 57 | 58 | if len(failed_cases) == 0: 59 | print("\033[92m All tests passed") 60 | else: 61 | print("\033[92m", successful_cases, " Tests passed") 62 | print("\033[91m", len(failed_cases), " Tests failed") 63 | 64 | def test_prob_word_given_class(target, word_frequency, class_frequency): 65 | 66 | successful_cases = 0 67 | failed_cases = [] 68 | 69 | test_cases = [ 70 | {'name': 'test_case_0', 71 | 'input': {'word': 'compile', 'cls': 'ham'}, 72 | 'expected': 0.0025951557093425604}, 73 | {'name': 'test_case_1', 74 | 'input': {'word': 'doc', 'cls': 'spam'}, 75 | 'expected': 0.0026929982046678637}, 76 | {'name': 'test_case_2', 77 | 'input': {'word': 'nights', 'cls': 'ham'}, 78 | 'expected': 0.003748558246828143}, 79 | {'name': 'test_case_3', 80 | 'input': {'word': 'attached', 'cls': 'spam'}, 81 | 'expected': 0.008976660682226212}, 82 | {'name': 'test_case_4', 83 | 'input': {'word': 'hook', 'cls': 'ham'}, 84 | 'expected': 0.0025951557093425604}, 85 | {'name': 'test_case_5', 86 | 'input': {'word': 'projector', 'cls': 'spam'}, 87 | 'expected': 0.0017953321364452424}, 88 | {'name': 'test_case_6', 89 | 'input': {'word': 'also', 'cls': 'ham'}, 90 | 'expected': 0.2577854671280277}, 91 | {'name': 'test_case_7', 92 | 'input': {'word': 'requirements', 'cls': 'spam'}, 93 | 'expected': 0.018850987432675045}, 94 | {'name': 'test_case_8', 95 | 'input': {'word': 'dietary', 'cls': 'ham'}, 96 | 'expected': 0.0008650519031141869}, 97 | {'name': 'test_case_9', 98 | 'input': {'word': 'equipment', 'cls': 'spam'}, 99 | 'expected': 0.02064631956912029}, 100 | {'name': 'test_case_10', 101 | 'input': {'word': 'staying', 'cls': 'ham'}, 102 | 'expected': 0.008362168396770472}, 103 | {'name': 'test_case_11', 104 | 'input': {'word': 'find', 'cls': 'spam'}, 105 | 'expected': 0.09425493716337523}, 106 | {'name': 'test_case_12', 107 | 'input': {'word': 'reserve', 'cls': 'ham'}, 108 | 'expected': 0.019319492502883506}, 109 | {'name': 'test_case_13', 110 | 'input': {'word': 'several', 'cls': 'spam'}, 111 | 'expected': 0.04039497307001795}, 112 | {'name': 'test_case_14', 113 | 'input': {'word': 'university', 'cls': 'ham'}, 114 | 'expected': 0.13408304498269896}, 115 | {'name': 'test_case_15', 116 | 'input': {'word': 'shirley', 'cls': 'spam'}, 117 | 'expected': 0.0017953321364452424}, 118 | {'name': 'test_case_16', 119 | 'input': {'word': 'ca', 'cls': 'ham'}, 120 | 'expected': 0.03460207612456748}, 121 | {'name': 'test_case_17', 122 | 'input': {'word': 'enron', 'cls': 'spam'}, 123 | 'expected': 0.0008976660682226212}, 124 | {'name': 'test_case_18', 125 | 'input': {'word': 'thanks', 'cls': 'ham'}, 126 | 'expected': 0.41205305651672436}, 127 | {'name': 'test_case_19', 128 | 'input': {'word': 'soon', 'cls': 'spam'}, 129 | 'expected': 0.04039497307001795} 130 | ] 131 | 132 | for test_case in test_cases: 133 | 134 | output = target(word_frequency = word_frequency, class_frequency = class_frequency, **test_case['input']) 135 | expected = test_case['expected'] 136 | 137 | if not np.isclose(output,expected): 138 | failed_cases.append( 139 | { 140 | "name":test_case['name'], 141 | "expected":expected, 142 | "got":output 143 | } 144 | 145 | ) 146 | print(f"Wrong value for P({test_case['input']['word']} | spam = {test_case['input']['spam']}) in {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 147 | continue 148 | successful_cases+=1 149 | 150 | if len(failed_cases) == 0: 151 | print("\033[92m All tests passed") 152 | else: 153 | print("\033[92m", successful_cases, " Tests passed") 154 | print("\033[91m", len(failed_cases), " Tests failed") 155 | 156 | 157 | 158 | def test_prob_email_given_class(target, word_frequency, class_frequency): 159 | 160 | successful_cases = 0 161 | failed_cases = [] 162 | 163 | test_cases = [{'name': 'test_case_0', 164 | 'input': {'treated_email': ['ca', 165 | 'enron', 166 | 'find', 167 | 'projector', 168 | 'compile', 169 | 'find', 170 | 'attached', 171 | 'staying', 172 | 'soon'], 173 | 'cls': 'ham'}, 174 | 'expected': 3.3983894210489835e-13}, 175 | {'name': 'test_case_1', 176 | 'input': {'treated_email': ['doc', 177 | 'soon', 178 | 'university', 179 | 'nights', 180 | 'attached', 181 | 'nights', 182 | 'equipment', 183 | 'hook'], 184 | 'cls': 'spam'}, 185 | 'expected': 7.069258091965318e-19}, 186 | {'name': 'test_case_2', 187 | 'input': {'treated_email': ['thanks', 188 | 'ca', 189 | 'university', 190 | 'enron', 191 | 'university', 192 | 'several'], 193 | 'cls': 'ham'}, 194 | 'expected': 9.77525599231039e-06}, 195 | {'name': 'test_case_3', 196 | 'input': {'treated_email': ['projector', 'also', 'also', 'ca', 'hook'], 197 | 'cls': 'spam'}, 198 | 'expected': 6.013747036672614e-10}, 199 | {'name': 'test_case_4', 200 | 'input': {'treated_email': ['dietary', 201 | 'find', 202 | 'thanks', 203 | 'staying', 204 | 'shirley', 205 | 'dietary', 206 | 'attached', 207 | 'thanks'], 208 | 'cls': 'ham'}, 209 | 'expected': 3.4470299679032404e-12}, 210 | {'name': 'test_case_5', 211 | 'input': {'treated_email': ['several', 212 | 'find', 213 | 'staying', 214 | 'staying', 215 | 'also', 216 | 'ca', 217 | 'university', 218 | 'equipment'], 219 | 'cls': 'spam'}, 220 | 'expected': 4.397549817075224e-15}, 221 | {'name': 'test_case_6', 222 | 'input': {'treated_email': ['projector', 223 | 'reserve', 224 | 'attached', 225 | 'staying', 226 | 'university', 227 | 'hook', 228 | 'staying', 229 | 'dietary'], 230 | 'cls': 'ham'}, 231 | 'expected': 2.717031873714673e-16}, 232 | {'name': 'test_case_7', 233 | 'input': {'treated_email': ['thanks', 234 | 'attached', 235 | 'thanks', 236 | 'equipment', 237 | 'also', 238 | 'staying', 239 | 'several', 240 | 'staying'], 241 | 'cls': 'spam'}, 242 | 'expected': 4.973982358381553e-15}, 243 | {'name': 'test_case_8', 244 | 'input': {'treated_email': ['compile', 245 | 'dietary', 246 | 'requirements', 247 | 'shirley', 248 | 'several', 249 | 'nights', 250 | 'doc', 251 | 'hook', 252 | 'thanks'], 253 | 'cls': 'ham'}, 254 | 'expected': 2.698275888443421e-16}, 255 | {'name': 'test_case_9', 256 | 'input': {'treated_email': ['enron', 257 | 'hook', 258 | 'staying', 259 | 'staying', 260 | 'doc', 261 | 'equipment'], 262 | 'cls': 'spam'}, 263 | 'expected': 1.444102224707258e-16}] 264 | 265 | for test_case in test_cases: 266 | 267 | got = target(word_frequency = word_frequency, class_frequency = class_frequency, **test_case["input"]) 268 | expected = test_case['expected'] 269 | 270 | if not np.isclose(got, expected): 271 | failed_cases.append( 272 | { 273 | "name":test_case['name'], 274 | "expected":expected, 275 | "got":got 276 | } 277 | 278 | ) 279 | print(f"Wrong value for email = {test_case['input']['treated_email']} and spam = {test_case['input']['spam']} in {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 280 | continue 281 | successful_cases+=1 282 | 283 | if len(failed_cases) == 0: 284 | print("\033[92m All tests passed") 285 | else: 286 | print("\033[92m", successful_cases, " Tests passed") 287 | print("\033[91m", len(failed_cases), " Tests failed") 288 | 289 | 290 | def test_naive_bayes(target, word_frequency, class_frequency): 291 | successful_cases = 0 292 | failed_cases = [] 293 | 294 | test_cases =[{'name': 'test_case_0', 295 | 'input': {'treated_email': ['ca', 296 | 'enron', 297 | 'find', 298 | 'projector', 299 | 'compile', 300 | 'find', 301 | 'attached', 302 | 'staying', 303 | 'soon']}, 304 | 'expected': 0}, 305 | {'name': 'test_case_1', 306 | 'input': {'treated_email': ['doc', 307 | 'soon', 308 | 'university', 309 | 'nights', 310 | 'attached', 311 | 'nights', 312 | 'equipment', 313 | 'hook']}, 314 | 'expected': 0}, 315 | {'name': 'test_case_2', 316 | 'input': {'treated_email': ['thanks', 317 | 'ca', 318 | 'university', 319 | 'enron', 320 | 'university', 321 | 'several']}, 322 | 'expected': 0}, 323 | {'name': 'test_case_3', 324 | 'input': {'treated_email': ['projector', 'also', 'also', 'ca', 'hook']}, 325 | 'expected': 0}, 326 | {'name': 'test_case_4', 327 | 'input': {'treated_email': ['dietary', 328 | 'find', 329 | 'thanks', 330 | 'staying', 331 | 'shirley', 332 | 'dietary', 333 | 'attached', 334 | 'thanks']}, 335 | 'expected': 0}, 336 | {'name': 'test_case_5', 337 | 'input': {'treated_email': ['several', 338 | 'find', 339 | 'staying', 340 | 'staying', 341 | 'also', 342 | 'ca', 343 | 'university', 344 | 'equipment']}, 345 | 'expected': 0}, 346 | {'name': 'test_case_6', 347 | 'input': {'treated_email': ['projector', 348 | 'reserve', 349 | 'attached', 350 | 'staying', 351 | 'university', 352 | 'hook', 353 | 'staying', 354 | 'dietary']}, 355 | 'expected': 0}, 356 | {'name': 'test_case_7', 357 | 'input': {'treated_email': ['thanks', 358 | 'attached', 359 | 'thanks', 360 | 'equipment', 361 | 'also', 362 | 'staying', 363 | 'several', 364 | 'staying']}, 365 | 'expected': 0}, 366 | {'name': 'test_case_8', 367 | 'input': {'treated_email': ['compile', 368 | 'dietary', 369 | 'requirements', 370 | 'shirley', 371 | 'several', 372 | 'nights', 373 | 'doc', 374 | 'hook', 375 | 'thanks']}, 376 | 'expected': 0}, 377 | {'name': 'test_case_9', 378 | 'input': {'treated_email': ['enron', 379 | 'hook', 380 | 'staying', 381 | 'staying', 382 | 'doc', 383 | 'equipment']}, 384 | 'expected': 0}] 385 | 386 | for test_case in test_cases: 387 | 388 | got = target(word_frequency = word_frequency, class_frequency = class_frequency, **test_case["input"]) 389 | expected = test_case['expected'] 390 | 391 | if not np.isclose(got, expected): 392 | failed_cases.append( 393 | { 394 | "name":test_case['name'], 395 | "expected":expected, 396 | "got":got 397 | } 398 | 399 | ) 400 | print(f"Wrong decision for email = {test_case['input']['treated_email']} in {failed_cases[-1]['name']}. Expected: {failed_cases[-1]['expected']}. Got: {failed_cases[-1]['got']}") 401 | continue 402 | successful_cases+=1 403 | 404 | if len(failed_cases) == 0: 405 | print("\033[92m All tests passed") 406 | else: 407 | print("\033[92m", successful_cases, " Tests passed") 408 | print("\033[91m", len(failed_cases), " Tests failed") 409 | 410 | 411 | -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/00. Notes/Week_2_Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 2/00. Notes/Week_2_Notes.pdf -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/01. Describing Distributions/01. Practice Quiz/Quiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 2/01. Describing Distributions/01. Practice Quiz/Quiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/02. Probability Distributions with Multiple Variables/01. Labs/01. Summary Statistics/df_anscombe.csv: -------------------------------------------------------------------------------- 1 | x,y,group 2 | 10.0,8.04,1 3 | 8.0,6.95,1 4 | 13.0,7.58,1 5 | 9.0,8.81,1 6 | 11.0,8.33,1 7 | 14.0,9.96,1 8 | 6.0,7.24,1 9 | 4.0,4.26,1 10 | 12.0,10.84,1 11 | 7.0,4.82,1 12 | 5.0,5.68,1 13 | 10.0,9.14,2 14 | 8.0,8.14,2 15 | 13.0,8.74,2 16 | 9.0,8.77,2 17 | 11.0,9.26,2 18 | 14.0,8.1,2 19 | 6.0,6.13,2 20 | 4.0,3.1,2 21 | 12.0,9.13,2 22 | 7.0,7.26,2 23 | 5.0,4.74,2 24 | 10.0,7.46,3 25 | 8.0,6.77,3 26 | 13.0,12.74,3 27 | 9.0,7.11,3 28 | 11.0,7.81,3 29 | 14.0,8.84,3 30 | 6.0,6.08,3 31 | 4.0,5.39,3 32 | 12.0,8.15,3 33 | 7.0,6.42,3 34 | 5.0,5.73,3 35 | 8.0,6.58,4 36 | 8.0,5.76,4 37 | 8.0,7.71,4 38 | 8.0,8.84,4 39 | 8.0,8.47,4 40 | 8.0,7.04,4 41 | 8.0,5.25,4 42 | 19.0,12.5,4 43 | 8.0,5.56,4 44 | 8.0,7.91,4 45 | 8.0,6.89,4 46 | -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/02. Probability Distributions with Multiple Variables/01. Labs/01. Summary Statistics/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | import os 4 | import matplotlib.pyplot as plt 5 | from IPython.display import display 6 | import ipywidgets as widgets 7 | from ipywidgets import interact,HBox, VBox 8 | import matplotlib.gridspec as gridspec 9 | 10 | df_anscombe = pd.read_csv('df_anscombe.csv') 11 | df_datasaurus = pd.read_csv("datasaurus.csv") 12 | 13 | def plot_anscombes_quartet(): 14 | fig, axs = plt.subplots(2,2, figsize = (8,5), tight_layout = True) 15 | i = 1 16 | fig.suptitle("Anscombe's quartet", fontsize = 16) 17 | for line in axs: 18 | for ax in line: 19 | ax.scatter(df_anscombe[df_anscombe.group == i]['x'],df_anscombe[df_anscombe.group == i]['y']) 20 | ax.set_title(f'Group {i}') 21 | ax.set_ylim(2,15) 22 | ax.set_xlim(0,21) 23 | ax.set_xlabel('x') 24 | ax.set_ylabel('y') 25 | i+=1 26 | 27 | def display_widget(): 28 | 29 | dropdown_graph_1 = widgets.Dropdown( 30 | options=df_datasaurus.group.unique(), 31 | value='dino', 32 | description='Data set 1: ', 33 | disabled=False, 34 | ) 35 | 36 | statistics_graph_1 = widgets.Button( 37 | value=False, 38 | description='Compute stats', 39 | disabled=False, 40 | button_style='', 41 | tooltip='Description', 42 | icon='' 43 | ) 44 | 45 | dropdown_graph_2 = widgets.Dropdown( 46 | options=df_datasaurus.group.unique(), 47 | value='h_lines', 48 | description='Data set 2: ', 49 | disabled=False, 50 | ) 51 | 52 | statistics_graph_2 = widgets.Button( 53 | value=False, 54 | description='Compute stats', 55 | disabled=False, 56 | button_style='', 57 | tooltip='Description', 58 | icon='' 59 | ) 60 | plotted_stats_graph_1 = None 61 | plotted_stats_graph_2 = None 62 | 63 | fig = plt.figure(figsize = (8,4), tight_layout = True) 64 | gs = gridspec.GridSpec(2,2) 65 | ax_1 = fig.add_subplot(gs[0,0]) 66 | ax_2 = fig.add_subplot(gs[1,0]) 67 | ax_text_1 = fig.add_subplot(gs[0,1]) 68 | ax_text_2 = fig.add_subplot(gs[1,1]) 69 | df_group_1 = df_datasaurus.groupby('group').get_group('dino') 70 | df_group_2 = df_datasaurus.groupby('group').get_group('h_lines') 71 | sc_1 = ax_1.scatter(df_group_1['x'],df_group_1['y'], s = 4) 72 | sc_2 = ax_2.scatter(df_group_2['x'],df_group_2['y'], s = 4) 73 | ax_1.set_xlabel('x') 74 | ax_1.set_ylabel('y') 75 | ax_2.set_xlabel('x') 76 | ax_2.set_ylabel('y') 77 | ax_text_1.axis('off') 78 | ax_text_2.axis('off') 79 | 80 | def dropdown_choice(value, plotted_stats, ax_text, sc): 81 | if value.new != plotted_stats: 82 | ax_text.clear() 83 | ax_text.axis('off') 84 | sc.set_offsets(df_datasaurus.groupby('group').get_group(value.new)[['x', 'y']]) 85 | fig.canvas.draw_idle() 86 | 87 | 88 | def get_stats(value, plotted_stats, ax_text, dropdown, val): 89 | value = dropdown.value 90 | if value == plotted_stats: 91 | return 92 | ax_text.clear() 93 | ax_text.axis('off') 94 | df_group = df_datasaurus.groupby('group').get_group(value)[['x','y']] 95 | means = df_group.mean() 96 | var = df_group.var() 97 | corr = df_group.corr() 98 | ax_text.text(0, 99 | 0, 100 | f"Statistics:\n Mean x: {means['x']:.2f}\n Variance x: {var['x']:.2f}\n\n Mean y: {means['y']:.2f}\n Variance y: {var['y']:.2f}\n\n Correlation: {corr['x']['y']:.2f}" 101 | ) 102 | if val == 1: 103 | plotted_stats_graph_1 = value 104 | if val == 2: 105 | plotted_stats_graph_2 = value 106 | 107 | 108 | 109 | 110 | dropdown_graph_1.observe(lambda value: dropdown_choice(value,plotted_stats_graph_1, ax_text_1, sc_1), names = 'value') 111 | statistics_graph_1.on_click(lambda value: get_stats(value, plotted_stats_graph_1, ax_text_1, dropdown_graph_1,1)) 112 | dropdown_graph_2.observe(lambda value: dropdown_choice(value,plotted_stats_graph_2, ax_text_2, sc_2), names = 'value') 113 | statistics_graph_2.on_click(lambda value: get_stats(value, plotted_stats_graph_2, ax_text_2, dropdown_graph_2,2)) 114 | graph_1_box = HBox([dropdown_graph_1, statistics_graph_1]) 115 | graph_2_box = HBox([dropdown_graph_2, statistics_graph_2]) 116 | display(VBox([graph_1_box,graph_2_box])) 117 | 118 | 119 | def plot_datasaurus(): 120 | 121 | fig, axs = plt.subplots(6,2, figsize = (7,9), tight_layout = True) 122 | i = 0 123 | fig.suptitle("Datasaurus", fontsize = 16) 124 | for line in axs: 125 | for ax in line: 126 | if i > 12: 127 | ax.axis('off') 128 | else: 129 | group = df_datasaurus.group.unique()[i] 130 | ax.scatter(df_datasaurus[df_datasaurus.group == group]['x'],df_datasaurus[df_datasaurus.group == group]['y'], s = 4) 131 | ax.set_title(f'Group {group}') 132 | ax.set_ylim(-5,110) 133 | ax.set_xlim(10,110) 134 | ax.set_xlabel('x') 135 | ax.set_ylabel('y') 136 | i+=1 137 | 138 | -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/02. Probability Distributions with Multiple Variables/01. Labs/02.Exploratory Data/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # + 3 | import matplotlib.pyplot as plt 4 | import seaborn as sns 5 | import numpy as np 6 | import pandas as pd 7 | from sklearn.inspection import permutation_importance 8 | 9 | 10 | FONT_SIZE_TICKS = 14 11 | FONT_SIZE_TITLE = 20 12 | FONT_SIZE_AXES = 16 13 | 14 | 15 | def calculate_feature_importance(features, lr, X_test, y_test): 16 | if len(features) > 1: 17 | bunch = permutation_importance( 18 | lr, X_test, y_test, n_repeats=10, random_state=42 19 | ) 20 | imp_means = bunch.importances_mean 21 | ordered_imp_means_args = np.argsort(imp_means)[::-1] 22 | 23 | results = {} 24 | for i in ordered_imp_means_args: 25 | name = list(X_test.columns)[i] 26 | imp_score = imp_means[i] 27 | results.update({name: [imp_score]}) 28 | 29 | most_important = list(X_test.columns)[ordered_imp_means_args[0]] 30 | results_df = pd.DataFrame.from_dict(results) 31 | 32 | return most_important, results_df 33 | 34 | else: 35 | return features[0], None 36 | 37 | 38 | def plot_feature_importance(df): 39 | # Create a plot for feature importance 40 | fig, ax = plt.subplots(figsize=(8, 6)) 41 | ax.set_xlabel("Importance Score", fontsize=FONT_SIZE_AXES) 42 | ax.set_ylabel("Feature", fontsize=FONT_SIZE_AXES) 43 | ax.set_title("Feature Importance", fontsize=FONT_SIZE_TITLE) 44 | ax.tick_params(labelsize=FONT_SIZE_TICKS) 45 | 46 | sns.barplot(data=df, orient="h", ax=ax, color="deepskyblue") 47 | 48 | plt.show() 49 | 50 | 51 | def plot_happiness(variable, x1, y1, x2, y2): 52 | """Plots a predictor variable on x-axis and happiness (life ladder) on y axis. 53 | 54 | Args: 55 | variable: The name of the x axis variable 56 | x1, y1: The x, y original data to be plotted. Both can be None if not available. 57 | x2, y2: The x, y data model to be plotted. Both can be None if not available. 58 | """ 59 | # Create the plot 60 | fig, ax = plt.subplots(figsize=(8, 6)) 61 | # Plot the original data 62 | ax.scatter( 63 | x1, y1, color="blue", edgecolors="white", s=15, label="Training Data" 64 | ) 65 | # Plot the model 66 | ax.scatter( 67 | x2, y2, 68 | color="orange", edgecolors="black", s=15, marker="D", label="Predictions on the Test Set" 69 | ) 70 | 71 | variable_title = " ".join(variable.split("_")).title() 72 | ax.set_xlabel(f"{variable_title}", fontsize=FONT_SIZE_AXES) 73 | ax.set_ylabel("Life Lsadder (1-10)", fontsize=FONT_SIZE_AXES) 74 | ax.set_title(f"Happiness vs. {variable_title}", fontsize=FONT_SIZE_TITLE) 75 | ax.tick_params(labelsize=FONT_SIZE_TICKS) 76 | ax.legend(fontsize=FONT_SIZE_TICKS) 77 | -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/02. Probability Distributions with Multiple Variables/02. Graded Quiz/Graded_Quiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 2/02. Probability Distributions with Multiple Variables/02. Graded Quiz/Graded_Quiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/03. Programming Assignment/Graded_Assignment/answers.json: -------------------------------------------------------------------------------- 1 | {"ex1": {"mean": 3.499, "var": 2.916}, "ex2": {"hist": "left"}, "ex3": {"sum_2_8": 0.0626, "sum_3_7": 0.125, "sum_4_6": 0.187, "sum_5": 0.2502}, "ex4": {"mean": 5.0, "var": 2.499, "cov": -0.001}, "ex5": {"hist": "right"}, "ex6": {"max_sum": 6}, "ex7": {"hist": "left-center"}, "ex8": {"hist": "right-most"}, "ex9": {"mean": "increases", "var": "increases", "cov": "stays the same"}, "ex10": {"options": "changing the loaded side from 1 to 6 will yield a higher mean but the same variance"}, "ex11": {"options": "yes, regardless if the die is fair or not"}} -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 2/03. Programming Assignment/Graded_Assignment/utils.py: -------------------------------------------------------------------------------- 1 | import json 2 | from IPython.display import display 3 | import ipywidgets as widgets 4 | from ipywidgets import interact 5 | 6 | 7 | 8 | 9 | def reset_answers(): 10 | with open("answers.json", "w") as f: 11 | json.dump({}, f) 12 | 13 | def exercise_example(): 14 | mean = widgets.FloatText( 15 | # value='', 16 | placeholder=0.0, 17 | description='Probability:', 18 | disabled=False 19 | ) 20 | 21 | 22 | button = widgets.Button(description="Save your answer!", button_style="success") 23 | output = widgets.Output() 24 | 25 | display(mean) 26 | 27 | display(button, output) 28 | 29 | def on_button_clicked(b): 30 | 31 | with output: 32 | print("Answer for the example exercise saved.") 33 | 34 | 35 | button.on_click(on_button_clicked) 36 | 37 | def exercise_1(): 38 | mean = widgets.FloatText( 39 | # value='', 40 | placeholder=0.0, 41 | description='Mean:', 42 | disabled=False 43 | ) 44 | 45 | var = widgets.FloatText( 46 | # value='', 47 | placeholder=0.0, 48 | description='Variance:', 49 | disabled=False 50 | ) 51 | 52 | 53 | button = widgets.Button(description="Save your answer!", button_style="success") 54 | output = widgets.Output() 55 | 56 | display(mean) 57 | display(var) 58 | # display(cov) 59 | 60 | display(button, output) 61 | 62 | def on_button_clicked(b): 63 | 64 | with open("answers.json", "r") as f: 65 | source_dict = json.load(f) 66 | 67 | answer_dict = { 68 | "ex1": { 69 | "mean": mean.value, 70 | "var": var.value, 71 | } 72 | } 73 | 74 | source_dict.update(answer_dict) 75 | 76 | with open("answers.json", "w") as f: 77 | json.dump(source_dict, f) 78 | 79 | with output: 80 | print("Answer for exercise 1 saved.") 81 | 82 | 83 | button.on_click(on_button_clicked) 84 | 85 | 86 | def exercise_2(): 87 | hist = widgets.ToggleButtons( 88 | options=['left', 'center', 'right'], 89 | description='Your answer:', 90 | disabled=False, 91 | button_style='', # 'success', 'info', 'warning', 'danger' or '' 92 | ) 93 | 94 | button = widgets.Button(description="Save your answer!", button_style="success") 95 | output = widgets.Output() 96 | 97 | display(hist) 98 | 99 | display(button, output) 100 | 101 | def on_button_clicked(b): 102 | 103 | with open("answers.json", "r") as f: 104 | source_dict = json.load(f) 105 | 106 | answer_dict = { 107 | "ex2": { 108 | "hist": hist.value 109 | } 110 | } 111 | 112 | source_dict.update(answer_dict) 113 | 114 | with open("answers.json", "w") as f: 115 | json.dump(source_dict, f) 116 | 117 | with output: 118 | print("Answer for exercise 2 saved.") 119 | 120 | 121 | button.on_click(on_button_clicked) 122 | 123 | 124 | def exercise_3(): 125 | sum_2_8 = widgets.FloatText( 126 | # value='', 127 | placeholder=0.0, 128 | description='P for sum=2', 129 | style = {'description_width': 'initial'}, 130 | disabled=False 131 | ) 132 | 133 | sum_3_7 = widgets.FloatText( 134 | # value='', 135 | placeholder=0.0, 136 | description='P for sum=3:', 137 | style = {'description_width': 'initial'}, 138 | disabled=False 139 | ) 140 | 141 | sum_4_6 = widgets.FloatText( 142 | # value='', 143 | placeholder=0.0, 144 | description='P for sum=4:', 145 | style = {'description_width': 'initial'}, 146 | disabled=False 147 | ) 148 | 149 | sum_5 = widgets.FloatText( 150 | # value='', 151 | placeholder=0.0, 152 | description='P for sum=5:', 153 | style = {'description_width': 'initial'}, 154 | disabled=False 155 | ) 156 | 157 | button = widgets.Button(description="Save your answer!", button_style="success") 158 | output = widgets.Output() 159 | 160 | display(sum_2_8) 161 | display(sum_3_7) 162 | display(sum_4_6) 163 | display(sum_5) 164 | 165 | display(button, output) 166 | 167 | def on_button_clicked(b): 168 | 169 | with open("answers.json", "r") as f: 170 | source_dict = json.load(f) 171 | 172 | answer_dict = { 173 | "ex3": { 174 | "sum_2_8": sum_2_8.value, 175 | "sum_3_7": sum_3_7.value, 176 | "sum_4_6": sum_4_6.value, 177 | "sum_5": sum_5.value 178 | } 179 | } 180 | 181 | source_dict.update(answer_dict) 182 | 183 | with open("answers.json", "w") as f: 184 | json.dump(source_dict, f) 185 | 186 | with output: 187 | print("Answer for exercise 3 saved.") 188 | 189 | 190 | button.on_click(on_button_clicked) 191 | 192 | def exercise_4(): 193 | mean = widgets.FloatText( 194 | # value='', 195 | placeholder=0.0, 196 | description='Mean:', 197 | disabled=False 198 | ) 199 | 200 | var = widgets.FloatText( 201 | # value='', 202 | placeholder=0.0, 203 | description='Variance:', 204 | disabled=False 205 | ) 206 | 207 | cov = widgets.FloatText( 208 | # value='', 209 | placeholder=0.0, 210 | description='Covariance:', 211 | disabled=False 212 | ) 213 | 214 | button = widgets.Button(description="Save your answer!", button_style="success") 215 | output = widgets.Output() 216 | 217 | display(mean) 218 | display(var) 219 | display(cov) 220 | 221 | display(button, output) 222 | 223 | def on_button_clicked(b): 224 | 225 | with open("answers.json", "r") as f: 226 | source_dict = json.load(f) 227 | 228 | answer_dict = { 229 | "ex4": { 230 | "mean": mean.value, 231 | "var": var.value, 232 | "cov": cov.value 233 | } 234 | } 235 | 236 | source_dict.update(answer_dict) 237 | 238 | with open("answers.json", "w") as f: 239 | json.dump(source_dict, f) 240 | 241 | with output: 242 | print("Answer for exercise 4 saved.") 243 | 244 | 245 | button.on_click(on_button_clicked) 246 | 247 | 248 | def exercise_5(): 249 | hist = widgets.ToggleButtons( 250 | options=['left', 'center', 'right'], 251 | description='Your answer:', 252 | disabled=False, 253 | button_style='', # 'success', 'info', 'warning', 'danger' or '' 254 | ) 255 | 256 | button = widgets.Button(description="Save your answer!", button_style="success") 257 | output = widgets.Output() 258 | 259 | display(hist) 260 | 261 | display(button, output) 262 | 263 | def on_button_clicked(b): 264 | 265 | with open("answers.json", "r") as f: 266 | source_dict = json.load(f) 267 | 268 | answer_dict = { 269 | "ex5": { 270 | "hist": hist.value 271 | } 272 | } 273 | 274 | source_dict.update(answer_dict) 275 | 276 | with open("answers.json", "w") as f: 277 | json.dump(source_dict, f) 278 | 279 | with output: 280 | print("Answer for exercise 5 saved.") 281 | 282 | 283 | button.on_click(on_button_clicked) 284 | 285 | 286 | def exercise_6(): 287 | max_sum = widgets.IntSlider( 288 | value=2, 289 | min=2, 290 | max=12, 291 | step=1, 292 | description='Sum:', 293 | disabled=False, 294 | continuous_update=False, 295 | orientation='horizontal', 296 | readout=True, 297 | readout_format='d' 298 | ) 299 | 300 | button = widgets.Button(description="Save your answer!", button_style="success") 301 | output = widgets.Output() 302 | 303 | display(max_sum) 304 | 305 | display(button, output) 306 | 307 | def on_button_clicked(b): 308 | 309 | with open("answers.json", "r") as f: 310 | source_dict = json.load(f) 311 | 312 | answer_dict = { 313 | "ex6": { 314 | "max_sum": max_sum.value 315 | } 316 | } 317 | 318 | source_dict.update(answer_dict) 319 | 320 | with open("answers.json", "w") as f: 321 | json.dump(source_dict, f) 322 | 323 | with output: 324 | print("Answer for exercise 6 saved.") 325 | 326 | 327 | button.on_click(on_button_clicked) 328 | 329 | 330 | def exercise_7(): 331 | hist = widgets.ToggleButtons( 332 | options=['left-most', 'left-center', 'right-center', 'right-most'], 333 | description='Your answer:', 334 | disabled=False, 335 | button_style='', # 'success', 'info', 'warning', 'danger' or '' 336 | ) 337 | 338 | button = widgets.Button(description="Save your answer!", button_style="success") 339 | output = widgets.Output() 340 | 341 | display(hist) 342 | 343 | display(button, output) 344 | 345 | def on_button_clicked(b): 346 | 347 | with open("answers.json", "r") as f: 348 | source_dict = json.load(f) 349 | 350 | answer_dict = { 351 | "ex7": { 352 | "hist": hist.value 353 | } 354 | } 355 | 356 | source_dict.update(answer_dict) 357 | 358 | with open("answers.json", "w") as f: 359 | json.dump(source_dict, f) 360 | 361 | with output: 362 | print("Answer for exercise 7 saved.") 363 | 364 | 365 | button.on_click(on_button_clicked) 366 | 367 | 368 | def exercise_8(): 369 | hist = widgets.ToggleButtons( 370 | options=['left-most', 'left-center', 'right-center', 'right-most'], 371 | description='Your answer:', 372 | disabled=False, 373 | button_style='', # 'success', 'info', 'warning', 'danger' or '' 374 | ) 375 | 376 | button = widgets.Button(description="Save your answer!", button_style="success") 377 | output = widgets.Output() 378 | 379 | display(hist) 380 | 381 | display(button, output) 382 | 383 | def on_button_clicked(b): 384 | 385 | with open("answers.json", "r") as f: 386 | source_dict = json.load(f) 387 | 388 | answer_dict = { 389 | "ex8": { 390 | "hist": hist.value 391 | } 392 | } 393 | 394 | source_dict.update(answer_dict) 395 | 396 | with open("answers.json", "w") as f: 397 | json.dump(source_dict, f) 398 | 399 | with output: 400 | print("Answer for exercise 8 saved.") 401 | 402 | 403 | button.on_click(on_button_clicked) 404 | 405 | 406 | def exercise_9(): 407 | mean = widgets.ToggleButtons( 408 | options=['stays the same', 'increases', 'decreases'], 409 | description='The mean of the sum:', 410 | disabled=False, 411 | button_style='', 412 | ) 413 | 414 | var = widgets.ToggleButtons( 415 | options=['stays the same', 'increases', 'decreases'], 416 | description='The variance of the sum:', 417 | disabled=False, 418 | button_style='', 419 | ) 420 | 421 | cov = widgets.ToggleButtons( 422 | options=['stays the same', 'increases', 'decreases'], 423 | description='The covariance of the joint distribution:', 424 | disabled=False, 425 | button_style='', 426 | ) 427 | 428 | button = widgets.Button(description="Save your answer!", button_style="success") 429 | output = widgets.Output() 430 | 431 | print("As the number of sides in the die increases:") 432 | display(mean) 433 | display(var) 434 | display(cov) 435 | 436 | display(button, output) 437 | 438 | def on_button_clicked(b): 439 | 440 | with open("answers.json", "r") as f: 441 | source_dict = json.load(f) 442 | 443 | answer_dict = { 444 | "ex9": { 445 | "mean": mean.value, 446 | "var": var.value, 447 | "cov": cov.value, 448 | } 449 | } 450 | 451 | source_dict.update(answer_dict) 452 | 453 | with open("answers.json", "w") as f: 454 | json.dump(source_dict, f) 455 | 456 | with output: 457 | print("Answer for exercise 9 saved.") 458 | 459 | 460 | button.on_click(on_button_clicked) 461 | 462 | 463 | 464 | def exercise_10(): 465 | options = widgets.RadioButtons( 466 | options=[ 467 | 'the mean and variance is the same regardless of which side is loaded', 468 | 'having the sides 3 or 4 loaded will yield a higher covariance than any other sides', 469 | 'the mean will decrease as the value of the loaded side increases', 470 | 'changing the loaded side from 1 to 6 will yield a higher mean but the same variance' 471 | ], 472 | layout={'width': 'max-content'} 473 | ) 474 | 475 | button = widgets.Button(description="Save your answer!", button_style="success") 476 | output = widgets.Output() 477 | 478 | display(options) 479 | 480 | display(button, output) 481 | 482 | def on_button_clicked(b): 483 | 484 | with open("answers.json", "r") as f: 485 | source_dict = json.load(f) 486 | 487 | answer_dict = { 488 | "ex10": { 489 | "options": options.value, 490 | } 491 | } 492 | 493 | source_dict.update(answer_dict) 494 | 495 | with open("answers.json", "w") as f: 496 | json.dump(source_dict, f) 497 | 498 | with output: 499 | print("Answer for exercise 10 saved.") 500 | 501 | 502 | button.on_click(on_button_clicked) 503 | 504 | 505 | def exercise_11(): 506 | options = widgets.RadioButtons( 507 | options=[ 508 | 'yes, but only if one of the sides is loaded', 509 | 'no, regardless if the die is fair or not', 510 | 'yes, but only if the die is fair', 511 | 'yes, regardless if the die is fair or not' 512 | ], 513 | layout={'width': 'max-content'} 514 | ) 515 | 516 | button = widgets.Button(description="Save your answer!", button_style="success") 517 | output = widgets.Output() 518 | 519 | display(options) 520 | 521 | display(button, output) 522 | 523 | def on_button_clicked(b): 524 | 525 | with open("answers.json", "r") as f: 526 | source_dict = json.load(f) 527 | 528 | answer_dict = { 529 | "ex11": { 530 | "options": options.value, 531 | } 532 | } 533 | 534 | source_dict.update(answer_dict) 535 | 536 | with open("answers.json", "w") as f: 537 | json.dump(source_dict, f) 538 | 539 | with output: 540 | print("Answer for exercise 11 saved.") 541 | 542 | 543 | button.on_click(on_button_clicked) 544 | 545 | 546 | def check_submissions(): 547 | with open("./answers.json", "r") as f: 548 | answer_dict = json.load(f) 549 | 550 | saved_exercises = [k for k in answer_dict.keys()] 551 | expected = ['ex1', 'ex2', 'ex3', 'ex4', 'ex5', 'ex6', 'ex7', 'ex8', 'ex9', 'ex10', 'ex11'] 552 | missing = [e for e in expected if not e in saved_exercises] 553 | 554 | if missing: 555 | print(f"missing answers for exercises {[ex.split('ex')[1] for ex in missing]}\n\nSave your answers before submitting for grading!") 556 | return 557 | 558 | print("All answers saved, you can submit the assignment for grading!") -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 3/00. Notes/Notes_Week3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 3/00. Notes/Notes_Week3.pdf -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 3/01. Population and Sample/01. Practice Quiz/PQuiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 3/01. Population and Sample/01. Practice Quiz/PQuiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 3/02. Point Estimation/01. Lab/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # + 3 | import matplotlib.pyplot as plt 4 | import seaborn as sns 5 | import numpy as np 6 | import pandas as pd 7 | from sklearn.inspection import permutation_importance 8 | 9 | 10 | FONT_SIZE_TICKS = 14 11 | FONT_SIZE_TITLE = 20 12 | FONT_SIZE_AXES = 16 13 | 14 | 15 | def calculate_feature_importance(features, lr, X_test, y_test): 16 | if len(features) > 1: 17 | bunch = permutation_importance( 18 | lr, X_test, y_test, n_repeats=10, random_state=42 19 | ) 20 | imp_means = bunch.importances_mean 21 | ordered_imp_means_args = np.argsort(imp_means)[::-1] 22 | 23 | results = {} 24 | for i in ordered_imp_means_args: 25 | name = list(X_test.columns)[i] 26 | imp_score = imp_means[i] 27 | results.update({name: [imp_score]}) 28 | 29 | most_important = list(X_test.columns)[ordered_imp_means_args[0]] 30 | results_df = pd.DataFrame.from_dict(results) 31 | 32 | return most_important, results_df 33 | 34 | else: 35 | return features[0], None 36 | 37 | 38 | def plot_feature_importance(df): 39 | # Create a plot for feature importance 40 | fig, ax = plt.subplots(figsize=(8, 6)) 41 | ax.set_xlabel("Importance Score", fontsize=FONT_SIZE_AXES) 42 | ax.set_ylabel("Feature", fontsize=FONT_SIZE_AXES) 43 | ax.set_title("Feature Importance", fontsize=FONT_SIZE_TITLE) 44 | ax.tick_params(labelsize=FONT_SIZE_TICKS) 45 | 46 | sns.barplot(data=df, orient="h", ax=ax, color="deepskyblue") 47 | 48 | plt.show() 49 | 50 | 51 | def plot_happiness(variable, x1, y1, x2, y2): 52 | """Plots a predictor variable on x-axis and happiness (life ladder) on y axis. 53 | 54 | Args: 55 | variable: The name of the x axis variable 56 | x1, y1: The x, y original data to be plotted. Both can be None if not available. 57 | x2, y2: The x, y data model to be plotted. Both can be None if not available. 58 | """ 59 | # Create the plot 60 | fig, ax = plt.subplots(figsize=(8, 6)) 61 | # Plot the original data 62 | ax.scatter( 63 | x1, y1, color="blue", edgecolors="white", s=15, label="Training Data" 64 | ) 65 | # Plot the model 66 | ax.scatter( 67 | x2, y2, 68 | color="orange", edgecolors="black", s=15, marker="D", label="Predictions on the Test Set" 69 | ) 70 | 71 | variable_title = " ".join(variable.split("_")).title() 72 | ax.set_xlabel(f"{variable_title}", fontsize=FONT_SIZE_AXES) 73 | ax.set_ylabel("Life Lsadder (1-10)", fontsize=FONT_SIZE_AXES) 74 | ax.set_title(f"Happiness vs. {variable_title}", fontsize=FONT_SIZE_TITLE) 75 | ax.tick_params(labelsize=FONT_SIZE_TICKS) 76 | ax.legend(fontsize=FONT_SIZE_TICKS) 77 | -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 3/02. Point Estimation/02. Graded Quiz/GQuiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 3/02. Point Estimation/02. Graded Quiz/GQuiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/00. Notes/Notes_Week4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 4/00. Notes/Notes_Week4.pdf -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/01. Confidence Interval/01. Practice Quiz/PQuiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 4/01. Confidence Interval/01. Practice Quiz/PQuiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/02. Hypothesis Testing/01. Lab/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # + 3 | import matplotlib.pyplot as plt 4 | import seaborn as sns 5 | import numpy as np 6 | import pandas as pd 7 | from sklearn.inspection import permutation_importance 8 | 9 | 10 | FONT_SIZE_TICKS = 14 11 | FONT_SIZE_TITLE = 20 12 | FONT_SIZE_AXES = 16 13 | 14 | 15 | def calculate_feature_importance(features, lr, X_test, y_test): 16 | if len(features) > 1: 17 | bunch = permutation_importance( 18 | lr, X_test, y_test, n_repeats=10, random_state=42 19 | ) 20 | imp_means = bunch.importances_mean 21 | ordered_imp_means_args = np.argsort(imp_means)[::-1] 22 | 23 | results = {} 24 | for i in ordered_imp_means_args: 25 | name = list(X_test.columns)[i] 26 | imp_score = imp_means[i] 27 | results.update({name: [imp_score]}) 28 | 29 | most_important = list(X_test.columns)[ordered_imp_means_args[0]] 30 | results_df = pd.DataFrame.from_dict(results) 31 | 32 | return most_important, results_df 33 | 34 | else: 35 | return features[0], None 36 | 37 | 38 | def plot_feature_importance(df): 39 | # Create a plot for feature importance 40 | fig, ax = plt.subplots(figsize=(8, 6)) 41 | ax.set_xlabel("Importance Score", fontsize=FONT_SIZE_AXES) 42 | ax.set_ylabel("Feature", fontsize=FONT_SIZE_AXES) 43 | ax.set_title("Feature Importance", fontsize=FONT_SIZE_TITLE) 44 | ax.tick_params(labelsize=FONT_SIZE_TICKS) 45 | 46 | sns.barplot(data=df, orient="h", ax=ax, color="deepskyblue") 47 | 48 | plt.show() 49 | 50 | 51 | def plot_happiness(variable, x1, y1, x2, y2): 52 | """Plots a predictor variable on x-axis and happiness (life ladder) on y axis. 53 | 54 | Args: 55 | variable: The name of the x axis variable 56 | x1, y1: The x, y original data to be plotted. Both can be None if not available. 57 | x2, y2: The x, y data model to be plotted. Both can be None if not available. 58 | """ 59 | # Create the plot 60 | fig, ax = plt.subplots(figsize=(8, 6)) 61 | # Plot the original data 62 | ax.scatter( 63 | x1, y1, color="blue", edgecolors="white", s=15, label="Training Data" 64 | ) 65 | # Plot the model 66 | ax.scatter( 67 | x2, y2, 68 | color="orange", edgecolors="black", s=15, marker="D", label="Predictions on the Test Set" 69 | ) 70 | 71 | variable_title = " ".join(variable.split("_")).title() 72 | ax.set_xlabel(f"{variable_title}", fontsize=FONT_SIZE_AXES) 73 | ax.set_ylabel("Life Lsadder (1-10)", fontsize=FONT_SIZE_AXES) 74 | ax.set_title(f"Happiness vs. {variable_title}", fontsize=FONT_SIZE_TITLE) 75 | ax.tick_params(labelsize=FONT_SIZE_TICKS) 76 | ax.legend(fontsize=FONT_SIZE_TICKS) 77 | -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/02. Hypothesis Testing/02. Graded Quiz/GQuiz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 4/02. Hypothesis Testing/02. Graded Quiz/GQuiz.png -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/03. Graded Lab/__pycache__/utils.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 4/03. Graded Lab/__pycache__/utils.cpython-310.pyc -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/03. Graded Lab/__pycache__/w4_unittest.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrunalnshah/Mathematics-for-Machine-Learning-and-Data-Science/9aa2d5adab047fd056ebe6baba7bb391ac9cc4ad/03. Probability and Statistics/Week 4/03. Graded Lab/__pycache__/w4_unittest.cpython-310.pyc -------------------------------------------------------------------------------- /03. Probability and Statistics/Week 4/03. Graded Lab/w4_unittest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(179) 4 | 5 | np.random.uniform(low = 10, high = 20, size = 10) 6 | 7 | def test_get_stats(target): 8 | 9 | test_cases = [{'name': 'test_case_1', 10 | 'input': np.array([19.50455046, 12.90527347, 18.68267129, 17.85301249, 13.71774122, 11 | 13.91098702, 18.16410518, 16.63597096, 18.57756755, 17.41476787]), 12 | 'expected': (10, 16.73666475089095, 2.365698170445931)}, 13 | {'name': 'test_case_2', 14 | 'input': np.array([49.99704465, 49.26508329, 48.15882412, 51.59740797, 49.05896027, 15 | 46.01397403, 45.29696906, 48.63119845, 45.52315415, 43.58113295, 16 | 43.58810372, 48.93753677, 51.91145255, 52.23945901, 42.47087567, 17 | 42.50170719, 42.44115578, 46.40779799, 40.57215065, 48.43968513]), 18 | 'expected': (20, 46.83168366960498, 3.501190844170854)}, 19 | {'name': 'test_case_3', 20 | 'input': np.array([ 1.70923536, 0.39026122, 4.04785046, 2.34032621, 2.88279857, 21 | 0.37808171, 3.46175874, 24.92570901, 0.27035019, 2.35627335, 22 | 5.66096993, 3.00928774, 5.69682927, 2.86099122, 16.65419663, 23 | 3.65581194, 1.56435499, 30.57348257, 5.08099823, 2.71325506, 24 | 0.7967606 , 5.55042057, 3.37970955, 4.70478162, 0.55352832, 25 | 12.62701788, 0.68942262, 3.01225193, 2.7348847 , 7.91641966]), 26 | 'expected': (30, 5.406600662367174, 7.053067681261619)}] 27 | 28 | successful_cases = 0 29 | failed_cases = [] 30 | 31 | for test_case in test_cases: 32 | 33 | try: 34 | output = target(test_case['input']) 35 | check_array = [np.isclose(x,y) for x,y in zip(output, test_case['expected'])] 36 | except Exception as e: 37 | print(f"\033[91mUnit test broken in {test_case['name']} due to an Exception being thrown. Input was:\n\t{test_case['input']}\nException is: {e}.\nAborting. ") 38 | return 39 | 40 | 41 | # Check if outputs are numbers 42 | for value in output: 43 | if not isinstance(value, (int,float)): 44 | print(f"\033[91mIncorrect data type for {'n' if output.index(value) == 0 else 'x' if output.index(value) == 1 else 's'}. Expected output is a float or integer, but got {type(value)}.\nAborting test.") 45 | return 46 | 47 | # Check for n 48 | if not check_array[0]: 49 | failed_cases.append( 50 | { 51 | "name":f"n value for {test_case['name']}", 52 | "expected":test_case['expected'][0], 53 | "got":output[0] 54 | 55 | } 56 | 57 | ) 58 | print(f"Wrong n value for test case {failed_cases[-1]['name']}. The input was:\n{test_case['input']}.\nExpected n = {failed_cases[-1]['expected']}, but got n = {failed_cases[-1]['got']}.") 59 | else: 60 | successful_cases += 1 61 | # Check for x 62 | if not check_array[1]: 63 | failed_cases.append( 64 | { 65 | "name":f"x value for {test_case['name']}", 66 | "expected":test_case['expected'][1], 67 | "got":output[1] 68 | 69 | } 70 | 71 | ) 72 | print(f"Wrong x value for test case {failed_cases[-1]['name']}. The input was:\n{test_case['input']}.\nExpected x = {failed_cases[-1]['expected']}, but got x = {failed_cases[-1]['got']}.") 73 | else: 74 | successful_cases += 1 75 | # Check for s 76 | if not check_array[2]: 77 | failed_cases.append( 78 | { 79 | "name":f"s value for {test_case['name']}", 80 | "expected":test_case['expected'][2], 81 | "got":output[2] 82 | 83 | } 84 | 85 | ) 86 | print(f"Wrong s value for test case {failed_cases[-1]['name']}. The input was:\n{test_case['input']}.\nExpected s = {failed_cases[-1]['expected']}, but got s = {failed_cases[-1]['got']}. Do not forget to pass the argument ddof with the proper value!") 87 | else: 88 | successful_cases += 1 89 | 90 | if len(failed_cases) == 0: 91 | print("\033[92m All tests passed") 92 | else: 93 | print("\033[92m", successful_cases, " Tests passed") 94 | print("\033[91m", len(failed_cases), " Tests failed") 95 | 96 | def test_degrees_of_freedom(target): 97 | 98 | test_cases = [{'name': 'test_case_1', 99 | 'input': {'n_v': 5906, 's_v': 46.13, 'n_c': 4442, 's_c': 44.45}, 100 | 'expected': 9742.194110899103}, 101 | {'name': 'test_case_2', 102 | 'input': {'n_v': 5261, 's_v': 36.39, 'n_c': 2537, 's_c': 54.62}, 103 | 'expected': 3657.064062203471}, 104 | {'name': 'test_case_3', 105 | 'input': {'n_v': 5804, 's_v': 40.89, 'n_c': 3098, 's_c': 39.12}, 106 | 'expected': 6569.922250340089}, 107 | {'name': 'test_case_4', 108 | 'input': {'n_v': 2029, 's_v': 42.62, 'n_c': 4081, 's_c': 41.52}, 109 | 'expected': 3955.588594226483}] 110 | 111 | 112 | successful_cases = 0 113 | failed_cases = [] 114 | 115 | for test_case in test_cases: 116 | 117 | try: 118 | output = target(**test_case['input']) 119 | check_solution = np.isclose(output, test_case['expected']) 120 | except Exception as e: 121 | print(f"\033[91mUnit test broken in {test_case['name']} due to an Exception being thrown. Input was:\n\t{test_case['input']}\nException is: {e}.\nAborting. ") 122 | return 123 | 124 | if not check_solution: 125 | failed_cases.append( 126 | { 127 | "name":f"n value for {test_case['name']}", 128 | "expected":test_case['expected'], 129 | "got":output 130 | 131 | } 132 | 133 | ) 134 | print(f"Wrong value for test case {failed_cases[-1]['name']}. The input values were:\n{test_case['input']}.\nExpected {failed_cases[-1]['expected']}, but got {failed_cases[-1]['got']}.") 135 | else: 136 | successful_cases += 1 137 | 138 | 139 | if len(failed_cases) == 0: 140 | print("\033[92m All tests passed") 141 | else: 142 | print("\033[92m", successful_cases, " Tests passed") 143 | print("\033[91m", len(failed_cases), " Tests failed") 144 | 145 | 146 | def test_t_value(target): 147 | 148 | 149 | test_cases = [{'name': 'test_case_1', 150 | 'input': {'n_v': 5776, 151 | 'x_v': 23.38294493263588, 152 | 's_v': 38.92, 153 | 'n_c': 2139, 154 | 's_c': 42.88, 155 | 'x_c': 41.890689354074816}, 156 | 'expected': -17.473698745465104}, 157 | {'name': 'test_case_2', 158 | 'input': {'n_v': 4187, 159 | 'x_v': 26.131926712301706, 160 | 's_v': 44.35, 161 | 'n_c': 4566, 162 | 's_c': 47.56, 163 | 'x_c': 35.456317192552234}, 164 | 'expected': -9.49119497926876}, 165 | {'name': 'test_case_3', 166 | 'input': {'n_v': 5866, 167 | 'x_v': 39.14918250716274, 168 | 's_v': 41.06, 169 | 'n_c': 2419, 170 | 's_c': 38.36, 171 | 'x_c': 41.01944898537004}, 172 | 'expected': -1.9761484013170285}, 173 | {'name': 'test_case_4', 174 | 'input': {'n_v': 4600, 175 | 'x_v': 30.641116701676623, 176 | 's_v': 40.83, 177 | 'n_c': 5542, 178 | 's_c': 49.07, 179 | 'x_c': 15.063310320827487}, 180 | 'expected': 17.450508434704457}] 181 | 182 | successful_cases = 0 183 | failed_cases = [] 184 | 185 | for test_case in test_cases: 186 | 187 | try: 188 | output = target(**test_case['input']) 189 | check_solution = np.isclose(output, test_case['expected']) 190 | except Exception as e: 191 | print(f"\033[91mUnit test broken in {test_case['name']} due to an Exception being thrown. Input was:\n\t{test_case['input']}\nException is: {e}.\nAborting. ") 192 | return 193 | 194 | if not check_solution: 195 | failed_cases.append( 196 | { 197 | "name":f"n value for {test_case['name']}", 198 | "expected":test_case['expected'], 199 | "got":output 200 | 201 | } 202 | 203 | ) 204 | print(f"Wrong value for test case {failed_cases[-1]['name']}. The input values were:\n{test_case['input']}.\nExpected {failed_cases[-1]['expected']}, but got {failed_cases[-1]['got']}.") 205 | else: 206 | successful_cases += 1 207 | 208 | if len(failed_cases) == 0: 209 | print("\033[92m All tests passed") 210 | else: 211 | print("\033[92m", successful_cases, " Tests passed") 212 | print("\033[91m", len(failed_cases), " Tests failed") 213 | 214 | def test_p_value(target): 215 | 216 | 217 | test_cases = [{'name': 'test_case_1', 218 | 'input': {'d': 3546, 't_value': 1.121289949626803}, 219 | 'expected': 0.13112019466699698}, 220 | {'name': 'test_case_2', 221 | 'input': {'d': 3469, 't_value': 2.694950876114083}, 222 | 'expected': 0.003536921187750286}, 223 | {'name': 'test_case_3', 224 | 'input': {'d': 3550, 't_value': 1.1728775488141598}, 225 | 'expected': 0.12046180346625257}, 226 | {'name': 'test_case_4', 227 | 'input': {'d': 2681, 't_value': 1.5534967853750998}, 228 | 'expected': 0.060211263665166714}] 229 | 230 | successful_cases = 0 231 | failed_cases = [] 232 | 233 | for test_case in test_cases: 234 | 235 | try: 236 | output = target(**test_case['input']) 237 | check_solution = np.isclose(output, test_case['expected']) 238 | except Exception as e: 239 | print(f"\033[91mUnit test broken in {test_case['name']} due to an Exception being thrown. Input was:\n\t{test_case['input']}\nException is: {e}.\nAborting. ") 240 | return 241 | 242 | if not check_solution: 243 | failed_cases.append( 244 | { 245 | "name":f"n value for {test_case['name']}", 246 | "expected":test_case['expected'], 247 | "got":output 248 | 249 | } 250 | 251 | ) 252 | print(f"Wrong value for test case {failed_cases[-1]['name']}. The input values were:\n{test_case['input']}.\nExpected {failed_cases[-1]['expected']}, but got {failed_cases[-1]['got']}.") 253 | else: 254 | successful_cases += 1 255 | 256 | if len(failed_cases) == 0: 257 | print("\033[92m All tests passed") 258 | else: 259 | print("\033[92m", successful_cases, " Tests passed") 260 | print("\033[91m", len(failed_cases), " Tests failed") 261 | 262 | 263 | 264 | 265 | def test_make_decision(target): 266 | 267 | 268 | test_cases = [{'name': 'test_case_1', 269 | 'input': {'X_v': np.array([34, 27, 32, 23, 31, 26, 29, 37, 37, 23]), 270 | 'X_c': np.array([24, 23, 34, 35, 23, 30, 21, 20, 29, 21]), 271 | 'alpha': 0.09}, 272 | 'expected': 'Reject H_0'}, 273 | {'name': 'test_case_2', 274 | 'input': {'X_v': np.array([38, 27, 36, 28, 26, 33, 34, 30, 31, 27]), 275 | 'X_c': np.array([27, 28, 26, 28, 31, 36, 20, 21, 39, 22]), 276 | 'alpha': 0.01}, 277 | 'expected': 'Do not reject H_0'}, 278 | {'name': 'test_case_3', 279 | 'input': {'X_v': np.array([32, 37, 30, 28, 32, 34, 26, 34, 31, 32]), 280 | 'X_c': np.array([34, 34, 28, 21, 23, 25, 30, 31, 32, 34]), 281 | 'alpha': 0.01}, 282 | 'expected': 'Do not reject H_0'}, 283 | {'name': 'test_case_4', 284 | 'input': {'X_v': np.array([29, 35, 32, 20, 32, 30, 21, 23, 26, 32]), 285 | 'X_c': np.array([25, 24, 28, 23, 28, 24, 33, 31, 39, 29]), 286 | 'alpha': 0.08}, 287 | 'expected': 'Do not reject H_0'}] 288 | 289 | successful_cases = 0 290 | failed_cases = [] 291 | 292 | for test_case in test_cases: 293 | 294 | try: 295 | output = target(**test_case['input']) 296 | check_solution = output == test_case['expected'] 297 | except Exception as e: 298 | print(f"\033[91mUnit test broken in {test_case['name']} due to an Exception being thrown. Input was:\n\t{test_case['input']}\nException is: {e}.\nAborting. ") 299 | return 300 | 301 | if not check_solution: 302 | failed_cases.append( 303 | { 304 | "name":f"n value for {test_case['name']}", 305 | "expected":test_case['expected'], 306 | "got":output 307 | 308 | } 309 | 310 | ) 311 | print(f"Wrong value for test case {failed_cases[-1]['name']}. The input values were:\n{test_case['input']}.\nExpected {failed_cases[-1]['expected']}, but got {failed_cases[-1]['got']}.") 312 | else: 313 | successful_cases += 1 314 | 315 | if len(failed_cases) == 0: 316 | print("\033[92m All tests passed") 317 | else: 318 | print("\033[92m", successful_cases, " Tests passed") 319 | print("\033[91m", len(failed_cases), " Tests failed") --------------------------------------------------------------------------------