Skip to content Skip to sidebar Skip to footer

Create A 20x20 Matrix Using Numpy Broadcast

I'm looking how to create a matrix of 20x20 using Numpy broadcasting, result should look like: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,

Solution 1:

Let's take your calculations step by step:

First make an array with arange:

In [166]: x = np.arange(5)                                                                           
In [167]: x                                                                                          
Out[167]: array([0, 1, 2, 3, 4])

We can multiply it by a scalar:

In [168]: x * (5)                                                                                    
Out[168]: array([ 0,  5, 10, 15, 20])

The () add nothing here.

Then you try to multiply by a tuple:

In [169]: x * (0, 5)                                                                                 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-169-b6f23a3901bc> in <module>
----> 1 x * (0, 5)

ValueError: operands could not be broadcast together with shapes (5,) (2,) 

I suspect you expected to somehow multiply the (0,5) 'range' by (0,5) 'range'. That's not how it works. (0,5) is a tuple. In the multiplication expression, the array x controls the action, so the tuple is turned into an array, np.array([0,2]). By broadcasting rules, a 5 element array can't multiply with a 2 element one.

Your first print suggests you want to multiply two arrays (n,) shape array to produces a (n,n) array, or (n*n,).

We could reshape x to (5,1):

In [170]: x[:,None]                                                                                  
Out[170]:           # elsewhere this might called a column vector
array([[0],
       [1],
       [2],
       [3],
       [4]])

Now by broadcasting:

In [171]: x*x[:,None]                                                                                
Out[171]: 
array([[ 0,  0,  0,  0,  0],
       [ 0,  1,  2,  3,  4],
       [ 0,  2,  4,  6,  8],
       [ 0,  3,  6,  9, 12],
       [ 0,  4,  8, 12, 16]])

and if you want it as 1d:

In [172]: np.ravel(x*x[:,None])                                                                      
Out[172]: 
array([ 0,  0,  0,  0,  0,  0,  1,  2,  3,  4,  0,  2,  4,  6,  8,  0,  3,
        6,  9, 12,  0,  4,  8, 12, 16])

The [171] action is: (5,) * (5,1) => (1,5) * (5,1) => (5,5)

The basic broadcasting rules are:

  • leading dimensions might be added to match
  • size 1 dimensions might be adjusted to match

This multiplication is like the outer or cartesian product of two arrays:

In [173]: np.outer(x,x)                                                                              
Out[173]: 
array([[ 0,  0,  0,  0,  0],
       [ 0,  1,  2,  3,  4],
       [ 0,  2,  4,  6,  8],
       [ 0,  3,  6,  9, 12],
       [ 0,  4,  8, 12, 16]])

Solution 2:

I believe you are looking for something like this:

a = np.arange(21)
b = np.arange(21)
a[:, np.newaxis] * b

More can be found here: https://numpy.org/doc/stable/user/basics.broadcasting.html


Post a Comment for "Create A 20x20 Matrix Using Numpy Broadcast"