Search⌘ K

Transposing of NumPy Array

Explore how to transpose 2D NumPy arrays by converting rows into columns and vice versa using the transpose() function or T attribute. Understand reshaping arrays with arange and reshape, and learn essential NumPy functions for element-wise exponentiation, squaring, square roots, and array addition and subtraction. This lesson equips you with foundational skills for processing arrays efficiently in predictive data analysis.

We'll cover the following...

Transpose

As the name suggests, we will take the transpose of a given 2-D NumPy array. Just like matrix transpose in linear algebra, we convert the rows of a 2-D NumPy array into columns and convert its columns into rows. The function transpose() or simply T can be used to take the transpose of a 2-D array.

Python 3.5
import numpy as np
# Creating 2-D array
arr = np.arange(0,50,1).reshape(10,5) # Declare a 2-D array
print("The original array")
print(arr)
print("\nThe transposed array")
print(arr.transpose()) # Print the transposed array
#print(arr.T) # This can also be used and same result will be produced

On line 3, a 2-D array is declared using the arange and reshape ...