Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to subtract matrices in Python properly?

I’ve been trying to subtract two matrices (not sure if they are really matrices since one of them is pandas series) but the result is not true. I added the code and outputs, how can I get the correct 200X1 shape result?

*X is 200×4 and w is 4×1

Code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can reshape y_hat :

y - y_hat.reshape(-1,1)

The reason you get (200,200) is because of numpy broadcasting, it treats y_hat as (1,200), so in order for the shapes to match, numpy broadcasts y into (200,200) and y_hat into (200,200) and then does the substraction.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading