Python 不能将序列乘以类型为 'numpy.float64' 的非 int


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:无法将序列与类型为"numpy.foat64"的非int相乘

我期待的预测值

我相信这是因为X是一个列表,而不是numpy数组,所以你不能对它进行数组运算(例如将它乘以a(

简单的解决方案可能是:

import numpy as np
X = np.array([5, 15, 25, 35, 45, 55])
# rest of your code...

最新更新