0

I'm taking a course about Applied Statistics. I use Python, or more specifically, Jupyter Notebook. One of my labs consists of performing basic operations on data fitting. I mostly used pandas, numpy and matplotlib (sorry, can't remember the exact name but something like that). I find that my teacher used the "@" sign to do multiplication for two matrixes (given that they are multiply-able). When I tried to use the basic * to multiply, the terminal told that:

operands could not be broadcast together with shapes

Here's an example, in this one, a and b are 2d-matrices. a.T is a transposed matrix. I perform an operation:

thetaMatrix = np.linalg.inv(a.T@a) @ (a.T@b)

So what is the problem I'm having at the moment? I don't see any difference between the "@" and the "*". Thanks in advance.

ikmun
  • 3
  • 2
  • `*` is for scalar multiplication (and also for [broadcasting](https://numpy.org/doc/stable/reference/generated/numpy.multiply.html) in NumPy). `@` is for [matrix multiplication](https://stackoverflow.com/questions/6392739/what-does-the-at-symbol-do-in-python). – J. Choi Jul 15 '22 at 09:20

1 Answers1

0

It's simply because if you want to multiply two matrices you cannot use the same sign that you use for scalar values, so as you said you have to use @. Another option is using the function numpy.dot().

Jock
  • 388
  • 2
  • 12
  • Thanks for your clarification! If you don't mind, do you have any documents relating to the topic I'm discussing? It would be great if you had any source. If not, it's totally fine and I'm more than satisfied with the answer you provide :) – ikmun Jul 15 '22 at 09:20
  • I just found this [article of GeekForGeeks](https://www.geeksforgeeks.org/multiplication-two-matrices-single-line-using-numpy-python/) that explains it clearly :) – Jock Jul 15 '22 at 09:27