I'm trying to solve this exercise about gradient descent. When I run my code I get an error on yi_pred = xi * m + c
. Can someone please help me understand where I'm doing wrong? Thanks
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```
Data:
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
The error is: `TypeError: can only concatenate list (not "int") to list`.
You are using a nested list.
Remove the extra square brackets '[]' from x
and y
:
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.