Home > Software engineering >  Can't perform matrix multiplication on float matrix in numpy
Can't perform matrix multiplication on float matrix in numpy

Time:09-25

Today as I'm trying to implement linear regression by hand, I encountered an issue in numpy that stops me from performing the intended matrix multiplication. Because the two matrix contains float numbers (not sure if that is the issue), the notebook kept on returning

TypeError: can't multiply sequence by non-int of type 'float'

Here is the notebook at the data I used, please help!

CodePudding user response:

You probably meant to use np.matmul or @ (shorthand) rather than the * shorthand, which makes soft-copies duplicates in a Python list (and so can't create a fractional number, only integral ones)

See the notes in the numpy.dot docs https://numpy.org/doc/stable/reference/generated/numpy.dot.html

CodePudding user response:

Code and traceback should be part of your question. But this error is produced by an expression like

In [99]: [1,2,3]*1.2
Traceback (most recent call last):
  File "<ipython-input-99-fdfde54247f2>", line 1, in <module>
    [1,2,3]*1.2
TypeError: can't multiply sequence by non-int of type 'float'

[1,2,3] a list is a sequence. multiply is only defined for an integer, and means replication.

At this point in the code you aren't using numpy.

  • Related