Skip to content

Commit

Permalink
uses the simpler KnnResult api
Browse files Browse the repository at this point in the history
  • Loading branch information
sadit committed Jan 16, 2022
1 parent 3856205 commit 9efca5b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KNearestCenters"
uuid = "4dca28ae-43b8-11eb-1f5e-d55054101997"
authors = ["Eric S. Tellez"]
version = "0.6.1"
version = "0.6.2"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
5 changes: 2 additions & 3 deletions src/knc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ end

function predict(nc::Knc, x, res::KnnResult=reuse!(nc.res))
C = nc.centers
st = initialstate(res)
for i in eachindex(C)
d = -evaluate(nc.config.kernel, x, C[i], nc.dmax[i])
st = push!(res, st, i, d)
push!(res, i, d)
end

argmin(res, st)
argmin(res)
end

function Base.broadcastable(nc::Knc)
Expand Down
11 changes: 5 additions & 6 deletions src/kncproto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ function KncProto(
end

"""
most_frequent_label(nc::KncProto, res::KnnResult, st::KnnResultState)
most_frequent_label(nc::KncProto, res::KnnResult)
Summary function that computes the label as the most frequent label among labels of the k nearest prototypes (categorical labels)
"""
function most_frequent_label(nc::KncProto, res::KnnResult, st::KnnResultState)
c = counts([nc.class_map[id] for id in idview(res, st)], 1:nc.nclasses)
function most_frequent_label(nc::KncProto, res::KnnResult)
c = counts([nc.class_map[id] for id in idview(res)], 1:nc.nclasses)
findmax(c)[end]
end

Expand All @@ -273,14 +273,13 @@ Predicts the class of `x` using the label of the `k` nearest centers under the `
"""
function predict(nc::KncProto, x, res::KnnResult=reuse!(nc.res))
C = nc.centers
st = initialstate(res)
dmax = nc.dmax
for i in eachindex(C)
s = evaluate(nc.config.kernel, x, C[i], dmax[i])
st = push!(res, st, i, -s)
push!(res, i, -s)
end

most_frequent_label(nc, res, st)
most_frequent_label(nc, res)
end

function Base.broadcastable(nc::KncProto)
Expand Down

2 comments on commit 9efca5b

@sadit
Copy link
Owner Author

@sadit sadit commented on 9efca5b Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/52548

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.2 -m "<description of version>" 9efca5ba755adf4b0483bc9c5d192fe5336897f1
git push origin v0.6.2

Please sign in to comment.