-
Notifications
You must be signed in to change notification settings - Fork 124
feat: method for clustering new data kmeans added #238
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
base: master
Are you sure you want to change the base?
Changes from all commits
82e9ab2
9cccfb2
32cc28e
86bc032
f33eedd
546805d
a11a636
ca29e80
48a8c0e
30c98f9
022c2f7
467b4be
7d4b7d4
d51e5d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Common utilities | ||
|
||
##### common types | ||
using Distances | ||
|
||
""" | ||
ClusteringResult | ||
|
@@ -70,3 +70,31 @@ function updatemin!(r::AbstractArray, x::AbstractArray) | |
end | ||
return r | ||
end | ||
|
||
|
||
""" | ||
assign_clusters(X::AbstractMatrix{<:Real}, R::ClusteringResult; kwargs...) -> Vector{Int} | ||
|
||
Assign the samples specified as the columns of `X` to the corresponding clusters from `R`. | ||
|
||
# Arguments | ||
- `X`: Input data to be clustered. | ||
- `R`: Fitted clustering result. | ||
davidbp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Keyword arguments | ||
- Cluster specific keyword arguments. For example, see the `assign_clusters` method in | ||
[`kmeans`](@ref) for the description of optional `kwargs`. | ||
|
||
""" | ||
function assign_clusters( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's some misunderstanding of how the generic assign_clusters(X::AbstractMatrix, R::ClusteringResult; kwargs...) =
error("assign_clusters(X, R::$(typeof(R))) not implemented") Your current implementation can only work with assign_clusters(X::AbstractMatrix, R::KMeansResult; distance::SemiMetric = SqEuclidean()) So in the end we will have the two implementations of the Pls let me know if you have any questions regarding this logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hopefully the new PRs adress this with a "fallback" implementation that returns not implemented (in utils.jl)
and a specific kmeans implementation (in kmeans.jl) that does the computation |
||
X::AbstractMatrix{T}, | ||
R::ClusteringResult; | ||
distance::SemiMetric = SqEuclidean(), | ||
pairwise_computation::Bool = true) where {T} | ||
|
||
if !(typeof(R) <: KmeansResult) | ||
throw(MethodError(assign_clusters, | ||
"NotImplemented: assign_clusters not implemented for R of type $(typeof(R))")) | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,4 +204,15 @@ end | |
end | ||
end | ||
|
||
@testset "get cluster assigments" begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add the testset to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the test to cover the case |
||
X = rand(5, 100) | ||
R = kmeans(X, 10; maxiter=200) | ||
reassigned_clusters = assign_clusters(X, R; pairwise_computation=true); | ||
@test R.assignments == reassigned_clusters | ||
|
||
reassigned_clusters2 = assign_clusters(X, R; pairwise_computation=false); | ||
@test R.assignments == reassigned_clusters2 | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Test | ||
using Clustering | ||
using Distances | ||
|
||
@testset "get cluster assigments not implemented method" begin | ||
|
||
X = rand(10,5) | ||
dist = pairwise(SqEuclidean(), X, dims=2) | ||
R = kmedoids!(dist, [1, 2, 3]) | ||
|
||
@test_throws MethodError assign_clusters(X, R); | ||
end |
Uh oh!
There was an error while loading. Please reload this page.