0

I have simple array like this

x = np.array([1,2,3,4])

In [3]: x.shape
Out[3]: (4,)

But I don't want shape to return (4,), but (4,1). How can I achieve this?

1
  • can you give a use case for why you need (4, 1). Giving valid context may help others who see this question. Commented Feb 2, 2018 at 15:58

5 Answers 5

3

Generally in Numpy you would declare a matrix or vector using two square brackets. It's common misconception to use single square brackets for single dimensional matrix or vector.

Here is an example:

a = np.array([[1,2,3,4], [5,6,7,8]])
a.shape # (2,4) -> Multi-Dimensional Matrix

In similar way if I want single dimensional matrix then just remove the data not the outer square bracket.

a = np.array([[1,2,3,4]])
a.shape # (1,4) -> Row Matrix

b = np.array([[1], [2], [3], [4]])
b.shape # (4, 1) -> Column Matrix

When you use single square brackets, it's likely to give some odd dimensions.

Always enclose your data within another square bracket for such single dimensional matrix (like you are entering the data for multi-dimensional matrix) without data for those extra dimensions.

Also: You could also always reshape

x = np.array([1,2,3,4])
x = x.reshape(4,1)
x.shape # (4,1)

One Line:

x = np.array([1,2,3,4]).reshape(4,1)
x.shape # (4,1)
Sign up to request clarification or add additional context in comments.

2 Comments

1d arrays, created with single square brackets are perfectly normal in numpy. While there are good uses for (1,4) and (4,1) arrays, I prefer to see (4,) as the default shape. There's nothing odd about that. :)
@hpaulj I didn't mean it useless. It's a simple rank 1 array. The thing using numpy is we perform calculations as in a Matrix and the dimensions of (4,) sometimes bring inconsistency. Like a, and a.T seems to have no any differences. If you play with lots of data, then you will need some specified dimensions rather than just (4,) to which some mathematical operations may provide some inconsistency and will spend your all day finding the bug. Thanks
2

If you want a column vector use

x2 = x[:, np.newaxis]
x2.shape  # (4, 1)

Comments

2

Alternatively, you could reshape the array yourself:

arr1 = np.array([1,2,3,4])
print arr1.shape
# (4,)

arr2 = arr1.reshape((4,1))
print arr2.shape
# (4, 1)

You could of course reshape the array when you create it:

arr1 = np.array([1,2,3,4]).reshape((4,1))

If you want to change the array in place as suggested by @FHTMitchell in the comments:

arr1.resize((4, 1))

1 Comment

Or even in place arr1 = np.array([1,2,3,4]); arr1.resize((4, 1))
1

Below achieves what you want. However, I strongly suggest you look at why exactly you need shape to return (4, 1). Most matrix-type operations are possible without this explicit casting.

x = np.array([1,2,3,4])
y = np.matrix(x)
z = y.T

x.shape  # (4,)
y.shape  # (1, 4)
z.shape  # (4, 1)

2 Comments

This changes the type though, which has knock-on effects. x*x.T == array([ 1, 4, 9, 16]) but y*y.T = matrix([[30]]). Since python added the @ operator (py 3.5) it is recommended to never use np.matrix.
@FHTMitchell, yes it does change the type. If user is on python version <3.5, syntax to do matrix calculations with np.array is cumbersome, in which case someone might want to convert to np.matrix. My logic is I can't see any other reason explicitly casting 1d arrays.
0

You can use zip to transpose at python (non-numpy) level:

>>> a = [1, 2, 3, 4]
>>> 
>>> *zip(a),
((1,), (2,), (3,), (4,))
>>> 
>>> import numpy as np
>>> np.array([*zip(a)])
array([[1],
       [2],
       [3],
       [4]])

Please note that while this is convenient in terms of key strokes it is a bit wasteful given that a tuple object has to be constructed for every list element whereas reshaping an array comes essentially for free. So do not use this on long lists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.