How to Transpose a 1D NumPy Array?

In this tutorial, we will learn how to transpose a 1D NumPy array? By Pranit Sharma Last updated : May 25, 2023

Given a 1D NumPy array, we have to transpose it.

Transpose a 1D NumPy Array

  1. First, convert the 1D vector into a 2D vector so that you can transpose it. It can be done by slicing it with np.newaxis during the creation of the array.
  2. And, then by using the .T method, you can transpose it.

Python program to transpose a 1D NumPy array

# Import numpy
import numpy as np

# Creating numpy array
arr = np.array([10, 20, 30, 40, 50])[np.newaxis]

# Printing the original array
print("Original array (arr):\n",arr,"\n")

# Transposing the NumPy array
transpose_arr = arr.T

# Display result
print("Transpose Array (transpose_arr):\n",transpose_arr)

Output

The output of the above program is:

Original array (arr):
 [[10 20 30 40 50]] 

Transpose Array (transpose_arr):
 [[10]
 [20]
 [30]
 [40]
 [50]]

Python NumPy Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.