array connection
this is Array Manipulation tutorial.
In NumPy, you can concatenate arrays using “.Join together in a chain ()” function. This function takes a series of arrays and concatenates them along the specified axis. For example, if the axis is 0, NumPy will concatenate along the rows of the array. Conversely, if the axis is 1, NumPy will concatenate along the columns of the array.
Let us understand the functionality through an example.
import numpy as np # Create sample arrays arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [10, 11, 12]]) # Concatenate axis 0 (rows) result_axis0 = np.concatenate((arr1, arr2), axis=0) print(result_axis0) # Concatenate axis 1 (columns) result_axis1 = np.concatenate((arr1, arr2), axis=1) print(result_axis1)
Below are the results.
[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] [[ 1 2 3 7 8 9] [ 4 5 6 10 11 12]]
Array Splitting
NumPy allows you to split an array into smaller arrays using “.divided()” function. This function takes an array to split and an index or section to split the array into. For example, if you have one number as an argument, NumPy will split the array into equal parts. If you have a list as an argument, NumPy splits the array into specific parts. (The list number gives NumPy an index into where the split is).
Below is a practical example.
import numpy as np # Ssample array arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # Split the array into three equal parts result = np.split(arr, 3) print(result) # Split the array at specified indices result_indices = np.split(arr, [2, 5]) print(result_indices)
Here is the output:
# Three equal parts [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])] # Three parts (split on index 2 and index 5) [array([1, 2]), array([3, 4, 5]), array([6, 7, 8, 9])]
Array Reconstruction
In NumPy, you can change the shape of an array using “.change shape()” Function. Changing the shape of an array means changing its shape (dimension) while keeping the same elements. For example, if the shape (dimension) of the array is 2×3, NumPy specifies this as an argument. You can change the array to 3×2 dimensions.
Let’s look at an example.
import numpy as np # Sample array arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) # Reshape the array into a 3x2 matrix reshaped_arr = np.reshape(arr, (3, 2)) print(reshaped_arr)
Below is an example.
# Original array [[1 2 3] [4 5 6]] # Reshaped array [[1 2] [3 4] [5 6]]
Array Transpose
NumPy allows you to transpose arrays using “.change()” function. Transposing an array means flipping its shape along the main diagonal. For example, if you have a two-dimensional (2D) array, NumPy will swap the rows and columns of the array by specifying it as an argument.
Let us understand this through a real example.
import numpy as np # Sample array arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) # Transpose the array transposed_arr1 = np.transpose(arr) print(transposed_arr1)
Here is an example:
# Original array [[1 2 3] [4 5 6]] # Transposed array [[1 4] [2 5] [3 6]]
this is the original Array Manipulation This is an educational material created by aicorr.com.
Next: Indexing and Slicing