Last active
May 26, 2018 01:11
-
-
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
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
# 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