python - python 不支持的浮点和整数操作数数据类型
问题描述
嘿,我正在尝试使用梯度下降进行线性回归,但我一直面临这个错误
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))
错误在这一行
while diff > epsilon & n < epochs: # 2 stopping criteria is set
任何建议或替代方案将不胜感激
解决方案
它返回错误是因为您使用&
来表示and
逻辑运算符。在 Python 中,您应该使用and
如下所示:
while diff > epsilon and n < epochs:
推荐阅读
- javascript - 应该如何在codepen中使用此代码添加以下部分任何人都知道我想在此标题下添加关于部分和工作部分
- powerapps - PowerApps 库 - 混合屏幕
- python - 我有带有情绪分数的词典,我想从标记化的推文中找到这些词并添加分数
- r - 带有此类数据的图表,我如何找到每个“集群”的峰值和谷值?在 R 中
- javascript - HTML Input[Date] 默认为 mm/dd/yyyy,尽管区域设置
- javascript - 如何解决这个模数运算符javascript不准确的问题?
- c++ - 将具有固定签名的自定义函数名称注入 CRTP
- android - 跟踪用户之间的纬度/经度距离
- vim - 如何找出导致 Enter 键延迟的原因?
- r - 为发布准备 R 数据集 - 删除重复项