Created
April 11, 2020 09:20
-
-
Save nitinmlvya/1ef3a4adba6d1533ee777abc9dfc5f3b to your computer and use it in GitHub Desktop.
Numpy for Data Science
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Numpy\n", | |
"- A Python library that is used for scientific computation over arrays. \n", | |
"- It provides a high-performance multidimensional array object, and tools for working with these arrays.\n", | |
"- A numpy array is a grid of values and all values have same data type.\n", | |
"- It saves memory and speed up the computation.\n", | |
"\n", | |
"<img src=\"https://1.bp.blogspot.com/-6eEKgu0wlh8/XX3rWzKpJ1I/AAAAAAAAVRk/WE1IdKp4z7oZig4ZWq5UnMaemjcCWwB1gCLcBGAsYHQ/s1600/narray2.png\" alt=\"NumPy\" style=\"width: 50%;\"/>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Import Numpy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 112, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Create Numpy array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1D dimensional array: \n", | |
" [1 2 3]\n", | |
"\n", | |
"2D dimensional array: \n", | |
" [[1 2 3]\n", | |
" [5 6 7]]\n", | |
"\n", | |
"3D dimensional array: \n", | |
" [[[ 1 2 3]\n", | |
" [ 5 6 7]]\n", | |
"\n", | |
" [[ 8 9 10]\n", | |
" [11 12 13]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# 1D array\n", | |
"d1 = np.array([1, 2, 3])\n", | |
"print('1D dimensional array: \\n', d1)\n", | |
"\n", | |
"# 2D array\n", | |
"d2 = np.array(\n", | |
" [\n", | |
" [1, 2, 3], \n", | |
" [5, 6, 7]\n", | |
" ]\n", | |
")\n", | |
"print()\n", | |
"print('2D dimensional array: \\n', d2)\n", | |
"\n", | |
"# 3D array\n", | |
"d3 = np.array(\n", | |
" [ \n", | |
" [\n", | |
" [1, 2, 3], \n", | |
" [5, 6, 7]\n", | |
" ],[\n", | |
" [8, 9, 10], \n", | |
" [11, 12, 13]\n", | |
" ]\n", | |
" ]\n", | |
")\n", | |
"print()\n", | |
"print('3D dimensional array: \\n', d3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Get shape of an array/matrix" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Shape of 1D dimensional array: (3,)\n", | |
"Shape of 2D dimensional array: (2, 3)\n", | |
"Shape of 3D dimensional array: (2, 2, 3)\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Shape of 1D dimensional array: ', d1.shape)\n", | |
"print('Shape of 2D dimensional array: ', d2.shape)\n", | |
"print('Shape of 3D dimensional array: ', d3.shape)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Create a range between 1 and 10 and step by 2.\n", | |
"### np.arange(start, end, step)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 3 5 7 9]\n" | |
] | |
} | |
], | |
"source": [ | |
"r = np.arange(1, 10, 2)\n", | |
"print(r)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Reshape an array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 134, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Reshape 1D array into 2 rows and 5 columns: \n", | |
"[[ 1 2 3 4 5]\n", | |
" [ 6 7 8 9 10]]\n", | |
"\n", | |
"Reshape 2D 2x5 into 5x2\n", | |
"[[ 1 2]\n", | |
" [ 3 4]\n", | |
" [ 5 6]\n", | |
" [ 7 8]\n", | |
" [ 9 10]]\n", | |
"\n", | |
"Reshape 3D 2x2x3 into 2x3x2\n", | |
"[[[1 2]\n", | |
" [3 6]\n", | |
" [7 8]]\n", | |
"\n", | |
" [[1 2]\n", | |
" [3 6]\n", | |
" [7 8]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Reshape 1D\n", | |
"d1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n", | |
"print('Reshape 1D array into 2 rows and 5 columns: ')\n", | |
"print(d1.reshape(2, 5))\n", | |
"\n", | |
"print()\n", | |
"# Reshape 2D\n", | |
"d2 = np.array(\n", | |
" [\n", | |
" [1, 2, 3, 4, 5], \n", | |
" [6, 7, 8, 9, 10]\n", | |
" ]\n", | |
")\n", | |
"print('Reshape 2D 2x5 into 5x2')\n", | |
"print(d2.reshape(5, 2))\n", | |
"\n", | |
"print()\n", | |
"# Reshape 3D\n", | |
"d3 = np.array(\n", | |
" [\n", | |
" [\n", | |
" [1, 2, 3], \n", | |
" [6, 7, 8]\n", | |
" ],\n", | |
" [\n", | |
" [1, 2, 3], \n", | |
" [6, 7, 8]\n", | |
" ]\n", | |
" ]\n", | |
")\n", | |
"print('Reshape 3D 2x2x3 into 2x3x2')\n", | |
"print(d3.reshape(2, 3, 2))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Linspace\n", | |
"- np.linspace(start, end, array_size)\n", | |
"- Create a numpy array of equal sized values based on number of sample given." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 135, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Example 1: \n", | |
"[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]\n", | |
"\n", | |
"Example 2:\n", | |
"[ 1. 2.28571429 3.57142857 4.85714286 6.14285714 7.42857143\n", | |
" 8.71428571 10. ]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Example 1: ')\n", | |
"l = np.linspace(1, 10, 10)\n", | |
"print(l)\n", | |
"\n", | |
"print()\n", | |
"print('Example 2:')\n", | |
"l = np.linspace(1, 10, 8)\n", | |
"print(l)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Resize\n", | |
"- Change the shape of array in place, unlike shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 136, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Resize to 4x2: \n", | |
"[[ 1. 2.28571429]\n", | |
" [ 3.57142857 4.85714286]\n", | |
" [ 6.14285714 7.42857143]\n", | |
" [ 8.71428571 10. ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"l = np.linspace(1, 10, 8)\n", | |
"l.resize(4,2)\n", | |
"print('Resize to 4x2: ')\n", | |
"print(l)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Create empty array\n", | |
"- Return arbitary values that can be changed later.\n", | |
"- Use np.zeros() to create zero defined array for better use. Check further cells." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 137, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1.28822975e-231 1.28822975e-231]\n", | |
" [1.28822975e-231 2.82472626e-309]]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1.28822975e-231, 1.28822975e-231])" | |
] | |
}, | |
"execution_count": 137, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x = np.empty((2,2))\n", | |
"print(x)\n", | |
"x[0]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Zeros values in array.\n", | |
"- Create an array that contains only Zeros values." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 138, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1D Zeros array: \n", | |
"[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", | |
"\n", | |
"2D Zeros array: \n", | |
"[[0. 0. 0.]\n", | |
" [0. 0. 0.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# 1D Zeros array\n", | |
"x = np.zeros(10)\n", | |
"print('1D Zeros array: ')\n", | |
"print(x)\n", | |
"\n", | |
"print()\n", | |
"# 2D zeros array\n", | |
"x = np.zeros((2, 3))\n", | |
"print('2D Zeros array: ')\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Ones values in array.\n", | |
"- Create an array that contains only ones values." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 139, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1D ones array: \n", | |
"[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", | |
"\n", | |
"2D ones array: \n", | |
"[[1. 1. 1.]\n", | |
" [1. 1. 1.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# 1D ones array\n", | |
"x = np.ones(10)\n", | |
"print('1D ones array: ')\n", | |
"print(x)\n", | |
"\n", | |
"print()\n", | |
"# 2D ones array\n", | |
"x = np.ones((2, 3))\n", | |
"print('2D ones array: ')\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Diagonal matrix\n", | |
"- Create a diagonal matrix with diagonal values are 1." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 141, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1., 0., 0.],\n", | |
" [0., 1., 0.],\n", | |
" [0., 0., 1.]])" | |
] | |
}, | |
"execution_count": 141, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.eye(3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Constant values\n", | |
"- Fill matrix with same values.\n", | |
"- Values can be changed." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 142, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[5, 5],\n", | |
" [5, 5]])" | |
] | |
}, | |
"execution_count": 142, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.full((2,2), 5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 187, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Create 1D four random numbers:\n", | |
"[0.3637369 0.979445 0.08982104 0.39673661]\n", | |
"\n", | |
"Generate a random integer between 1 and 10: \n", | |
"1\n", | |
"\n", | |
"Generate multiple dimension random integer for shape 2x3: \n", | |
"[[-1.88576241 0.17092537 -0.40309331]\n", | |
" [ 1.63762849 -0.60529785 -1.59441507]]\n", | |
"\n", | |
"Seed value: 5\n", | |
"\n", | |
"Shuffle: \n", | |
"Before shuffling, x: [1 2 3 4 5]\n", | |
"After shuffled, x: [5 1 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"## Random initialize values.\n", | |
"\n", | |
"print('Create 1D four random numbers:')\n", | |
"print(np.random.random(4))\n", | |
"\n", | |
"print()\n", | |
"print('Generate a random integer between 1 and 10: ')\n", | |
"print(np.random.randint(1, 10))\n", | |
"\n", | |
"print()\n", | |
"print('Generate multiple dimension random integer for shape 2x3: ')\n", | |
"print(np.random.randn(2, 3))\n", | |
"\n", | |
"print()\n", | |
"# Seed value is used for randomize algorithm. Randomize algorithm gives different results on every execution.\n", | |
"print('Seed value: 5')\n", | |
"np.random.seed(5)\n", | |
"\n", | |
"print()\n", | |
"# Shuffles the values of numpy array in place.\n", | |
"print('Shuffle: ')\n", | |
"x = np.array([1, 2, 3, 4, 5])\n", | |
"print('Before shuffling, x: ', x)\n", | |
"np.random.shuffle(x)\n", | |
"print('After shuffled, x: ', x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 158, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1., 1., 1.],\n", | |
" [1., 1., 1.]])" | |
] | |
}, | |
"execution_count": 158, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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.7.2" | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment