float和int的python未移植操作数数据类型
嘿,我试图用梯度下降法做线性回归,我一直面临这个错误float和int的python未移植操作数数据类型,python,linear-regression,Python,Linear Regression,嘿,我试图用梯度下降法做线性回归,我一直面临这个错误 TypeError: unsupported operand type(s) for &: 'float' and 'int' 代码如下 b = 0 a = 0 L = 0.0001 epochs = 10000 n = len(X) n = 1 epsilon = 0.0001 # Stop algorithm when absolute difference between 2 consecutive x-values is
TypeError: unsupported operand type(s) for &: 'float' and 'int'
代码如下
b = 0
a = 0
L = 0.0001
epochs = 10000
n = len(X)
n = 1
epsilon = 0.0001 # Stop algorithm when absolute difference between 2 consecutive x-values is less than epsilon
diff = 1
while diff > epsilon & n < epochs: # 2 stopping criteria is set
Y_pred = b*X + a
cost = (1/n)*sum([val**2 for val in (Y-Y_pred)])
D_b = (-2/n) * sum(X*(Y - Y_pred))
D_a = (-2/n) * sum(Y - Y_pred)
b = b - L*D_b
a = a - L*D_a
diff = abs(Y_pred -Y)
j = j + 1
y = Y_pred
print('x')
print('The value of b is {},cost is {} and the value of a is {} when itstheminimum'.format(b,cost,a))
b=0
a=0
L=0.0001
纪元=10000
n=len(X)
n=1
ε=0.0001#当两个连续x值之间的绝对差小于ε时停止算法
差异=1
而diff>epsilon&n
错误在这行
while diff > epsilon & n < epochs: # 2 stopping criteria is set
而diff>epsilon&n
任何建议或备选方案都将不胜感激它返回错误,因为您使用
&
表示和逻辑运算符。在Python中,应使用和,如下所示:
while diff > epsilon and n < epochs:
而diff>ε和n
&
是按位和。使用&&
进行逻辑AND。或者,如果你在while(diff>epsilon)和(n
@Barmar你的意思是和