Transpose 1 Dimensional Array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Transposing a 1D array has no effect in most languages because transpose swaps rows and columns, and a 1D array has only one dimension. In NumPy, np.transpose(arr) on a 1D array returns the same array unchanged. To convert a 1D array into a column vector (2D array with one column), use reshape(-1, 1) or arr[:, np.newaxis]. This distinction between 1D arrays and 2D column/row vectors is crucial for linear algebra operations, matrix multiplication, and sklearn input requirements.
NumPy: Transpose Has No Effect on 1D
NumPy's .T and np.transpose() swap axes. A 1D array has one axis (axis 0), so swapping produces the same shape.
Converting 1D to Column Vector
All four methods convert the 1D shape (5,) to a 2D column vector (5, 1). reshape(-1, 1) is the most common because -1 infers the row count automatically.
Converting 1D to Row Vector
Once you have a 2D row vector (1, 5), .T correctly transposes it to a column vector (5, 1).
Transpose on 2D Arrays (for Comparison)
Transpose swaps rows and columns on 2D arrays as expected. The distinction is that 1D (n,) and 2D (n, 1) or (1, n) are different shapes in NumPy.
Why It Matters: sklearn and Matrix Multiplication
Other Languages
Common Pitfalls
- Expecting .T to convert 1D to column vector:
np.array([1,2,3]).Treturns the same 1D array. You must use.reshape(-1, 1)or[:, np.newaxis]to get a true column vector. - Confusing (5,) with (5, 1) and (1, 5): Shape
(5,)is 1D,(5, 1)is a 2D column vector,(1, 5)is a 2D row vector. They behave differently in matrix multiplication and broadcasting. - Passing 1D arrays to sklearn: Most sklearn estimators require 2D input
(n_samples, n_features). A 1D array raisesValueError. Always reshape single-feature input with.reshape(-1, 1). - Using reshape without -1:
arr.reshape(5, 1)hardcodes the length.arr.reshape(-1, 1)infers it, making the code work for any array length. - Assuming all languages handle transpose the same: NumPy's transpose is a view (no copy), while converting a list to column format in Python or JavaScript creates new data structures.
Summary
- Transposing a 1D NumPy array with
.Tornp.transpose()returns the same 1D array unchanged - Use
arr.reshape(-1, 1)to create a column vector(n, 1)from a 1D array(n,) - Use
arr.reshape(1, -1)to create a row vector(1, n)from a 1D array - Once reshaped to 2D,
.Ttransposes between row and column vectors correctly - sklearn requires 2D input — always reshape single-feature 1D arrays with
.reshape(-1, 1)
Related reading
- Transpose a 1 dimensional array, that does not represent a square, in place
- Transpose list of lists
- Transposing a 1D NumPy array
- Transposition table in Monte Carlo Tree Search algorithm unintended effect on UCT score
- Travelling salesman with repeat nodes dynamic weights
- Traversal of cyclic directed graph
- Traverse a list in reverse order in Python
- Traverse a list in reverse order in Python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.