Skip to content

Instantly share code, notes, and snippets.

@NithyaSahadevan
Created January 19, 2019 02:56
Show Gist options
  • Save NithyaSahadevan/7d425fd05d3fb94d43cb74c7f5875c8e to your computer and use it in GitHub Desktop.
Save NithyaSahadevan/7d425fd05d3fb94d43cb74c7f5875c8e 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": [
" <div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"http://cocl.us/NotebooksPython101\"><img src = \"https://ibm.box.com/shared/static/yfe6h4az47ktg2mm9h05wby2n7e8kei3.png\" width = 750, align = \"center\"></a>\n",
"\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": true
},
"outputs": [],
"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: "
]
},
{
"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": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"np.sin(Z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"C=np.array([[1,1],[2,2],[3,3]])\n",
"C"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"C.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"http://cocl.us/NotebooksPython101bottom\"><img src = \"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width = 750, align = \"center\"></a>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright &copy; 2017 [cognitiveclass.ai](cognitiveclass.ai?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/).​"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment