0

I feel the behaviour of ndarray object is incorrect. I created one using the line of code below

c = np.ones((5,0), dtype=np.int32)

Some of the commands and outputs are given below

print(c)
[]
c
array([], shape=(5, 0), dtype=int32)
c[0]
array([], dtype=int32)
print(c[0])
[]

It's like empty array contains empty array. I can assign values but this value is lost, it doesn't show.

print(c)
[]
c.shape
(5, 0)
c[0]=10
print(c)
[]
print(c[0])
[]

What does (5,0) array mean? What is the difference between a and c?

a = np.ones((5,), dtype=np.int32)
c = np.ones((5,0), dtype=np.int32)

I am sorry I am new to Python so my knowledge is very basic.

Ashish Shukla
  • 111
  • 1
  • 2

2 Answers2

0

Welcome to python. There seems to be some misconception about shape of an array, in particular the shape of a 1D array ( shapes of the form (n,). You see the shape (n,) corresponds to a 1 dimensional numpy array. If you are familiar with linear algebra, then this 1D array is analogous to a row vector. IT IS NOT the same thing as (n,0). What (n, m) represents is the shape of a 2D numpy array ( which is analagous to a matrix in linear algebra). Therefore saying an array has a shape (n,0) relates to an array with n rows but each row would have 0 columns therefore you are returned an empty array. If you do infact want a vector of ones you can type np.ones((5,)). Hope it helps. Comment if you require any further help.

Archit Kithania
  • 177
  • 4
  • 13
0
In [43]: c = np.ones((5,0), dtype=np.int32)                                                                  
In [44]: c                                                                                                   
Out[44]: array([], shape=(5, 0), dtype=int32)
In [45]: c.size                                                                                              
Out[45]: 0
In [46]: np.ones(5).size                                                                                     
Out[46]: 5

The size, or number of elements of an array is the product of its shape. For c that 5*0 = 0. c is a 2d array that contains nothing.

If I try to assign a value to a column of c I get an error:

In [49]: c[:,0]=10                                                                                           
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-49-7baeeb61be4e> in <module>
----> 1 c[:,0]=10

IndexError: index 0 is out of bounds for axis 1 with size 0

Your assignment:

In [51]: c[0] = 10                                                                                           

is actually:

In [52]: c[0,:] = np.array(10) 

That works because the c[0,:].shape is (0,), and an array with shape () or (1,) can be 'broadcast' to that target. That's a tricky case of broadcasting.

A more instructive case of assignment to c is where we try to assign 2 values:

In [57]: c[[0,1],:] = np.array([1,2])                                                                        
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-9ad1339293df> in <module>
----> 1 c[[0,1],:] = np.array([1,2])

ValueError: shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (2,0)

The source array is 1d with shape (2,). The target is 2d with shape (2,0).

In general arrays with a 0 in the shape are confusing, and shouldn't be created. They some sometimes arise when indexing other arrays. But don't make one np.zeros((n,0)) except as an experiment.

hpaulj
  • 221,503
  • 14
  • 230
  • 353