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

InexactError: converting to Int64 using kmeans #285

Closed
aborzunov opened this issue Dec 5, 2024 · 1 comment
Closed

InexactError: converting to Int64 using kmeans #285

aborzunov opened this issue Dec 5, 2024 · 1 comment

Comments

@aborzunov
Copy link

Julia version: Version 1.11.1 (2024-10-16)
Package version:

(@v1.11) pkg> status Clustering
Status `~/.julia/environments/v1.11/Project.toml`
  [aaaa29a8] Clustering v0.15.7

Error: ERROR: InexactError: Int64(1.5143524864540896e6)

Steps to reproduce:

julia> @show fs
fs = Real[2530.6228, 3588.6382, 3734.6614, 3743.5217, 3787.343, 3781.5833, 3786.8713, 3786.8774, 3795.4502, 3797.0173, 3792.176, 3798.5337, 3802.8047, 3798.4106, 3797.0593, 3801.621, 3793.6523, 3797.8796, 3788.598, 3782.3643, 3783.711, 3782.1304, 3775.1907, 3775.5623, 3781.2214, 3778.0942, 3784.7712, 3783.2368, 3787.496, 3783.89, 3779.4907, 3778.8562, 3781.099, 3781.0989, 3783.989, 3785.749, 3779.4814, 3786.7463, 3776.925, 3786.4768, 3784.4246, 3781.518, 3786.1404, 3778.7288, 3770.2327, 3769.5112, 3769.3184, 3772.6284, 3779.833, 3778.3235, 3777.4395, 3777.688, 3772.608, 3774.4868, 3765.0747, 3774.5864, 3768.8337, 3766.881, 3763.6145, 3765.7983, 3763.7893, 3768.7952, 3771.589, 3771.647, 3770.689, 3770.2998, 3773.4404, 3760.1555, 3768.2588, 3764.1575, 3766.0757, 3766.8535, 3763.753, 3765.077, 3756.2734, 3759.2808, 3761.2131, 3767.3147, 3770.9436, 3769.1045, 3774.5337, 3763.52, 3761.4167, 3765.7832, 3765.9082, 3761.6826, 3757.2097, 3755.8606, 3758.1658, 3760.027, 3762.6748, 3758.6973, 3759.4707, 3763.619, 3757.0476, 3761.4084, 3750.9497, 3744.2056, 3746.2617, 3755.1316, 3770.0051, 3772.382, 3764.9985, 3763.3247, 3756.66, 3739.35, 3740.5583, 3748.689, 3760.9043, 3766.0525, 3761.701, 3769.4585, 3765.6973, 3769.515, 3772.6848, 3774.4402, 3774.8489, 3771.528, 3773.139, 3762.212, 3758.393, 3753.7148, 3755.2974, 3753.2249, 3751.801, 3756.4004, 3751.262, 3740.1104, 3744.5264, 3738.4866, 3736.9583, 3741.925, 3744.4048, 3745.2512, 3740.0276, 3735.7217, 3734.1936, 3731.8965, 3722.8894, 3727.5469, 3729.073, 3734, 3740.857, 3741.8394, 3742.6428, 3733.0754, 3728.5334, 3731.9055, 3740.8435, 3745.121, 3725.6047, 3728.026, 3726.7766, 3726.7058, 3726.6072, 3725.531, 3725.107, 2732.9595, 951.42993, 608.17145, 607.59, 606.88995, 605.2483, 705.0368, 613.60144, 630.2143, 673.90076, 647.07733, 611.26227, 656.975, 669.02594, 585.1286]

