noclew - 8 months ago 49

Python Question

I am so confused about Numpy array. Let's say I have two Numpy arrays.

`a = np.array([[1,2], [3,4], [5,6]])`

b = np.array([[1,10], [1, 10]])

My interpretations of

`a`

`b`

`a = 1 2 b = 1 10`

3 4 1 10

5 6

Then, I thought it should be fine to do

`a * b`

`a.dot(b)`

Given this fact, I think my intepretation of Numpy array is not right. Can anyone let me know how I should think of Numpy array? I know that I can do

`a*b`

`a`

`b`

Answer

For numpy arrays, the `*`

operator is used for element by element multiplication of arrays. This is only well defined if both arrays have the same dimensions. To illuminate `*`

-multiplication, note that element by element multiplication with the identity matrix will not return the same matrix

```
>>> I = np.array([[1,0],[0,1]])
>>> B = np.array([[1,2],[3,4]])
>>> I*B
array([[ 1, 0],
[ 0, 10]])
```

Using the numpy function `dot(a,b)`

produces the typical matrix multiplication.

```
>>> dot(I,B)
array([[ 1, 2],
[ 3, 4]])
```

Source (Stackoverflow)