4

I'm trying to understand how a np.array can be created with shape(0,2).

Is this even possible?

So

np.array([]).shape
#output (0,)

np.array([[],[]]).shape
#output (2, 0)

can I get (0, 2) ?

(without using .T)

4
  • 2
    np.array([[],[]]).T.shape? Commented Feb 5, 2019 at 17:40
  • @Divakar without Transposing Commented Feb 5, 2019 at 17:41
  • np.array([[],[]]).swapaxes(0,1)? Commented Feb 5, 2019 at 17:41
  • 2
    The problem with nested lists is that the outer list will necessarily contain lists, and won't have a 0 size. Shape (0,2) means 0 'rows', 2 'columns'. Commented Feb 5, 2019 at 17:48

2 Answers 2

11

Use a function such as np.zeros. No need to worry about the value it generates since there are no values in a 0 x 2 array:

arr = np.zeros((0, 2))
arr.shape

(0, 2)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the display of arr array([], shape=(0, 2), dtype=float64) does not use any nested brackets. It has an explicit shape instead.
2

It can be done by np.empty((0,2)) or np.zeros((0,2)).

np.empty(), unlike np.zeros(), does not set the array values to zero and it is slightly faster.

Extra information: In your code np.array([]).shape outputs a rank-1 array which is equal to its transpose.

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.