|
| 1 | +## Numpy |
| 2 | + |
| 3 | +### Introduction |
| 4 | +- Numpy is an abbreviated form of **Numerical Python** or **Numeric Python**. |
| 5 | +- It is the most basic yet powerful package for scientific computing and data manipulation in Python. |
| 6 | +- It supports large, multidimensional arrays and matrices. |
| 7 | +- It is generally used for Data Analysis and is a part of scientific python. |
| 8 | +- It is an extension module for Python, mostly written in C. |
| 9 | + |
| 10 | +Official Website:https://numpy.org/ |
| 11 | + |
| 12 | +### Why Numpy? |
| 13 | +- Numpy support N-dimensional array. |
| 14 | +- It consumes less memory. |
| 15 | +- NumPy array is faster than Python List |
| 16 | +- We can create an n-dimensional array in python using numpy.array(). |
| 17 | +- Numpy array provide multidimensional slicing on an array |
| 18 | +- Easy for matrix computation |
| 19 | +- In Python Array is not present where list store different types of elements but numpy helps to behave like a normal array to store similar data types. |
| 20 | +- the list need looping to perform scalar operations |
| 21 | + |
| 22 | +Normal List |
| 23 | +``` |
| 24 | +lst=[1,2,3,4] |
| 25 | +#Add 5 to every element |
| 26 | +res=[elem+5 for elem in lst] |
| 27 | +print(res) |
| 28 | +#Result: |
| 29 | +``` |
| 30 | +Output: |
| 31 | +``` |
| 32 | +[6, 7, 8, 9] |
| 33 | +``` |
| 34 | + |
| 35 | +Using Numpy Array |
| 36 | +```python |
| 37 | +import numpy as np |
| 38 | +lst=np.array([1,2,3,4]) |
| 39 | +res=lst+5 |
| 40 | +print(res) |
| 41 | +``` |
| 42 | +Output: |
| 43 | +``` |
| 44 | +[6 7 8 9] |
| 45 | +``` |
| 46 | + |
| 47 | +- Consumes Less Memory as Compared to Python List |
| 48 | +Python List Memory Consumes |
| 49 | +```python |
| 50 | +import sys |
| 51 | +lst=list(range(0,100)) |
| 52 | +#Add 5 to every element |
| 53 | +res=sys.getsizeof(lst) |
| 54 | +print(res) |
| 55 | +``` |
| 56 | +Output: |
| 57 | +``` |
| 58 | +1008 |
| 59 | +``` |
| 60 | + |
| 61 | +Numpy Array Memory Consumes |
| 62 | +```python |
| 63 | +import sys |
| 64 | +import numpy as np |
| 65 | +lst=np.array(range(0,100)) |
| 66 | +res=sys.getsizeof(res) |
| 67 | +print(res) |
| 68 | +``` |
| 69 | +Output: |
| 70 | +``` |
| 71 | +28 |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | +### Install Numpy Array |
| 76 | +- Open command prompt in windows machine and type following command |
| 77 | +```python |
| 78 | +pip install numpy |
| 79 | +``` |
| 80 | +- After Completion of Installation open Jupyter Notebook or python shell to test whether numpy is properly install or not. |
| 81 | +- Type |
| 82 | + |
| 83 | +```python |
| 84 | +import numpy |
| 85 | +``` |
| 86 | +and hit Enter if it does not give any error then your numpy is installed properly(Do not try on Normal command Prompt use python shell or any idle). |
| 87 | + |
| 88 | + |
| 89 | +### import Numpy in Your Program |
| 90 | +write down following line for importing Numpy |
| 91 | +```python |
| 92 | +import numpy |
| 93 | +``` |
| 94 | +or Rename Numpy in Import as follows |
| 95 | +```python |
| 96 | +import numpy as np |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | +### Create Simple Numpy Array |
| 101 | + |
| 102 | +- To Create a simple numpy array use *array()* function. |
| 103 | + |
| 104 | +Syntax: |
| 105 | +```python |
| 106 | +import numpy as np |
| 107 | +Numpy_array=np.array(<Elements iterative>) |
| 108 | +``` |
| 109 | + |
| 110 | +Example: |
| 111 | +```python |
| 112 | +import numpy as np |
| 113 | +np_array=np.array([1,2,3,4,5]) |
| 114 | + |
| 115 | +#print Type of Array |
| 116 | +print(type(np_array)) |
| 117 | + |
| 118 | +#print numpy array |
| 119 | +print(np_array) |
| 120 | +``` |
| 121 | +Output: |
| 122 | +``` |
| 123 | +<class 'numpy.ndarray'> |
| 124 | +[1 2 3 4 5] |
| 125 | +``` |
| 126 | + |
0 commit comments