array manipulation
1D array
transpose
programming
data structures

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.

Practice algorithms

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

python
1import numpy as np
2
3arr = np.array([1, 2, 3, 4, 5])
4
5print(arr.shape)          # (5,)
6print(arr.T.shape)        # (5,) — unchanged!
7print(np.transpose(arr).shape)  # (5,) — still unchanged
8
9# 1D arrays have only one axis, so there's nothing to swap
10print(arr)    # [1 2 3 4 5]
11print(arr.T)  # [1 2 3 4 5]

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

python
1import numpy as np
2
3arr = np.array([1, 2, 3, 4, 5])
4
5# Method 1: reshape
6col = arr.reshape(-1, 1)
7print(col.shape)  # (5, 1)
8print(col)
9# [[1]
10#  [2]
11#  [3]
12#  [4]
13#  [5]]
14
15# Method 2: np.newaxis (adds a new axis)
16col = arr[:, np.newaxis]
17print(col.shape)  # (5, 1)
18
19# Method 3: np.expand_dims
20col = np.expand_dims(arr, axis=1)
21print(col.shape)  # (5, 1)
22
23# Method 4: reshape with explicit dimensions
24col = arr.reshape(5, 1)
25print(col.shape)  # (5, 1)

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

python
1import numpy as np
2
3arr = np.array([1, 2, 3, 4, 5])
4
5# Row vector: shape (1, 5)
6row = arr.reshape(1, -1)
7print(row.shape)  # (1, 5)
8print(row)        # [[1 2 3 4 5]]
9
10# Using np.newaxis
11row = arr[np.newaxis, :]
12print(row.shape)  # (1, 5)
13
14# Now transpose works on the 2D array
15print(row.T.shape)  # (5, 1) — column 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)

python
1import numpy as np
2
3matrix = np.array([[1, 2, 3],
4                   [4, 5, 6]])
5
6print(matrix.shape)    # (2, 3)
7print(matrix.T.shape)  # (3, 2)
8print(matrix.T)
9# [[1 4]
10#  [2 5]
11#  [3 6]]

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

python
1import numpy as np
2
3# sklearn requires 2D input for features
4from sklearn.linear_model import LinearRegression
5
6X_1d = np.array([1, 2, 3, 4, 5])
7y = np.array([2, 4, 6, 8, 10])
8
9# This fails — sklearn expects 2D
10# model.fit(X_1d, y)  # ValueError: Expected 2D array, got 1D
11
12# Fix: reshape to column vector
13X_2d = X_1d.reshape(-1, 1)
14model = LinearRegression()
15model.fit(X_2d, y)  # Works
16
17# Matrix multiplication also requires correct shapes
18a = np.array([1, 2, 3])
19b = np.array([4, 5, 6])
20
21# Dot product (1D @ 1D = scalar)
22print(np.dot(a, b))  # 32
23
24# Outer product (column @ row = matrix)
25print(a.reshape(-1, 1) @ b.reshape(1, -1))
26# [[ 4  5  6]
27#  [ 8 10 12]
28#  [12 15 18]]

Other Languages

python
1# Python lists — no transpose concept
2lst = [1, 2, 3, 4, 5]
3# list(zip(lst)) creates [(1,), (2,), (3,), (4,), (5,)]
4transposed = list(zip(lst))
5print(transposed)  # [(1,), (2,), (3,), (4,), (5,)]
javascript
1// JavaScript — no native transpose
2const arr = [1, 2, 3, 4, 5];
3// Convert to column format (array of single-element arrays)
4const column = arr.map(x => [x]);
5console.log(column);  // [[1], [2], [3], [4], [5]]
java
1// Java — 1D to 2D column array
2int[] arr = {1, 2, 3, 4, 5};
3int[][] column = new int[arr.length][1];
4for (int i = 0; i < arr.length; i++) {
5    column[i][0] = arr[i];
6}

Common Pitfalls

  • Expecting .T to convert 1D to column vector: np.array([1,2,3]).T returns 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 raises ValueError. 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 .T or np.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, .T transposes between row and column vectors correctly
  • sklearn requires 2D input — always reshape single-feature 1D arrays with .reshape(-1, 1)

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.