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

Update examples/crossval.jl #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions examples/crossval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
# of the RMSE (root-mean-square-error) evaluated on the testing set
#

using Printf: @printf
using MLBase

# functions

function compute_center(X::Matrix{Float64})
c = vec(mean(X, 2))
c = vec(mean(X; dims=2))
@printf("training on %d samples => (%.4f, %.4f)\n",
size(X,2), c[1], c[2])
return c
end

function compute_rmse(c::Vector{Float64}, X::Matrix{Float64})
v = sqrt(mean(sum(abs2(X .- c),1)))
v = sqrt(mean(sum(abs2.(X .- c); dims=1)))
@printf("RMSE on test set: %.6f\n\n", v)
return v
end
Expand All @@ -29,14 +30,12 @@ const data = [2., 3.] .+ randn(2, n)

# cross validation

(c, v, inds) = cross_validate(
scores = cross_validate(
inds -> compute_center(data[:, inds]), # training function
(c, inds) -> compute_rmse(c, data[:, inds]), # evaluation function
n, # total number of samples
Kfold(n, 5), # cross validation plan: 5-fold
Reverse) # smaller score indicates better model
Kfold(n, 5)) # cross validation plan: 5-fold

# display results

@printf("best model = (%.4f, %.4f), score = %.6f\n", c[1], c[2], v)

@show scores