julia> kmeans(fs', 2)
ERROR: InexactError: Int64(1.6037550296302496e6)
Stacktrace:
  [1] Int64
    @ ./float.jl:994 [inlined]
  [2] convert
    @ ./number.jl:7 [inlined]
  [3] setindex!(A::Vector{Int64}, x::Float64, i::Int64)
    @ Base ./array.jl:976
  [4] colwise!(metric::Distances.SqEuclidean, r::Vector{Int64}, a::LinearAlgebra.Adjoint{Real, Vector{…}}, b::SubArray{Real, 1, LinearAlgebra.Adjoint{…}, Tuple{…}, true})
    @ Distances ~/.julia/packages/Distances/mlz7p/src/generic.jl:106
  [5] colwise
    @ ~/.julia/packages/Distances/mlz7p/src/generic.jl:186 [inlined]
  [6] initseeds!(iseeds::Vector{Int64}, alg::KmppAlg, X::LinearAlgebra.Adjoint{Real, Vector{Real}}, metric::Distances.SqEuclidean; rng::Random.TaskLocalRNG)
    @ Clustering ~/.julia/packages/Clustering/JwhfU/src/seeding.jl:173
  [7] initseeds! (repeats 2 times)
    @ ~/.julia/packages/Clustering/JwhfU/src/seeding.jl:160 [inlined]
  [8] initseeds
    @ ~/.julia/packages/Clustering/JwhfU/src/seeding.jl:43 [inlined]
  [9] initseeds(algname::Symbol, X::LinearAlgebra.Adjoint{Real, Vector{Real}}, k::Int64; kwargs::@Kwargs{rng::Random.TaskLocalRNG})
    @ Clustering ~/.julia/packages/Clustering/JwhfU/src/seeding.jl:75
 [10] initseeds
    @ ~/.julia/packages/Clustering/JwhfU/src/seeding.jl:75 [inlined]
 [11] kmeans(X::LinearAlgebra.Adjoint{…}, k::Int64; weights::Nothing, init::Symbol, maxiter::Int64, tol::Float64, display::Symbol, distance::Distances.SqEuclidean, rng::Random.TaskLocalRNG)
    @ Clustering ~/.julia/packages/Clustering/JwhfU/src/kmeans.jl:111
 [12] kmeans(X::LinearAlgebra.Adjoint{Real, Vector{Real}}, k::Int64)
    @ Clustering ~/.julia/packages/Clustering/JwhfU/src/kmeans.jl:95
 [13] top-level scope
    @ REPL[337]:1
Some type information was truncated. Use `show(err)` to see complete types.

Same error using kmeans(reshape(fs, 1, length(fs)), 2), but works using inline data passing:

julia> kmeans([2530.6228, 3588.6382, 3734.6614, 3743.5217, 3787.343, 3781.5833, 3786.8713, 3786.8774, 3795.4502, 3797.0173, 3792.176, 3798.5337, 3802.8047, 3798.4106, 3797.0593, 3801.621, 3793.6523, 3797.8796, 3788.598, 3782.3643, 3783.711, 3782.1304, 3775.1907, 3775.5623, 3781.2214, 3778.0942, 3784.7712, 3783.2368, 3787.496, 3783.89, 3779.4907, 3778.8562, 3781.099, 3781.0989, 3783.989, 3785.749, 3779.4814, 3786.7463, 3776.925, 3786.4768, 3784.4246, 3781.518, 3786.1404, 3778.7288, 3770.2327, 3769.5112, 3769.3184, 3772.6284, 3779.833, 3778.3235, 3777.4395, 3777.688, 3772.608, 3774.4868, 3765.0747, 3774.5864, 3768.8337, 3766.881, 3763.6145, 3765.7983, 3763.7893, 3768.7952, 3771.589, 3771.647, 3770.689, 3770.2998, 3773.4404, 3760.1555, 3768.2588, 3764.1575, 3766.0757, 3766.8535, 3763.753, 3765.077, 3756.2734, 3759.2808, 3761.2131, 3767.3147, 3770.9436, 3769.1045, 3774.5337, 3763.52, 3761.4167, 3765.7832, 3765.9082, 3761.6826, 3757.2097, 3755.8606, 3758.1658, 3760.027, 3762.6748, 3758.6973, 3759.4707, 3763.619, 3757.0476, 3761.4084, 3750.9497, 3744.2056, 3746.2617, 3755.1316, 3770.0051, 3772.382, 3764.9985, 3763.3247, 3756.66, 3739.35, 3740.5583, 3748.689, 3760.9043, 3766.0525, 3761.701, 3769.4585, 3765.6973, 3769.515, 3772.6848, 3774.4402, 3774.8489, 3771.528, 3773.139, 3762.212, 3758.393, 3753.7148, 3755.2974, 3753.2249, 3751.801, 3756.4004, 3751.262, 3740.1104, 3744.5264, 3738.4866, 3736.9583, 3741.925, 3744.4048, 3745.2512, 3740.0276, 3735.7217, 3734.1936, 3731.8965, 3722.8894, 3727.5469, 3729.073, 3734, 3740.857, 3741.8394, 3742.6428, 3733.0754, 3728.5334, 3731.9055, 3740.8435, 3745.121, 3725.6047, 3728.026, 3726.7766, 3726.7058, 3726.6072, 3725.531, 3725.107, 2732.9595, 951.42993, 608.17145, 607.59, 606.88995, 605.2483, 705.0368, 613.60144, 630.2143, 673.90076, 647.07733, 611.26227, 656.975, 669.02594, 585.1286]', 2)
KmeansResult{Matrix{Float64}, Float64, Int64}([3748.343334177213 655.1108621428573], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1  …  2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [1.4828432993568368e6, 25505.729882564396, 187.1953228339553, 23.248156137764454, 1520.973934289068, 1104.895327899605, 1484.404150441289, 1484.8742288313806, 2219.0568076446652, 2369.1549489200115  …  2486.2751034501707, 2492.599270915147, 1723.032126633916, 619.8388065331383, 353.0602614817908, 64.53763869032264, 1922.6990329106338, 3.4750099503435194, 193.62939177034423, 4897.517014631536], [158, 14], [158, 14], 2.7425830743179293e6, 2, true)
@alyst
Copy link
Member

alyst commented Dec 6, 2024

Thanks for the report!
The cause of the issue is that fs elements are of type Real, not Float64.
The issue itself is in Distances.jl, not in Clustering.jl; in the first colwise() call, to be precise.
There it incorrectly estimates the numeric type of the distance as Int64, not Float64 or Real:

julia> result_type(SqEuclidean(), Matrix{Real}(undef, 1, 1), Vector{Real}(undef, 1))
Int64

So I suggest you submit the bug report to Distances.jl.

Meanwhile, you can workaround it by converting fs to Vector{Float64} (inline data passing works because that vector has Float64 eltype).

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

No branches or pull requests

2 participants