Skip to content

Instantly share code, notes, and snippets.

@SimonAB
Created February 20, 2020 22:10
Show Gist options
  • Save SimonAB/60645eea9cb32ee714635e36a584e262 to your computer and use it in GitHub Desktop.
Save SimonAB/60645eea9cb32ee714635e36a584e262 to your computer and use it in GitHub Desktop.
"""
This function uses gradient descent to search for the weights
that minimises the logit cost function.
A tuple with learned weights vector (θ) and the cost vector (𝐉)
are returned.
"""
function logistic_regression_sgd(X, y, λ, fit_intercept=true, η=0.01, max_iter=1000)
# Initialize some useful values
m = length(y); # number of training examples
if fit_intercept
# Add a constant of 1s if fit_intercept is specified
constant = ones(m, 1)
X = hcat(constant, X)
else
X # Assume user added constants
end
# Use the number of features to initialise the theta θ vector
n = size(X)[2]
θ = zeros(n)
# Initialise the cost vector based on the number of iterations
𝐉 = zeros(max_iter)
for iter in range(1, stop=max_iter)
# Calcaluate the cost and gradient (∇𝐉) for each iter
𝐉[iter], ∇𝐉 = regularised_cost(X, y, θ, λ)
# Update θ using gradients (∇𝐉) for direction and (η) for the magnitude of steps in that direction
θ = θ - (η * ∇𝐉)
end
return (θ, 𝐉)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment