Shape and Reshaping an Array

The shape of an array refers to the number of elements along each dimension. In NumPy, arrays can have one or more dimensions, also known as axes. The shape of an array is represented as a tuple of integers, where each integer corresponds to the size of the array along a particular axis.

For example, a 1-dimensional array with 5 numbers has a shape of (5,), and a 2-dimensional array with 3 rows and 4 columns has a shape of (3, 4). By the help of shape attribute, you can get the shape of an array.

NumPy provides various functions to manipulate the shape of arrays. Let’s explore these functions:

Flattening an Array

The flatten() method in NumPy is a convenient tool for transforming multi-dimensional arrays into one-dimensional arrays.

When you apply the flatten() method to a NumPy array, it iterates over each element in the array and puts them all into a single one-dimensional array. It starts from the first element of the first dimension, then moves to the second dimension, and so on, until it has included all elements in the resulting flattened array.

Example:

import numpy as np
 
# Let's create a 2D array
matrix = np.array([[1, 2, 3], [4, 5, 6]])

# Flattening a 2D array
flat_matrix = matrix.flatten()
print("Flattened Array:")
print(flat_matrix)

Output :

Flattened Array:
[1 2 3 4 5 6]

Reshaping Arrays

The reshape() function lets you change the shape of arrays without changing the data they contain. It’s handy when you want to rearrange your data into different dimensions. It takes the desired shape as an argument and returns a new array with the specified dimensions.

Example: Reshaping a 1D Array into a 2D Array

import numpy as np
 
# Start with a 1D array
array_1d = np.array([1, 2, 3, 4, 5, 6])
 
# Reshape it into a 2D array (3 rows, 2 columns)
array_2d = array_1d.reshape(3, 2)
 
print("Original 1D Array:")
print(array_1d)
 
print("\nReshaped 2D Array:")
print(array_2d)

Output :

Original Array (1D):
[1 2 3 4 5 6]

Reshaped Array (2D):
[[1 2]
 [3 4]
 [5 6]]

In this example, we’ve transformed the 1D array into a 2D array with three rows and two columns.

Using -1 for Automatic Dimension Inference

Sometimes, you don’t know how many rows or columns you need in your reshaped array. That’s where -1 comes in handy. If you put -1 for one dimension, NumPy will figure out the size for you based on the total number of elements in the array and the size of the other dimension.

Example:

import numpy as np
 
# Start with a 1D array
array_1d = np.array([1, 2, 3, 4, 5, 6])
 
# Reshape it into a 2D array with automatic inference of the number of columns
array_2d = array_1d.reshape(3, -1)
 
print("Original 1D Array:")
print(array_1d)
 
print("\nReshaped 2D Array with Automatic Inference:")
print(array_2d)

Output:

Original 1D Array:
[1 2 3 4 5 6]

Reshaped 2D Array with Automatic Inference:
[[1 2]
 [3 4]
 [5 6]]

Resizing an Array

If you have an array with some numbers and you want to make it longer or shorter, you can use resize(). Unlike reshape(), which only changes the shape without altering the data, resize() can add or remove elements to match the desired size. For example, if you have [1, 2, 3] and you can make it longer to [1, 2, 3, 1, 2, 3].

Example:

import numpy as np
 
# Let's create a 2D array
matrix = np.array([[1, 2, 3], [4, 5, 6]])

# Resizing a 2x3 array into a 3x3 array by adding an extra row
resized_matrix = np.resize(matrix, (3, 3))
print("Resized Array:")
print(resized_matrix)

Output :

Resized Array:
[[1 2 3]
 [4 5 6]
 [1 2 3]]

Transposing Arrays

Imagine you have data arranged in rows and columns. But what if you need it the other way around? Maybe you want the rows to become columns and the columns to become rows. Or perhaps you want to swap data between axes to make it easier to work with. Transposing allows you to do just that—rearrange your data to fit your needs.

In NumPy, you have two main ways to transpose an array:

Using .T Attribute

NumPy arrays come with a built-in attribute called .T that transposes the array. It’s like flipping a matrix along its diagonal. Here’s a simple example:

Example:

import numpy as np
 
# Let's create a 2D array
matrix = np.array([[1, 2, 3], [4, 5, 6]])
 
# Transposing with .T attribute
transposed_matrix = matrix.T
 
print("Original Array:")
print(matrix)
 
print("\nTransposed Array:")
print(transposed_matrix)

Output:

Original Array:
[[1 2 3]
 [4 5 6]]

Transposed Array:
[[1 4]
 [2 5]
 [3 6]]

Using numpy.transpose() Function

If you prefer to use a function, NumPy provides numpy.transpose() to achieve the same result.

Example:

# Transposing with numpy.transpose() function
transposed_matrix = np.transpose(matrix)
 
print("Original Array:")
print(matrix)
 
print("\nTransposed Array:")
print(transposed_matrix)

Output:

Original Array:
[[1 2 3]
[4 5 6]]

Transposed Array:
[[1 4]

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *