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

python can't multiply sequence by non-int of type 'numpy.float64'

X = [5, 15, 25, 35, 45, 55]
Y = [5, 20, 14, 32, 22, 38]
plt.scatter(X, Y)

xm=np.mean(X) #valeur moyenne de x
ym=np.mean(Y) #valeur moyenne de y
num=0 #numerator
den=0 #denominator
for i in range(len(X)):
    num +=(X[i]-xm)*(Y[i]-ym)
    den +=(X[i]-xm)**2
a=num/den
b=ym-a*xm
print(a,b)
yp=a*X+b #y predict
plt.scatter(X,Y)
plt.plot(X,yp,'r')

TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’

i was expecting a predict value

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 :

I believe this is because X is a list and not a numpy array, so you can’t do array operations on it (such as multiplying it by a)

Simple solution could be this:

import numpy as np

X = np.array([5, 15, 25, 35, 45, 55])

# rest of your code...
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