NumPy

Array Manipulation

Concatenating arrays

This is an array manipulation tutorial.

In NumPy, you can concatenate arrays using the “concatenate()” function. This function takes a sequence of arrays and concatenates them along a specified axis. For instance, if axis equals 0, then NumPy concatenates along the rows of the arrays. In contrast, if axis equals 1, NumPy concatenates along the arrays’ columns.

Let’s understand the function 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 outcomes.

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

Splitting arrays

NumPy can split arrays into smaller arrays using the “split()” function. This function takes an array to split and indices or sections at which to split the array. For instance, if there is one number as an argument, NumPy splits the array into equal parts. If there is a list as an argument, NumPy splits the array into specific parts (the list numbers provide NumPy with the indices of where to split).

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)

The following are the outputs.

# 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])]

Reshaping arrays

In NumPy, you can reshape arrays using the “reshape()” function. Reshaping an array means changing the shape (dimensions) of the array while keeping the same elements. For instance, if an array has 2×3 shape (dimension), NumPy can reshape it to 3×2 dimension by specifying it as an argument.

Let’s work through 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]]

Transposing arrays

NumPy can transpose arrays using the “transpose()” function. Transposing an array means flipping its shape along the main diagonal. For instance, if there is a two dimensional (2D) array, NumPy swaps the rows and columns of the array by specifying it as an argument.

Let’s comprehend this through a practical 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)

The following is an example.

# Original array
[[1 2 3]
 [4 5 6]]

# Transposed array
[[1 4]
 [2 5]
 [3 6]]

This is an original array manipulation educational material created by aicorr.com.

Next: Indexing and Slicing

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.