Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage #87

Open
jaraheel opened this issue Dec 29, 2020 · 0 comments
Open

Example usage #87

jaraheel opened this issue Dec 29, 2020 · 0 comments

Comments

@jaraheel
Copy link

jaraheel commented Dec 29, 2020

`
using DelimitedFiles
using MLJLinearModels

#############################################
'''
wrapper for the logistic regression function with elastic
net penalty provided by MLJLinearModels
'''
#############################################
function elastic_net_logistic_regression(Z::Matrix{Float64},
y::Vector{Float64},
λ::Float64 = 1.0,
α::Float64 = 0.0)
model = LogisticRegression(λ, α, penalty=:en)
return MLJLinearModels.fit(model, Z, y)
end

#############################################
'''
as MLJLinearModels require the labels to be +1, -1 instead
of 0, +1, the following code will read the data and the
labels (0, +1). Then it shall convert the labels to +1, -1
'''
#############################################
M = readdlm("train_data.txt", Float64)
n, m = size(M)
X = Matrix(M[1:n,1:m-1])
y = M[1:end,m:end][:]
z = copy(y) # z shall be the vector of labels
for i = 1:n
if y[i] == 0.0
z[i] = -1.0
end
end

#############################################
'''
compute the parameter vector on the training data
'''
#############################################
function train(X, y)
m, n = size(X)
β_bar = elastic_net_logistic_regression(X, y, 0.0, 0.0)[1:n] # ignore the intercept
return β_bar
end

#############################################
'''
make predictions after training
'''
#############################################
function generate_predictions(X, β)
n, m = size(X)
pred_y = zeros(n)
for i = 1:n
p = sigmoid(X[i,:], β)
if isinf(p)
println("error. sigmoid returned Inf")
end
if p >= 0.5
pred_y[i] = 1.0
else
pred_y[i] = -1.0
end
end
return pred_y
end

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant