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
yp=a*X+b #y predict

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

i was expecting a predict value

>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...

Leave a Reply