1

I understand the basics of numpy (Pandas) broadcasting but got stuck on this simple example:

x = np.arange(5) 
y = np.random.uniform(size = (2,5)) 
z = x*y 
print(z.shape) #(2,5) 

My understanding of the z shape is that you had a (1,5) array multiplied with a (2,5) array, the trailing dimension for 5 is equal so you end up with a 2x5 array. Okay that sounds good. My problem is why is x.shape = (5,)? Isn't it one-dimensional so it's really 1x5?

hpaulj
  • 221,503
  • 14
  • 230
  • 353
NimbleTortoise
  • 355
  • 5
  • 13
  • In `numpy` a one-dimensional array really has just one dimension, and the shape is a single element tuple, e.g. `(5,)`. When broadcasting `numpy` adds leading dimensions as needed, so this will be expanded as needed to (1,5), (1,1,5), etc. – hpaulj Nov 26 '18 at 06:46
  • Don't forget that in python, a single element list is written as `[n]` and a single element tuple as `(n,)`. https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences. The shape of an array is a tuple. – hpaulj Nov 26 '18 at 08:00

2 Answers2

1

NumPy 1D array like x gives you shape such as (5,) without reshaping. If you want to treat it as 1 column matrix of shape 1x5 then do np.arange(5).reshape(1,5)

sync11
  • 1,224
  • 2
  • 10
  • 23
  • How come 1-D arrays are formatted as (# , )? I think this is what is throwing me off. I'm used to viewing it comparing AxBx format so would it look like this? : (3,) = 3 (1,3) = 1 x 3 (2,3,3) = 2 x 3 x 3 – NimbleTortoise Nov 26 '18 at 06:42
  • Usually we talk about a 2d array shape as (rows, columns). So `(1,5)` would be a single row, 5 columns, (5,1) is 5 rows, 1 column, or a single column array. For broadcasting purposes a (5,) is treated the same as a (1,5). – hpaulj Nov 26 '18 at 06:45
1

The broadcasting rules are:

Add leading singleton dimensions as needed to match number of dimensions
Scale any singleton dimensions to match dimension values
Raise error dimensions can't be matched

With your x and y:

(5,) * (2,5)
(1,5) * (2,5)          # add the leading 1
(2,5) * (2,5)          # scale the 1
=> (2,5)

If y was (5,2), it would raise an error, because (1,5) cannot be paired with (5,2). But (5,1) is ok, because (1,5) * (5,1) => (5,5).

hpaulj
  • 221,503
  • 14
  • 230
  • 353