You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#############################################
'''
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
`
The text was updated successfully, but these errors were encountered:
`
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
`
The text was updated successfully, but these errors were encountered: