diff --git a/contrib/numpy/concatenation-of-arrays.md b/contrib/numpy/concatenation-of-arrays.md new file mode 100644 index 0000000..bf27512 --- /dev/null +++ b/contrib/numpy/concatenation-of-arrays.md @@ -0,0 +1,223 @@ +# Concatenation of Arrays + +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. + +# Functions of Concatenation + +## np.concatenate + +Joins two or more arrays along an existing axis. + +### Syntax + +```python +numpy.concatenate((arr1, arr2, ...), axis) +``` + +Args: +- arr1, arr2, ...: Sequence of arrays to concatenate. +- axis: Axis along which the arrays will be joined. Default is 0. + +### Example + +#### Concatenate along axis 0 + +```python +import numpy as np +#creating 2 arrays +arr1 = np.array([1 2 3],[7 8 9]) +arr2 = np.array([4 5 6],[10 11 12]) + +result_1 = np.concatenate((arr1, arr2), axis=0) +print(result_1) +``` + +#### Output +``` +[[ 1 2 3] + [ 7 8 9] + [ 4 5 6] + [10 11 12]] +``` + +#### Concatenate along axis 1 + +```python +result_2 = np.concatenate((arr1, arr2), axis=1) +print(result_2) +``` + +#### Output +``` +[[ 1 2 3 4 5 6 ] + [ 7 8 9 10 11 12]] +``` + +## np.vstack + +Vertical stacking of arrays (row-wise). + +### Syntax + +```python +numpy.vstack(arrays) +``` + +Args: +- arrays: Sequence of arrays to stack. + +### Example + +```python +import numpy as np +#create arrays +arr1= np.array([1 2 3], [7 8 9]) +arr2 = np.array([4 5 6],[10 11 12]) + +result = np.vstack((arr1, arr2)) +print(result) +``` + +#### Output +``` +[[ 1 2 3] + [ 7 8 9] + [ 4 5 6] + [10 11 12]] +``` + +## 3. np.hstack + +Stacks arrays horizontally (column-wise). + +### Syntax + +```python +numpy.hstack(arrays) +``` + +Args: +- arrays: Sequence of arrays to stack. + +### Example + +```python +import numpy as np +#create arrays +arr1= np.array([1 2 3], [7 8 9]) +arr2 = np.array([4 5 6],[10 11 12]) + +result = np.hstack((arr1, arr2)) +print(result) +``` + +#### Output +``` +[[ 1 2 3] [ 4 5 6] + [ 7 8 9] [10 11 12]] +``` + +## np.dstack + +Stacks arrays along the third axis (depth-wise). + +### Syntax + +```python +numpy.dstack(arrays) +``` + +- arrays: Sequence of arrays to stack. + +### Example + +```python +import numpy as np +#create arrays +arr1= np.array([1 2 3], [7 8 9]) +arr2 = np.array([4 5 6],[10 11 12]) + +result = np.dstack((arr1, arr2)) +print(result) +``` + +#### Output +``` +[[[ 1 4] + [ 2 5] + [ 3 6]] + + [[ 7 10] + [ 8 11] + [ 9 12]]] +``` + +## np.stack + +Joins a sequence of arrays along a new axis. + +```python +numpy.stack(arrays, axis) +``` + +Args: +- arrays: Sequence of arrays to stack. + +### Example + +```python +import numpy as np +#create arrays +arr1= np.array([1 2 3], [7 8 9]) +arr2 = np.array([4 5 6],[10 11 12]) + +result = np.stack((arr1, arr2), axis=0) +print(result) +``` + +#### Output +``` +[[[ 1 2 3] + [ 7 8 9]] + + [[ 4 5 6] + [10 11 12]]] +``` + +# Concatenation with Mixed Dimensions + +When concatenating arrays with different shapes, it's often necessary to reshape them to have compatible dimensions. + +## Example + +#### Concatenate along axis 0 + +```python +arr1 = np.array([[1, 2, 3], [4, 5, 6]]) +arr2 = np.array([7, 8, 9]) + +result_0= np.concatenate((arr1, arr2[np.newaxis, :]), axis=0) +print(result_0) +``` + +#### Output +``` +[[1 2 3] + [4 5 6] + [7 8 9]] +``` + +#### Concatenate along axis 1 + +```python +result_1 = np.concatenate((arr1, arr2[:, np.newaxis]), axis=1) +print(result_1) +``` + +#### Output +``` +[[1 2 3 7] + [4 5 6 8]] +``` + + diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index b2d459a..fb07865 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -8,3 +8,4 @@ - [Loading Arrays from Files](loading_arrays_from_files.md) - [Saving Numpy Arrays into FIles](saving_numpy_arrays_to_files.md) - [Sorting NumPy Arrays](sorting-array.md) +- [Concatenation of Arrays](concatenation-of-arrays.md)