Skip to content

Instantly share code, notes, and snippets.

@ravi07bec
Created November 26, 2021 00:18
Show Gist options
  • Save ravi07bec/cb381508dbdb489626e38c4e59357cbe to your computer and use it in GitHub Desktop.
Save ravi07bec/cb381508dbdb489626e38c4e59357cbe to your computer and use it in GitHub Desktop.
# Make a prediction with coefficients
def predict(row, coefficients):
yhat = coefficients[0]
for i in range(len(row)-1):
yhat += coefficients[i + 1] * row[i]
return yhat
# Estimate linear regression coefficients using stochastic gradient descent
def coefficients_sgd(train, l_rate, n_epoch):
coef = [0.0 for i in range(len(train[0]))]
for epoch in range(n_epoch):
sum_error = 0
for row in train:
yhat = predict(row, coef)
error = yhat - row[-1]
sum_error += error**2
coef[0] = coef[0] - l_rate * error
for i in range(len(row)-1):
coef[i + 1] = coef[i + 1] - l_rate * error * row[i]
print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))
return coef
# Calculate coefficients
dataset = [[1, 1], [2, 3], [4, 3], [3, 2], [5, 5]]
l_rate = 0.001
n_epoch = 5
coef = coefficients_sgd(dataset, l_rate, n_epoch)
print(coef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment