{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"RollNumber_partA_PA1.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"WdFUrY9aLUhI"},"source":["## CS535/EE514 - Spring 2022 - Assignment 1 - Part A\n","\n","#### Important Instructions and Submission Guidelines:\n","- Naming convention for submission of this notebook is `RollNumber_partA_PA1.ipynb` where. For example: `23100042_partA_PA1.ipynb`\n","- All the cells must be run once before submission. If your submission's cells are not showing the results (plots etc.), marks wil be deducted\n","- Only the code written within this notebook's marked areas will be considered while grading. No other files will be entertained\n","- You are advised to follow good programming practies including approriate variable naming and making use of logical comments \n","\n","\n","The university honor code should be maintained. Any violation, if found, will result in disciplinary action. \n"]},{"cell_type":"markdown","metadata":{"id":"dClE8ipfLWKI"},"source":["Double click here to enter your name and roll number: \n","Name: \n","\n","Roll Number: \n",""]},{"cell_type":"markdown","source":["# Setup"],"metadata":{"id":"bo-kd-r5ftrz"}},{"cell_type":"markdown","source":["**Install packages/libraries.**
\n","Run the following code to install the relevant libraries on your machine.\n"],"metadata":{"id":"-E-RJnOMfrij"}},{"cell_type":"code","source":["!pip install scipy==1.1.0\n","!pip install numpy\n","!pip install matplotlib\n","!pip install pandas\n","!pip install python-mnist\n","!pip install sklearn\n","!pip install scikit-image\n","!pip install Wave\n","!pip install os-sys\n","!pip install python-math\n","!pip install random2\n","!pip install scikit-learn"],"metadata":{"id":"ZoiKwsFofl1b"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Import packages/libraries.**
\n","Run the following code to check if the relevant libraries are installed on your machine.\n"],"metadata":{"id":"RuTM0sR0gmaJ"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"otRAC8KaKMAe"},"outputs":[],"source":["import numpy as np\n","import scipy as sp\n","from scipy.linalg import null_space\n","from numpy.linalg import inv\n","from matplotlib.image import imread\n","import matplotlib.pyplot as plt\n","import pandas as pd\n","import math\n","from scipy import stats\n","import random\n","from numpy.random import seed\n","from numpy.random import rand\n","from numpy.random import randn\n","from scipy.stats import pearsonr\n","from scipy.stats import ttest_ind\n","from scipy.stats import mannwhitneyu\n","from mnist import MNIST\n","from sklearn.preprocessing import MinMaxScaler\n","from sklearn.svm import SVC\n","from sklearn import metrics\n","from skimage.feature import hog\n","from sklearn.decomposition import PCA\n","from scipy.io import wavfile\n","from scipy.io.wavfile import write\n","import wave\n","import sys"]},{"cell_type":"markdown","source":["# Task 1: Lists, Dictonaries, Tuples (10 Marks)"],"metadata":{"id":"XFUG6e__g3B3"}},{"cell_type":"markdown","source":["[Lists](https://www.geeksforgeeks.org/python-list/?ref=lbp):A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.
\n","[Dictionaries](https://www.geeksforgeeks.org/python-dictionary/?ref=lbp):Dictionary in Python is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair.
\n","[Tupules](https://www.geeksforgeeks.org/python-tuples/?ref=lbp):Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
"],"metadata":{"id":"6MWrOJDgkqFZ"}},{"cell_type":"markdown","source":["**Lists**
\n","Given a list $nums = [3, 4, 7, 8, 15]$
\n","Make another list named cubes and append the cubes of the given list in this list and print it."],"metadata":{"id":"ruXgEIaWg1pQ"}},{"cell_type":"code","source":["nums = [3, 4, 7, 8, 15]\n","cubes = None \n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"ij_64CBIhxzx"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Dictionaries**
\n","You are given an empty dictionary *dictionary = {}*
\n","\n","\n","* Add the following data to the dictionary: \n"," $'person': 2, 'cat': 4, 'spider': 8, 'horse': 4$
\n"," as key value pairs.
\n","* Use the $items$ method to loop over the dictionary and print the animals and their corresponding legs.\n","* Sum the legs of each animal, and print the total at the end."],"metadata":{"id":"OVjwARa_jD3u"}},{"cell_type":"code","source":["dictionary = {}\n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"4DE2OSrwivev"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Tuples**
\n","Given the following tuple: $D = (1, 15, 4, [5, 10])$
\n","* Change the value in the list from $5$ to $3$.\n","* Delete the tuple $D$. \n","\n","Given another tuple: $E = ('a', 'p', 'p', 'l', 'e')$
\n","* Print the number of occurences of $p$ in tuple E.\n","* Print the index of $l$ in tuple E."],"metadata":{"id":"cvd01d4ukzPL"}},{"cell_type":"code","source":["D = (1, 15, 4, [5, 10])\n","# Enter code here\n","\n","# ----------------\n","print(D)\n","E = (‘a’, ‘p’, ‘p’, ‘l’, ‘e’)\n","# Enter code here\n","\n","# ----------------\n","print(E)"],"metadata":{"id":"rax4rXzJl0QV"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Task 2: Numpy (15 marks)"],"metadata":{"id":"nSvmdiC6owyd"}},{"cell_type":"markdown","source":["You should use only built in functions of numpy in this task, a list is available [here](https://numpy.org/doc/1.19/genindex.html).
\n","\n","\\begin{align}\n"," M = \\begin{pmatrix}\n"," 1 & 2 & 3 & 4\\\\\n"," 5 & 6 & 7 & 8\\\\\n"," 9 & 10 & 11 & 12 \n"," \\end{pmatrix}.\n","\\end{align}\n","\n","$z = np.array([1, 0, 1])$"],"metadata":{"id":"hy3YOsGmo9Zf"}},{"cell_type":"markdown","source":["Convert matrix $M$ into numpy array."],"metadata":{"id":"SsqOCnVxtjer"}},{"cell_type":"code","source":["M = None\n","# Enter code here\n","\n","# ----------------\n","print(M, '\\n' ,type(M))"],"metadata":{"id":"Ms-SW3SUtska"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Use slicing to pull out the subarray consisting of the first 2 rows and columns 1 and 2. Store it in $b$ which is a numpy array of shape $(2, 2)$."],"metadata":{"id":"C8NnijpvuJ7K"}},{"cell_type":"code","source":["b = None\n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"3tCtXlKeuQcc"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Create an empty matrix $y$ with the same shape as $M$.
\n","Add the vector $z$ to each column of the matrix $M$ with an explicit loop and store it in $y$."],"metadata":{"id":"CaWZtVUeuLcl"}},{"cell_type":"code","source":["z = np.array([1,0,1])\n","y = None\n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"3nWuWPWEut1y"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Sum up the elements of each column of $M$ and store it in a vector $col$.
\n","Sum up the elements of each row of $M$ and store it in a vector $row$.
\n","*Note: Do not use for loops for this, Use numpy functions.*"],"metadata":{"id":"3Nx1j6h_d_cK"}},{"cell_type":"code","source":["row = None\n","col = None\n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"fIudIxbJd-sJ"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Given:
\n","$A = np.array([ [1, 2], [3, 4] ])$
\n","$B = np.array([ [5, 6], [7, 8] ])$
\n","$v = np.array([9, 10])$
"],"metadata":{"id":"EDEnkXRfxI9l"}},{"cell_type":"markdown","source":["1. Add the two matrices A and B. Store in $sum$.
\n","2. Multiply the two matrices A and B. Store in $prod$.
\n","3. Take the element wise square root of matrix A. Store in $sqroot$.
\n","4. Take the dot product of the matrix A and vector v. Store in $dotprod$.
\n","5. Compute sum of each column of A. Store in $colsum$.
\n","6. Compute the transpose of B. Store in $trans$.
"],"metadata":{"id":"OF04jjHbxp4B"}},{"cell_type":"code","source":["sum, prod, sqroot, dotprod, colsum, trans = None, None, None, None, None, None\n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"xUgxL6lxyovx"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Display Results:"],"metadata":{"id":"YRaBR4HWy9hK"}},{"cell_type":"code","source":["print(sum, '\\n', prod, '\\n', sqroot, '\\n', dotprod, '\\n', colsum, '\\n', trans)"],"metadata":{"id":"NjDCCXfczAkl"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":[""],"metadata":{"id":"kBWbb8V-g8Tu"}},{"cell_type":"markdown","source":["### Calculate Eigen Vectors:"],"metadata":{"id":"HFqT7WE6X8ac"}},{"cell_type":"markdown","source":["\\begin{align}\n"," A = \\begin{pmatrix}\n"," 1 & -2 \\\\\n"," 3 & -4 \\\\\n"," \\end{pmatrix}.\n","\\end{align}\n","The eigen values of the matrix are $-2$ and $-1$.\n","\n","Use appropriate numpy functions for each of the following steps:
\n","\n","1. Create an identity matrix $I$ of size 2 x 2.\n","2. Multiply each eigen value with $I$ to form matrix $D1$ and $D2$. \n","3. Subtract $D1$ and $D2$ from $A$ and store in $B1$ and $B2$.\n","4. Find the null_space of $B1$ and $B2$ to find the eigen vectors of $A$.\n","5. Print the eigen vectors of $A$."],"metadata":{"id":"7JEPzvpQbOJ4"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"f2mNaz0hbNUz"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Task 3: Functions and For Loops (10 marks)"],"metadata":{"id":"o5TxMfu0zV7m"}},{"cell_type":"markdown","source":["**Function**
\n","Declare a function $Compute$ that takes two arguments: $distance$ and $time$, and use it to calculate $velocity$.\n"],"metadata":{"id":"qNoV7UZAzZa5"}},{"cell_type":"code","source":["# Enter function here\n","\n","# ----------------"],"metadata":{"id":"EUYo-Ycjzs4x"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Let's test the function!\n","Feel free to change the values of the parameters and see the result."],"metadata":{"id":"Een9PxKr0gBU"}},{"cell_type":"code","source":["distance = 10 \n","time = 2\n","velocity = Compute(distance, time)\n","print(\"The velocity is:\",velocity)"],"metadata":{"id":"b885ch330th_"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Forloop**
\n","* Declare a list $even$ that contains all even numbers up till 16. \n","* Declare a function $Sum$ that takes a $list$ as an argument and calculates the $sum$ of all entries using a for loop.\n"],"metadata":{"id":"bNHVdEtSz3gf"}},{"cell_type":"code","source":["even = None\n","# Enter code here\n","\n","# ----------------"],"metadata":{"id":"43bkaZGw0XS3"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Enter function here\n","\n","# ----------------"],"metadata":{"id":"LedTHdWL1OH9"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Let's test the function by passing $even$ as the parameter. "],"metadata":{"id":"cjZ7HxbD1Vh9"}},{"cell_type":"code","source":["print(\"The sum of the list is:\",Sum(even))"],"metadata":{"id":"wUdUi52I1fAr"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Task 4: Matplotlib (15 marks)"],"metadata":{"id":"uaBfqFTL14vm"}},{"cell_type":"markdown","source":["**Plotting a single line**
\n","Compute the x and y coordinates for points on a sine curve and plot the points using matplotlib.
\n","Use the function [plt.show()](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.show.html)."],"metadata":{"id":"9npv6VTI17Ub"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"BCvC_rse2YYj"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Plotting multiple lines**
\n","Compute the x and y coordinates for points on sine and cosine curves and plot them on the same graph using matplotlib.
\n","Add x and y labels to the graph as well."],"metadata":{"id":"5b6W8P_G2tg5"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"zvTYkLC3236F"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Subplots**
\n","Compute the x and y coordinates for points on sine and cosine curves.
\n","Set up a subplot grid that has height 2 and width 1, and set the first such subplot as active.
\n","Plot the sine and cosine graphs.
\n","*Hint: Use the [plt.subplot()](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.subplot.html) function.*"],"metadata":{"id":"eu6ceGa327sm"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"H3lQV3fb3ex5"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### Scatterplots:\n","In this part we will generate a random dataset for a population. We will look into the age and height of 30 people."],"metadata":{"id":"a34d39GSYGae"}},{"cell_type":"markdown","source":["**Age:**
\n","1. Create a 1-Dimensional numpy array of size 30 and data type uint8.\n","2. Fill this array with random numbers within the range (10,70) years inclusive. \n","**Height:**
\n","1. Create a 1-Dimensional numpy array of size 30 and data type float32.\n","2. Fill this array with random numbers within the range (4.5,7.2) ft inclusive. "],"metadata":{"id":"LBYm4g0qXULw"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"4R1beOKeamL2"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Population:**
\n","Create a 2-Dimensional numpy array which stores (age,height) for each person.\n","
\n","\n","*Example:*
\n","*Suppose there are 2 people. One has age and height 30 and 5.0, and the other has age and height 45 and 6.2. The 2-D array should look like this:*
\n","$array = [[30, 5.0], [45, 6.2]]$"],"metadata":{"id":"YOGE7WvOamV_"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"yH0zgIXrbda2"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Make a scatterplot for each of the following:\n","\n","\n","1. People with age greater than 20 yrs and less than 60 yrs. (blue)\n","2. People with height greater than 6.5 ft. (yellow)\n","3. People with height less than 5.0 ft. (red)\n","4. People that fulfil condition 1. and do not fulfil condition 2. or 3. (green)\n","\n","*Note: Display all the points on the same scatterplot in the colors specified. Use different shapes for each scatterplot as well.*\n","\n","\n","\n"],"metadata":{"id":"q1ycu-q0bd0m"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"eKt4QuT5c6Gn"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Task 5: Pandas DataFrame (15 marks)"],"metadata":{"id":"LJug_GBO3p3i"}},{"cell_type":"markdown","source":["**Making a DataFrame**
\n","Create a dataframe pd that contains 5 rows and 4 columns, similar to the one given below:
\n",""],"metadata":{"id":"fmcQkq1W3sod"}},{"cell_type":"code","source":["pd = None\n","# Enter code here\n","\n","# --------------"],"metadata":{"id":"JFDEQ7Z1bnEF"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Print only the first two rows of the dataframe."],"metadata":{"id":"JFswlXTAZfiF"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"LpsZVZLMZnIM"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Print the second column."],"metadata":{"id":"wBMLdlaKZsCE"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------"],"metadata":{"id":"5my1prA-Z-XN"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Change the name of the third column from $Col3$ to $XYZ$."],"metadata":{"id":"xAxNtshBZyCl"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------\n","display(pd)"],"metadata":{"id":"8hg0LeW4ZwUk"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Add a new column to the dataframe and name it $Sum$."],"metadata":{"id":"ENLYUi73Z7ND"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------\n","display(pd)"],"metadata":{"id":"0pVyo7J0aBKb"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Sum the entries of each row and add the result in the column $Sum$."],"metadata":{"id":"y_5SUAq3aEbL"}},{"cell_type":"code","source":["# Enter code here\n","\n","# ----------------\n","display(pd)"],"metadata":{"id":"hKrJ9RG6aEGE"},"execution_count":null,"outputs":[]}]}