试图连接列表时出现TypeError



我想解决这个关于梯度下降的练习。当我运行我的代码,我得到一个错误:yi_pred = xi * m + c.

def gradient_descent(x, y, m, c, epochs, L=0.001):
x_new =[]
for elem in x:
x_new.extend(elem)
y =[]
for i in range(epochs):
DM = []
DC = []
for num, xi in enumerate(x):
yi_pred = xi * m + c
yi = y[num]
di_m = xi * (yi_pred - yi)
di_c = yi_pred - yi
DM.append(di_m)
DC.append(di_c)
dm = sum(DM)/len(DM)
dc = sum(DC)/len(DC)
m = m - L * dm
c = c - L * dc
return m,c

数据:

x = [[0.18], [1.0], [0.92], [0.07], [0.85], [0.99], [0.87]]
y = [[109.85], [155.72], [137.66], [76.17], [139.75], [162.6], [151.77]]
m = 0
c = 0
epochs = 200

错误是:TypeError: can only concatenate list (not "int") to list.

您正在使用嵌套列表。

删除xy中额外的方括号"[]":

x = [0.18, 1.0, 0.92, 0.07, 0.85, 0.99, 0.87]
y = [109.85, 155.72, 137.66, 76.17, 139.75, 162.6, 151.77]

相关内容

  • 没有找到相关文章

最新更新