Skip to content

Instantly share code, notes, and snippets.

@hpoit
Last active May 26, 2018 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpoit/d1a24a65e4bf4a33ca6e5b69d38e60ca to your computer and use it in GitHub Desktop.
Save hpoit/d1a24a65e4bf4a33ca6e5b69d38e60ca to your computer and use it in GitHub Desktop.
Coursera ML week 3 - assignment 2 v0.3 - implementing Logit model on two datasets
# Logit mode, v0.3
using CSV, Plots; pyplot();
data = CSV.read("/Users/kevinliu/Documents/machine-learning-ex2/ex2/ex2data1.txt", datarow=1)
X = hcat(ones(100,1), Matrix(data[:, [1,2]]))
y = Vector(data[:, 3])
# Sigmoid function
function sigmoid(z)
1.0 ./ (1.0 .+ exp.(-z))
end
sigmoid(0) # => 0.5
z = rand(3,1); sigmoid(z) # vector
z = rand(3,3); sigmoid(z) # matrix
# Hypothesis: linearly combines X[i] and θ[i], to calculate all instances of cost()
function h(θ, X)
z = 0
for i in 1:length(θ)
z += θ[i] .* X[i, :]
end
sigmoid(z)
end
h([-24, 0.2, 0.2], X)
# Vectorized cost function
function cost(θ, X, y)
hx = sigmoid(X * θ)
m = length(y)
J = (-y' * log.(hx) - (1 - y') * log.(1 - hx)) / m
grad = X' * (hx - y) / m
println("Cost is $J")
println("Gradient is $grad")
end
cost([-24, 0.2, 0.2], X, y)
# will stop maintaining here, too many manual updates
# for more, please visit https://github.com/hpoit/ML-Coursera
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment