NumPy reshaping is a vital feature for manipulating array shapes without compromising data integrity. Here’s a concise overview:
NumPy reshaping allows seamless alteration of array shapes while preserving data integrity, essential for adapting arrays to various dimensions and formats.
Let’s understand the reshaping and its function with example.
reshape
The reshape
function in NumPy allows you to change the shape of an array without modifying its data. Here’s a quick overview:
Syntax: numpy.reshape(array, new_shape)
Parameters:
array
: The original array to be reshaped.new_shape
: The desired shape specified as a tuple of integers.# Original 1D array
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape into a 2x3 matrix
reshaped_arr = np.reshape(arr, (2, 3))
print(reshaped_arr)
We can also approach it in this manner:
arr.reshape(2,3)
This example reshapes the original 1D array into a 2×3 matrix, maintaining the order of elements.
resiz
eThe resize
function in NumPy is used to alter the shape and size of an array. It differs from reshape
in that it can change both the shape and the size of the array by either repeating or truncating elements as necessary to achieve the desired size.
Syntax: numpy.resize(array, new_shape)
Parameters:
array
: The original array to be resized.new_shape
: The desired shape of the resized array specified as a tuple of integers.# Original array
arr = np.array([1, 2, 3, 4, 5, 6])
# Resize the array to shape (2, 4)
np.resize(arr, (2, 4))
print(arr)
Observe that the numbers 1 and 2 are repeated when the current length cannot be completely accommodated.
In this example, the original array is resized to shape (2, 4), resulting in repetition of elements to fill the new shape. The original array is modified in place to match the new shape.
flatten
and ravel
Both flatten
and ravel
functions are used to convert multi-dimensional arrays into one-dimensional arrays. Here’s a concise explanation of each:
flatten
flatten
function creates a new one-dimensional array by unraveling the elements of the input multi-dimensional array, preserving the order of elements.# Original 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Flatten the array
flattened_arr = arr.flatten()
print(flattened_arr)
ravel
ravel
function also creates a one-dimensional array by unraveling the elements of the input multi-dimensional array.flatten
, ravel
may return a view of the original array if possible, which means it may share memory with the original array instead of creating a new copy.# Original 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Ravel the array
raveled_arr = arr.ravel()
print(raveled_arr)
In summary, both flatten
and ravel
achieve the same result of converting multi-dimensional arrays into one-dimensional arrays, but ravel
may offer better memory efficiency by returning a view of the original array when possible.
transpose
The transpose
function in NumPy is used to permute the dimensions of an array, effectively swapping the rows and columns. It allows you to rearrange the axes of the array to achieve a desired layout.
Syntax: numpy.transpose(array, axes=None)
Parameters:
array
: The input array to be transposed.axes
(optional): Specifies the new order of axes. If not provided, the axes are reversed by default.# Original array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Transpose the array
transposed_arr = np.transpose(arr)
print(transposed_arr)
In this example, the original 2×3 array is transposed into a 3×2 array by swapping the rows and columns. The resulting array reflects the new arrangement of dimensions specified by the transpose operation.
ravel
for better performance.NumPy reshaping is a game-changer for array manipulation, offering seamless adaptation of data structures. Mastering these techniques will enhance your scientific computing and data analysis workflows, unlocking new possibilities for efficient data processing.
Referenced : num.reshape
We hope that you liked our information, if you liked our information, then you must share it with your friends, family and group. So that they can also get this information.
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Master your Amazon SQL Interview Questions! This article reveals the key SQL concepts and question…
Data Analyst Interview Questions:- Get insights into Data Analyst interview questions at Flipkart. Learn about…
Understanding SQL query execution plans is crucial for performance tuning. Learn how execution plans work,…
Learn how single-column indexes work in SQL, when to use them, and how they improve…
Introduction SQL indexing is a fundamental technique for optimizing database performance. Essentially, an index is…
Introduction Advanced Indexing techniques, including covering, partial, and composite indexes, are crucial for optimizing SQL…