|
| 1 | +# Concatenation of Arrays |
| 2 | + |
| 3 | +Concatenation of arrays in NumPy refers to combining multiple arrays into a single array, either along existing axes or by adding new axes. NumPy provides several functions for this purpose. |
| 4 | + |
| 5 | +# Functions of Concatenation |
| 6 | + |
| 7 | +## np.concatenate |
| 8 | + |
| 9 | +Joins two or more arrays along an existing axis. |
| 10 | + |
| 11 | +### Syntax |
| 12 | + |
| 13 | +```python |
| 14 | +numpy.concatenate((arr1, arr2, ...), axis) |
| 15 | +``` |
| 16 | + |
| 17 | +Args: |
| 18 | +- arr1, arr2, ...: Sequence of arrays to concatenate. |
| 19 | +- axis: Axis along which the arrays will be joined. Default is 0. |
| 20 | + |
| 21 | +### Example |
| 22 | + |
| 23 | +#### Concatenate along axis 0 |
| 24 | + |
| 25 | +```python |
| 26 | +import numpy as np |
| 27 | +#creating 2 arrays |
| 28 | +arr1 = np.array([1 2 3],[7 8 9]) |
| 29 | +arr2 = np.array([4 5 6],[10 11 12]) |
| 30 | + |
| 31 | +result_1 = np.concatenate((arr1, arr2), axis=0) |
| 32 | +print(result_1) |
| 33 | +``` |
| 34 | + |
| 35 | +#### Output |
| 36 | +``` |
| 37 | +[[ 1 2 3] |
| 38 | + [ 7 8 9] |
| 39 | + [ 4 5 6] |
| 40 | + [10 11 12]] |
| 41 | +``` |
| 42 | + |
| 43 | +#### Concatenate along axis 1 |
| 44 | + |
| 45 | +```python |
| 46 | +result_2 = np.concatenate((arr1, arr2), axis=1) |
| 47 | +print(result_2) |
| 48 | +``` |
| 49 | + |
| 50 | +#### Output |
| 51 | +``` |
| 52 | +[[ 1 2 3 4 5 6 ] |
| 53 | + [ 7 8 9 10 11 12]] |
| 54 | +``` |
| 55 | + |
| 56 | +## np.vstack |
| 57 | + |
| 58 | +Vertical stacking of arrays (row-wise). |
| 59 | + |
| 60 | +### Syntax |
| 61 | + |
| 62 | +```python |
| 63 | +numpy.vstack(arrays) |
| 64 | +``` |
| 65 | + |
| 66 | +Args: |
| 67 | +- arrays: Sequence of arrays to stack. |
| 68 | + |
| 69 | +### Example |
| 70 | + |
| 71 | +```python |
| 72 | +import numpy as np |
| 73 | +#create arrays |
| 74 | +arr1= np.array([1 2 3], [7 8 9]) |
| 75 | +arr2 = np.array([4 5 6],[10 11 12]) |
| 76 | + |
| 77 | +result = np.vstack((arr1, arr2)) |
| 78 | +print(result) |
| 79 | +``` |
| 80 | + |
| 81 | +#### Output |
| 82 | +``` |
| 83 | +[[ 1 2 3] |
| 84 | + [ 7 8 9] |
| 85 | + [ 4 5 6] |
| 86 | + [10 11 12]] |
| 87 | +``` |
| 88 | + |
| 89 | +## 3. np.hstack |
| 90 | + |
| 91 | +Stacks arrays horizontally (column-wise). |
| 92 | + |
| 93 | +### Syntax |
| 94 | + |
| 95 | +```python |
| 96 | +numpy.hstack(arrays) |
| 97 | +``` |
| 98 | + |
| 99 | +Args: |
| 100 | +- arrays: Sequence of arrays to stack. |
| 101 | + |
| 102 | +### Example |
| 103 | + |
| 104 | +```python |
| 105 | +import numpy as np |
| 106 | +#create arrays |
| 107 | +arr1= np.array([1 2 3], [7 8 9]) |
| 108 | +arr2 = np.array([4 5 6],[10 11 12]) |
| 109 | + |
| 110 | +result = np.hstack((arr1, arr2)) |
| 111 | +print(result) |
| 112 | +``` |
| 113 | + |
| 114 | +#### Output |
| 115 | +``` |
| 116 | +[[ 1 2 3] [ 4 5 6] |
| 117 | + [ 7 8 9] [10 11 12]] |
| 118 | +``` |
| 119 | + |
| 120 | +## np.dstack |
| 121 | + |
| 122 | +Stacks arrays along the third axis (depth-wise). |
| 123 | + |
| 124 | +### Syntax |
| 125 | + |
| 126 | +```python |
| 127 | +numpy.dstack(arrays) |
| 128 | +``` |
| 129 | + |
| 130 | +- arrays: Sequence of arrays to stack. |
| 131 | + |
| 132 | +### Example |
| 133 | + |
| 134 | +```python |
| 135 | +import numpy as np |
| 136 | +#create arrays |
| 137 | +arr1= np.array([1 2 3], [7 8 9]) |
| 138 | +arr2 = np.array([4 5 6],[10 11 12]) |
| 139 | + |
| 140 | +result = np.dstack((arr1, arr2)) |
| 141 | +print(result) |
| 142 | +``` |
| 143 | + |
| 144 | +#### Output |
| 145 | +``` |
| 146 | +[[[ 1 4] |
| 147 | + [ 2 5] |
| 148 | + [ 3 6]] |
| 149 | +
|
| 150 | + [[ 7 10] |
| 151 | + [ 8 11] |
| 152 | + [ 9 12]]] |
| 153 | +``` |
| 154 | + |
| 155 | +## np.stack |
| 156 | + |
| 157 | +Joins a sequence of arrays along a new axis. |
| 158 | + |
| 159 | +```python |
| 160 | +numpy.stack(arrays, axis) |
| 161 | +``` |
| 162 | + |
| 163 | +Args: |
| 164 | +- arrays: Sequence of arrays to stack. |
| 165 | + |
| 166 | +### Example |
| 167 | + |
| 168 | +```python |
| 169 | +import numpy as np |
| 170 | +#create arrays |
| 171 | +arr1= np.array([1 2 3], [7 8 9]) |
| 172 | +arr2 = np.array([4 5 6],[10 11 12]) |
| 173 | + |
| 174 | +result = np.stack((arr1, arr2), axis=0) |
| 175 | +print(result) |
| 176 | +``` |
| 177 | + |
| 178 | +#### Output |
| 179 | +``` |
| 180 | +[[[ 1 2 3] |
| 181 | + [ 7 8 9]] |
| 182 | +
|
| 183 | + [[ 4 5 6] |
| 184 | + [10 11 12]]] |
| 185 | +``` |
| 186 | + |
| 187 | +# Concatenation with Mixed Dimensions |
| 188 | + |
| 189 | +When concatenating arrays with different shapes, it's often necessary to reshape them to have compatible dimensions. |
| 190 | + |
| 191 | +## Example |
| 192 | + |
| 193 | +#### Concatenate along axis 0 |
| 194 | + |
| 195 | +```python |
| 196 | +arr1 = np.array([[1, 2, 3], [4, 5, 6]]) |
| 197 | +arr2 = np.array([7, 8, 9]) |
| 198 | + |
| 199 | +result_0= np.concatenate((arr1, arr2[np.newaxis, :]), axis=0) |
| 200 | +print(result_0) |
| 201 | +``` |
| 202 | + |
| 203 | +#### Output |
| 204 | +``` |
| 205 | +[[1 2 3] |
| 206 | + [4 5 6] |
| 207 | + [7 8 9]] |
| 208 | +``` |
| 209 | + |
| 210 | +#### Concatenate along axis 1 |
| 211 | + |
| 212 | +```python |
| 213 | +result_1 = np.concatenate((arr1, arr2[:, np.newaxis]), axis=1) |
| 214 | +print(result_1) |
| 215 | +``` |
| 216 | + |
| 217 | +#### Output |
| 218 | +``` |
| 219 | +[[1 2 3 7] |
| 220 | + [4 5 6 8]] |
| 221 | +``` |
| 222 | + |
| 223 | + |
0 commit comments