Skip to content

Instantly share code, notes, and snippets.

@prafullkotecha
Created January 13, 2019 18:30
Show Gist options
  • Save prafullkotecha/fb0db24b302889da4c9556c85cbb6f2a to your computer and use it in GitHub Desktop.
Save prafullkotecha/fb0db24b302889da4c9556c85cbb6f2a to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"http://cocl.us/topNotebooksPython101Coursera\"><img src = \"https://ibm.box.com/shared/static/yfe6h4az47ktg2mm9h05wby2n7e8kei3.png\" width = 750, align = \"center\"></a>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://www.bigdatauniversity.com\"><img src = \"https://ibm.box.com/shared/static/ugcqz6ohbvff804xp84y4kqnvvk3bq1g.png\" width = 300, align = \"center\"></a>\n",
"\n",
"\n",
"\n",
"<h1 align=center><font size = 5>numpy in Python</font></h1>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.5/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.\n",
" warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')\n",
"/usr/local/lib/python3.5/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.\n",
" warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')\n"
]
}
],
"source": [
"\n",
"import numpy as np \n",
"import matplotlib.pyplot as plt\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Consider the list **a **, the list contains three nested lists **each of equal size**. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[11, 12, 13], [21, 22, 23], [31, 32, 33]]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=[[11,12,13],[21,22,23],[31,32,33]]\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can cast the list to a numpy array as follow"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[11, 12, 13],\n",
" [21, 22, 23],\n",
" [31, 32, 33]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#every elment if of the same type \n",
"A=np.array(a)\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the attribute **ndim** to obtain the number of axes or dimensions referred to as the rank. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Attribute **shape** returns a tuple corresponding to the size or number of each dimension."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The total number of elements in the array is given by the attribute **size**."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accessing different elements of an Numpy Array "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can use rectangular brackets to access the different elements of the array,The correspondence between the rectangular brackets and the list and the rectangular representation is shown in the following figure for a 3x3 array: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src = \"https://ibm.box.com/shared/static/dsa3nfspbvarlwikt2nuva4ka8ez7stm.png\" width = 500, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can access the 2nd-row 3rd column as shown in the following figure:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src=\"https://ibm.box.com/shared/static/7ezznbkz63cvd39wn6yidqojdqbh9v0u.png\" , width=500,align=\"center\"> </a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We simply use the square brackets and the indices corresponding to the element we would like:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[1,2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can also use the following notation to obtain the elements: "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[1][2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Consider the elements shown in the following figure "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src=\"https://ibm.box.com/shared/static/178texrln4qv2figweqtmgliwwikncnd.png\", width=500,align=\"center\"> </a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" we can access the element as follows "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[0][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can also use slicing in numpy arrays,consider the following figure; we would like to obtain the first two columns in the first row "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src=\"https://ibm.box.com/shared/static/mjmlvk9d9p1fokh5e1ka51bayw8qnv1e.png\", width=500,align=\"center\"> </a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This can be done with the following syntax "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([11, 12])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" A[0][0:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we can obtain the first two rows of the 3rd column as follows:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([13, 23])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[0:2,2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Corresponding to the following figure: The figure incorrectly takes last two rows, whereas it should consider the first two rows, hence the difference in the result and what's shown in the figure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a><img src=\"https://ibm.box.com/shared/static/p9y7q111sq4epvkn2vtvkda5667gyiho.png\", width=500,align=\"center\" ></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Operations "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can also add arrays; the process is identical to matrix addition. Matrix addition of **X** and **Y** is shown in the following figure:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src=\"https://ibm.box.com/shared/static/6fiwxq3nsnpk3ae82t0vbxv8qtz1c1gs.png\", width=500,align=\"center\"> </a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The numpy array is given by **X** and **Y**"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 1]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X=np.array([[1,0],[0,1]]) \n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y=np.array([[2,1],[1,2]]) \n",
"Y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can add the numpy arrays as follows."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 1],\n",
" [1, 3]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Z=X+Y\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Multiplying a numpy array by a scaler is identical to multiplying a matrix by a scaler. If we multiply the matrix **Y** by the scaler 2, we simply multiply every element in the matrix by 2 as shown in the figure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src=\"https://ibm.box.com/shared/static/b942bzigx47l3bfkielh0d36ghurrma8.png\", width=500,align=\"center\"> </a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can perform the same operation in numpy as follows "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y=np.array([[2,1],[1,2]]) \n",
"Y"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[4, 2],\n",
" [2, 4]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Z=2*Y\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Multiplication of two arrays corresponds to an element-wise product or Hadamard product. Consider matrix **X** and **Y**. The Hadamard product corresponds to multiplying each of the elements in the same position, i.e. multiplying elements contained in the same colour boxes together. The result is a new matrix that is the same size as matrix **Y** or **X**, as shown in the following figure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a><img src=\"https://ibm.box.com/shared/static/7yha01bj0orozx9vicpf32p6p4t3f3ii.png\", width=500,align=\"center\"> </a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can perform element-wise product of the array **X** and **Y** as follows:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y=np.array([[2,1],[1,2]]) \n",
"Y"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 1]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X=np.array([[1,0],[0,1]]) \n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 0],\n",
" [0, 2]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Z=X*Y\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can also perform matrix multiplication with the numpy arrays **A** and **B** as follows:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" First, we define matrix **A** and **B**:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1, 1],\n",
" [1, 0, 1]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A=np.array([[0,1,1],[1,0,1]])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 1],\n",
" [ 1, 1],\n",
" [-1, 1]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B=np.array([[1,1],[1,1],[-1,1]])\n",
"B"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use the numpy function **dot** to multiply the arrays together."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 2],\n",
" [0, 2]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Z=np.dot(A,B)\n",
"Z"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0. , 0.90929743],\n",
" [0. , 0.90929743]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sin(Z)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 1],\n",
" [2, 2],\n",
" [3, 3]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C=np.array([[1,1],[2,2],[3,3]])\n",
"C"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [1, 2, 3]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quiz Questions\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 2],\n",
" [0, 2]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Q1: how do you perform matrix multiplication on the numpy arrays A and B\n",
"\n",
"np.dot(A,B)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Q2: what values does the variable out take if the following lines of code are run:\n",
"X=np.array([[1,0,1],[2,2,2]]) \n",
"out=X[0:2,2]\n",
"out"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Q3: What is the value of Z after the following code is run:\n",
"X=np.array([[1,0],[0,1]])\n",
"Y=np.array([[2,1],[1,2]]) \n",
"Z=np.dot(X,Y)\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"<a href=\"http://cocl.us/bottemNotebooksPython101Coursera\"><img src = \"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width = 750, align = \"center\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### About the Authors: \n",
"\n",
" [Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright &copy; 2017 [cognitiveclass.ai](https:cognitiveclass.ai). This notebook and its source code are released under the terms of the [MIT License](cognitiveclass.ai)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment