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

Question:

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

Asked By: pumpkin

||

Answers:

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...
Answered By: Csaba