Created
August 5, 2020 19:29
-
-
Save derrickturk/d79e9bb45aa72d7bbdb1812315e68d52 to your computer and use it in GitHub Desktop.
Coursera ML course, exercise 1, ported to Julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LinearAlgebra | |
using DelimitedFiles | |
using Plots | |
gr() | |
warmup() = I | |
J(X, y, θ) = sum((X * θ - y) .^ 2) / (2 * length(y)) | |
function ∇J(X, y, θ) | |
err = X * θ - y | |
(err' * X)' ./ size(X, 1) | |
end | |
function gradientdescent(X, y, θ, α, n) | |
J_history = zeros(n, 1) | |
for i in 1:n | |
∇ = ∇J(X, y, θ) | |
θ -= α * ∇ | |
J_history[i] = J(X, y, θ) | |
end | |
θ, J_history | |
end | |
data = readdlm("ex1data1.txt", ',', Float64, '\n') | |
X = data[:, 1] | |
y = data[:, 2] | |
m = length(y) | |
p = scatter(X, y) | |
display(p) | |
X = [ones(m) X] | |
θ = zeros(2, 1) | |
println("J = $(J(X, y, θ))") | |
iterations = 1500 | |
α = 0.01 | |
θ, _ = gradientdescent(X, y, θ, α, iterations) | |
println("θ = $θ") | |
scatter!(p, X[:,2], X * θ) | |
θ0 = range(-10, 10, length=100) | |
θ1 = range(-1, 4, length=100) | |
c = contour(θ0, θ1, (θ0, θ1) -> J(X, y, [θ0, θ1]), | |
levels=exp10.(range(-2, 3, length=20))) | |
scatter!([θ[1]], [θ[2]]) | |
display(c) | |
plotly() | |
surface(θ0, θ1, (θ0, θ1) -> J(X, y, [θ0, θ1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment