siamii
siamii

Reputation: 24084

Convert row vector to column vector in NumPy

import numpy as np

matrix1 = np.array([[1,2,3],[4,5,6]])
vector1 = matrix1[:,0] # This should have shape (2,1) but actually has (2,)
matrix2 = np.array([[2,3],[5,6]])
np.hstack((vector1, matrix2))

ValueError: all the input arrays must have same number of dimensions

The problem is that when I select the first column of matrix1 and put it in vector1, it gets converted to a row vector, so when I try to concatenate with matrix2, I get a dimension error. I could do this.

np.hstack((vector1.reshape(matrix2.shape[0],1), matrix2))

But this looks too ugly for me to do every time I have to concatenate a matrix and a vector. Is there a simpler way to do this?

Upvotes: 21

Views: 48730

Answers (3)

Troll
Troll

Reputation: 1925

Subsetting

The even simpler way is to subset the matrix.

>>> matrix1
[[1 2 3]
 [4 5 6]]

>>> matrix1[:, [0]] # Subsetting
[[1]
 [4]]

>>> matrix1[:, 0] # Indexing
[1 4]

>>> matrix1[:, 0:1] # Slicing
[[1]
 [4]]

I also mentioned this in a similar question.

It works somewhat similarly to a Pandas dataframe. If you index the dataframe, it gives you a Series. If you subset or slice the dataframe, it gives you a dataframe.

Your approach uses indexing, David Z's approach uses slicing, and my approach uses subsetting.

Upvotes: 0

ali_m
ali_m

Reputation: 74154

Here are three other options:

  1. You can tidy up your solution a bit by allowing the row dimension of the vector to be set implicitly:

    np.hstack((vector1.reshape(-1, 1), matrix2))
    
  2. You can index with np.newaxis (or equivalently, None) to insert a new axis of size 1:

    np.hstack((vector1[:, np.newaxis], matrix2))
    np.hstack((vector1[:, None], matrix2))
    
  3. You can use np.matrix, for which indexing a column with an integer always returns a column vector:

    matrix1 = np.matrix([[1, 2, 3],[4, 5, 6]])
    vector1 = matrix1[:, 0]
    matrix2 = np.matrix([[2, 3], [5, 6]])
    np.hstack((vector1, matrix2))
    

Upvotes: 16

David Z
David Z

Reputation: 131550

The easier way is

vector1 = matrix1[:,0:1]

For the reason, let me refer you to another answer of mine:

When you write something like a[4], that's accessing the fifth element of the array, not giving you a view of some section of the original array. So for instance, if a is an array of numbers, then a[4] will be just a number. If a is a two-dimensional array, i.e. effectively an array of arrays, then a[4] would be a one-dimensional array. Basically, the operation of accessing an array element returns something with a dimensionality of one less than the original array.

Upvotes: 28

Related Questions