6

I am not sure about the difference between (N,) and (N,1) in numpy. Assuming both are some features, they have same N dimension, and both have one sample. What's the difference?

a = np.ones((10,))
print(a.shape) #(10,)
b = np.ones((10,1))
print(b.shape) #(10,1)
jef
  • 3,222
  • 5
  • 26
  • 62
  • 1
    A couple of duplicates: http://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r, http://stackoverflow.com/questions/38402227/numpy-why-is-there-a-difference-between-x-1-and-x-dimensionality – hpaulj Mar 19 '17 at 04:55

2 Answers2

8

In Python, (10,) is a one-tuple (the , being necessary to distinguish it from the use of parentheses for grouping: (10) just means 10), whereas (10,1) is a pair (a 2-tuple). So np.ones((10,)) creates a one-dimensional array of size 10, whereas np.ones((10,1)) creates a two-dimensional array of dimension 10×1. This is directly analogous to, say, the difference between a single number and a one-dimensional array of length 1.

ruakh
  • 156,364
  • 23
  • 244
  • 282
6

The difference is that, a is a one dimensional array. Like this:

[1,1,1] 

And b is a multidimensinal array. Like this:

[[1],
 [1],
 [1]]
el3ien
  • 4,809
  • 1
  • 14
  • 31