How do you transpose a matrix in NumPy using .T or np.transpose()?

Quick Overview

Asks how to transpose arrays in NumPy with the .T attribute and np.transpose, including explicit axis permutations for higher-dimensional arrays. Tests whether you know what the operation does to memory, views and strides, and how it treats 1-D arrays.

How do you transpose a matrix in NumPy using .T or np.transpose()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do you transpose a matrix in NumPy using `.T` or `np.transpose()`? ```hint Look at memory After transposing, write a value into the result and print the original array. Then compare `a.strides` with `a.T.strides`. ``` ### What a Strong Answer Covers - What transposing does to the shape and axes, for 2-D and for higher-dimensional arrays - How `.T` and `np.transpose(arr, axes)` differ - Whether the result copies data, and what that means for writes to it - The memory layout of the result and when a contiguous copy is needed - Edge cases such as 1-D arrays ### Follow-up Questions - `.T` leaves a 1-D array unchanged. How do you turn one into a column vector? - For a batch of matrices with shape `(batch, m, n)`, how do you transpose each matrix without moving the batch axis? - Why does `a.reshape(3, 2)` give a different result from `a.T` for an array of shape `(2, 3)`? - When can the memory layout of a transposed array slow down later operations?

Overview: Asks how to transpose arrays in NumPy with the .T attribute and np.transpose, including explicit axis permutations for higher-dimensional arrays. Tests whether you know what the operation does to memory, views and strides, and how it treats 1-D arrays.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you transpose a matrix in NumPy using .T or np.transpose()?

What a Strong Answer Covers Guidance

  • What transposing does to the shape and axes, for 2-D and for higher-dimensional arrays
  • How .T and np.transpose(arr, axes) differ
  • Whether the result copies data, and what that means for writes to it
  • The memory layout of the result and when a contiguous copy is needed
  • Edge cases such as 1-D arrays

Follow-up Questions Guidance

  • .T leaves a 1-D array unchanged. How do you turn one into a column vector?
  • For a batch of matrices with shape (batch, m, n) , how do you transpose each matrix without moving the batch axis?
  • Why does a.reshape(3, 2) give a different result from a.T for an array of shape (2, 3) ?
  • When can the memory layout of a transposed array slow down later operations?