Uncategorized

Unbelievable behavior of matrix multiplication in python numpy – (M*v)[n] differs from M*v[:,n]


Would be happy if someone can comment on the following issue and what are similar traps and how to avoid them ?

In math (a+b)+c = a+(b+c), a(b+c)=ab+bc, but that might be wrong with float arithmetic.

But at least one can expect that matrix multiplication has some fixed order of execution for all matrices and thus that problem should not arise, but it seems it is not the case: same vector placed in different matrices – we get different results (difference is small – about machine precision, but still)

Example 1: (M * N)[299] differs from M*N[:,299]
enter image description here

Moreover when we place the same vector into different matrix “N2” we get M * N2[:,* ] is different from the M * N[:,299]:
enter image description here

Examples can be found in the notebook – direct link to code fragment:
https://www.kaggle.com/code/alexandervc/santa23-states-distance-generate-save?scriptVersionId=159163678&cellId=24



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *