{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/09_Python_NumPy_Module)**\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python NumPy\n", "\n", "In this class, you will learn various NumPy concepts like how to install NumPy, arrays, functions, matrix multiplication, etc. This NumPy in Python tutorial will help you learn all Python NumPy basics..\n", "\n", "**[Numpy](http://www.numpy.org/)** (‘Numerical Python’) is the core open source library for scientific computing in Python. It is a very useful library to perform mathematical and statistical operations in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. In this part, we will review the essential functions that you need to know for the tutorial on 'TensorFlow.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why use NumPy?\n", "\n", "NumPy is memory efficiency, meaning it can handle the vast amount of data more accessible than any other library. Besides, NumPy is very convenient to work with, especially for matrix multiplication and reshaping. On top of that, NumPy is fast. In fact, TensorFlow and Scikit learn to use NumPy array to compute the matrix multiplication in the back end." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python NumPy Array: \n", "\n", "A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.\n", "\n", "Numpy array is a powerful N-dimensional array object which is in the form of rows and columns. We can initialize NumPy arrays from nested Python lists and access it elements." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to Install NumPy?\n", "\n", "NumPy is installed by default with Anaconda.\n", "\n", "In remote case, NumPy not installed-\n", "\n", "You can install NumPy using Anaconda:\n", "\n", "```puthon\n", "conda install -c anaconda numpy\t\t\n", "```\n", "In Jupyter Notebook :\n", "\n", "```python\n", ">>> import sys\n", ">>> !conda install --yes --prefix {sys.prefix} numpy\n", "```" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "### Import NumPy and Check Version\n", "\n", "The command to import numpy is\n", "\n", "```python\n", "import numpy as np\n", "```\n", "Above code renames the Numpy namespace to np. This permits us to prefix Numpy function, methods, and attributes with \" np \" instead of typing \" numpy.\" It is the standard shortcut you will find in the numpy literature" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-27T16:41:31.257895Z", "start_time": "2021-05-27T16:41:31.250082Z" }, "hidden": true }, "outputs": [], "source": [ "# To check your installed version of Numpy use the command\n", "\n", "print (np.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### NumPy Basics\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.array([1,2,3])`** | **1d array** | \n", "| **`np.array([(1,2,3),(4,5,6)])`** | **2d array** | \n", "| **`np.arange(start,stop,step)`** | **range array** | " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Placeholders\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.linspace(0,2,9)`** | **Add evenly spaced values btw interval to array of length** |\n", "| **`np.zeros((1,2))`** | **Create and array filled with zeros** |\n", "| **`np.ones((1,2))`** | **Creates an array filled with ones** |\n", "| **`np.random.random((5,5))`** | **Creates random array** |\n", "| **`np.empty((2,2))`** | **Creates an empty array** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`array.shape`** | **Dimensions (Rows,Columns)** |\n", "| **`len(array)`** | **Length of Array** |\n", "| **`array.ndim`** | **Number of Array Dimensions** |\n", "| **`array.dtype`** | **Data Type** |\n", "| **`array.astype(type)`** | **Converts to Data Type** |\n", "| **`type(array)`** | **Type of Array** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Copying/Sorting\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.copy(array)`** | **Creates copy of array** |\n", "| **`other = array.copy()`** | **Creates deep copy of array** |\n", "| **`array.sort()`** | **Sorts an array** |\n", "| **`array.sort(axis=0)`** | **Sorts axis of array** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Manipulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding or Removing Elements\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.append(a,b)`** | **Append items to array** |\n", "| **`np.insert(array, 1, 2, axis)`** | **Insert items into array at axis 0 or 1** |\n", "| **`np.resize((2,4))`** | **Resize array to shape(2,4)** |\n", "| **`np.delete(array,1,axis)`** | **Deletes items from array** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combining Arrays\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.concatenate((a,b),axis=0)`** | **Split an array into multiple sub-arrays.** |\n", "| **`np.vstack((a,b))`** | **Split an array in sub-arrays of (nearly) identical size** |\n", "| **`np.hstack((a,b))`** | **Split the array horizontally at 3rd index** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`other = ndarray.flatten()`** | **Flattens a 2d array to 1d** |\n", "| **`array = np.transpose(other)`** | **Transpose array** |\n", "| **`array.T`** | **Transpose array** |\n", "| **`inverse = np.linalg.inv(matrix)`** | **Inverse of a given matrix** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing and Subsetting\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`array[i]`** | **1d array at index i** |\n", "| **`array[i,j]`** | **2d array at index[i][j]** |\n", "| **`array[i<4]`** | **Boolean Indexing, see Tricks** |\n", "| **`array[0:3]`** | **Select items of index 0, 1 and 2** |\n", "| **`array[0:2,1]`** | **Select items of rows 0 and 1 at column 1** |\n", "| **`array[:1]`** | **Select items of row 0 (equals array[0:1, :])** |\n", "| **`array[1:2, :]`** | **Select items of row 1** |\n", "| **`[comment]: <> (`** | **array[1,...]** |\n", "| **`array[ : :-1]`** | **Reverses array** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mathematics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operations\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.add(x,y)`** | **Addition** |\n", "| **`np.substract(x,y)`** | **Subtraction** |\n", "| **`np.divide(x,y)`** | **Division** |\n", "| **`np.multiply(x,y)`** | **Multiplication** |\n", "| **`np.sqrt(x)`** | **Square Root** |\n", "| **`np.sin(x)`** | **Element-wise sine** |\n", "| **`np.cos(x)`** | **Element-wise cosine** |\n", "| **`np.log(x)`** | **Element-wise natural log** |\n", "| **`np.dot(x,y)`** | **Dot product** |\n", "| **`np.roots([1,0,-4])`** | **Roots of a given polynomial coefficients** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison\n", "\n", "| Operator | Description |\n", "|:----: |:---- |\n", "| **`==`** | **Equal** |\n", "| **`!=`** | **Not equal** |\n", "| **`<`** | **Smaller than** |\n", "| **`>`** | **Greater than** |\n", "| **`<=`** | **Smaller than or equal** |\n", "| **`>=`** | **Greater than or equal** |\n", "| **`np.array_equal(x,y)`** | **Array-wise comparison** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Statistics\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`np.mean(array)`** | **Mean** |\n", "| **`np.median(array)`** | **Median** |\n", "| **`array.corrcoef()`** | **Correlation Coefficient** |\n", "| **`np.std(array)`** | **Standard Deviation** |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More\n", "\n", "| Operator | Description |\n", "|:---- |:---- |\n", "| **`array.sum()`** | **Array-wise sum** |\n", "| **`array.min()`** | **Array-wise minimum value** |\n", "| **`array.max(axis=0)`** | **Maximum value of specified axis** |\n", "| **`array.cumsum(axis=0)`** | **Cumulative sum of specified axis** |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "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.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "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 }