|
| 1 | +# NumPy |
| 2 | + |
| 3 | +NumPy stands for Numerical Python and it'is a Python library used for working with arrays. |
| 4 | + |
| 5 | +It also has functions for working in domain of linear algebra, fourier transform, and matrices. |
| 6 | + |
| 7 | +## Importing NumPy |
| 8 | +```python |
| 9 | +import numpy as np |
| 10 | +``` |
| 11 | + |
| 12 | +## NumPy Array |
| 13 | + |
| 14 | +### Creating ndarray |
| 15 | + |
| 16 | +The array object in NumPy is called ndarray, which can be created by using the array() function. |
| 17 | + |
| 18 | +#### Basics |
| 19 | +```python |
| 20 | +a0D = np.array(42) |
| 21 | +a1D = np.array([1, 2, 3, 4, 5]) |
| 22 | +a2D = np.array([[1, 2, 3], [4, 5, 6]]) |
| 23 | +a3D = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) |
| 24 | + |
| 25 | +print(a.ndim) #0 |
| 26 | +print(b.ndim) #1 |
| 27 | +print(c.ndim) #2 |
| 28 | +print(d.ndim) #3 |
| 29 | + |
| 30 | +type(a) #<class 'numpy.ndarray'> |
| 31 | +``` |
| 32 | + |
| 33 | +#### Other ways |
| 34 | + |
| 35 | +* np.zeros() |
| 36 | +* np.ones() |
| 37 | +* np.random() |
| 38 | +* np.empty() --> empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution. |
| 39 | +* np.arange(n) --> range of values from 0 to n |
| 40 | +* np.arange(first, limit, increment) |
| 41 | +* np.linspace(first, limit, number_of_elements) |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +```python |
| 46 | +e = np.zeros(2) # [0. 0.] |
| 47 | +f = np.zeros((5,), dtype=int) # [0, 0, 0, 0, 0] |
| 48 | + |
| 49 | +g = np.ones(2) # [1. 1.] |
| 50 | + |
| 51 | +h = np.empty(2) # [ 2.51863511e-048 -2.35668071e+306] |
| 52 | + |
| 53 | +i = np.arange(4) # [0, 1, 2, 3] |
| 54 | +j = np.arange(2, 9, 2) # [2, 4, 6, 8] |
| 55 | + |
| 56 | +k = np.linspace(0, 10, num=5) # [ 0. , 2.5, 5. , 7.5, 10. ] |
| 57 | +``` |
| 58 | + |
| 59 | +### Indexing |
| 60 | + |
| 61 | +```python |
| 62 | +a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) |
| 63 | + |
| 64 | +a[0] # [1, 2, 3, 4] |
| 65 | +a[2] # [9, 10, 11, 12] |
| 66 | +a[2][3] # 12 |
| 67 | +a[2, 3] # 12 |
| 68 | + |
| 69 | +a[:, 2] # [ 3 7 11] --> all rows, column 2 |
| 70 | +a[1, :] # [5 6 7 8] --> row 1, all columns |
| 71 | +a[:, 1:3] # [[ 2 3] --> all rows, column from 1 to 3 (excluding the end) |
| 72 | + # [ 6 7] |
| 73 | + # [10 11]] |
| 74 | + |
| 75 | +a[a < 5] # [1 2 3 4] |
| 76 | +a[a%2==0] # [ 2 4 6 8 10 12] |
| 77 | +a[(a > 2) & (a < 9)] # [3 4 5 6 7 8] |
| 78 | +``` |
| 79 | + |
| 80 | +### Add, delete and order elements |
| 81 | +* np.sort() |
| 82 | +* np.concatenate() |
| 83 | + |
| 84 | +```python |
| 85 | +arr = np.array([2, 1, 5, 3, 7, 4, 6, 8]) |
| 86 | +np.sort(arr) # [1, 2, 3, 4, 5, 6, 7, 8] --> It's a copy, the original remains the same |
| 87 | + |
| 88 | +a = np.array([[10,40,30,20],[30,20,10,40]]) |
| 89 | +print("Order array first axis (col):") |
| 90 | +print(np.sort(a, axis=0)) |
| 91 | + |
| 92 | +a = np.array([1, 2, 3, 4]) |
| 93 | +b = np.array([5, 6, 7, 8]) |
| 94 | +np.concatenate((a, b)) # [1, 2, 3, 4, 5, 6, 7, 8] |
| 95 | +``` |
| 96 | + |
| 97 | +### Dimension and size |
| 98 | +```python |
| 99 | +a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) |
| 100 | +a.ndim # 2 |
| 101 | +a.size # 12 --> 3 rows * 4 col = 12 (total elements) |
| 102 | +a.shape # (3, 4) --> 3 rows, 4 col |
| 103 | + |
| 104 | +b = a.reshape(4, 3) # [[ 1 2 3] |
| 105 | + # [ 4 5 6] |
| 106 | + # [ 7 8 9] |
| 107 | + # [10 11 12]] |
| 108 | + |
| 109 | +a.flatten() # [ 1 2 3 4 5 6 7 8 9 10 11 12] --> matrix to vector |
| 110 | +``` |
| 111 | +### Operations |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +```python |
| 118 | +a = np.array([[0, 1], [2, 3]]) |
| 119 | +np.transpose(a) # [[0, 2], |
| 120 | + # [1, 3]] |
| 121 | + |
| 122 | +# average |
| 123 | +a.mean() # 1.5 |
| 124 | +np.median(a) # 1.5 |
| 125 | + |
| 126 | +# min value |
| 127 | +a.min() # 0 |
| 128 | + |
| 129 | +# max value |
| 130 | +a.max() # 3 |
| 131 | +``` |
| 132 | + |
| 133 | +### Saving and loading variables |
| 134 | + |
| 135 | +```python |
| 136 | +a = np.array([1, 2, 3, 4, 5, 6]) |
| 137 | +np.save('filename', a) |
| 138 | +b = np.load('filename.npy') |
| 139 | + |
| 140 | +# plain text (.csv or .txt) |
| 141 | +csv_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) |
| 142 | +np.savetxt('new_file.csv', csv_arr) |
| 143 | +np.loadtxt('new_file.csv') |
| 144 | +``` |
| 145 | +```python |
| 146 | + |
| 147 | +``` |
| 148 | +```python |
| 149 | + |
| 150 | +``` |
| 151 | +```python |
| 152 | + |
| 153 | +``` |
| 154 | +```python |
| 155 | + |
| 156 | +``` |
0 commit comments