Skip to content

Commit

Permalink
Lecture 8: Scripts updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sadda committed Apr 13, 2021
1 parent 8e74fd7 commit ded94e6
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions scripts/lecture_08/script_sol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ end
# Linear regression
###########################################

# EXTRA

σ(z) = 1/(1+exp(-z))
plot(-10:0.01:10, σ)

# Load data

using Plots
Expand Down Expand Up @@ -49,16 +54,31 @@ X = hcat(iris.PetalLength, ones(length(y)))

# Exercise

w = (X'*X) \ (X'*y)
w = (X'*X)^(-1)*X'*y
w = (X'*X)^(-1)*(X'*y)
w = X'*X \ X'*y

g(w) = X'*(X*w-y)
w2 = optim([], g, zeros(size(X,2)), GD(1e-4); max_iter=10000)

norm(w-w2)

# EXTRA

w = (X'*X)^(-1)*X'*y
w = (X'*X)^(-1)*(X'*y)
w = X'*X \ X'*y

A = randn(1000,1000)
b = randn(1000)

@time (A*A)*b;
@time A*(A*b);

# Exercise

f_pred(x, w) = w[1]*x + w[2]
f_pred(x::Real, w) = ([x 1]*w)[1]
f_pred(x::Real, w) = w[1]*x + w[2]*1

x_lims = extrema(iris.PetalLength) .+ [-0.1, 0.1]

Expand Down Expand Up @@ -89,7 +109,9 @@ iris_reduced = @from i in iris begin
label = i.Species == "virginica",
}
@collect DataFrame
end
end

# EXTRA

iris_reduced2 = iris[iris.Species .!= "setosa", :]
iris_reduced2 = iris_reduced2[:,[3;4;5]]
Expand All @@ -115,7 +137,20 @@ y = iris_reduced.label
legend = :topleft,
)

σ(z) = 1/(1+exp(-z))
# EXTRA

ii = iris_reduced.Species .== "virginica"

scatter(iris_reduced.PetalLength[ii,:], iris_reduced.PetalWidth[ii,:];
xlabel = "Petal length",
ylabel = "Petal width",
legend = :topleft,
label = "virginica"
)

scatter!(iris_reduced.PetalLength[.!ii,:], iris_reduced.PetalWidth[.!ii,:];
label = "versicolor"
)

# Exercise

Expand All @@ -132,6 +167,16 @@ end

w = log_reg(X, y, zeros(size(X,2)))

# EXTRA

function g(w, X, y)
n = size(X,1)
y_hat = σ.(X*w)
return X'*(y_hat - y) / n
end

optim([], w -> g(w, X, y), [0;0;0], GD(5e-1); max_iter=1000000)

# Plot the separating hyperplane

separ(x, w) = (-w[3]-w[1]*x)/w[2]
Expand Down

0 comments on commit ded94e6

Please sign in to comment.