# Error using kmean clustering

**URL:** <https://discourse.julialang.org/t/error-using-kmean-clustering/64184>\
**Category:** New to Julia\
**Tags:** clustering\
**Created:** [July 7, 2021, 12:12am UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184 "2021-07-07T00:12:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [July 7, 2021, 12:12am UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/1 "2021-07-07T00:12:43Z")

</div>

I am getting the following error when I try to use the kmean function:  
Julia version 1.5.3  
All the packages are updated

```julia
using Clustering
using Distances
using Statistics

X = rand(5, 1000)
R = kmeans(X, 20; maxiter=200, display=:iter)

```

 ![Screen Shot 2021-07-06 at 8.10.28 PM](https://global.discourse-cdn.com/julialang/original/3X/6/5/65d71069b3246c0432ee1e5ac622cae063959101.png)

---

<div class="post-metadata">

**Author:** ![eliassno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassno/32/18917_2.png) [@eliassno](https://discourse.julialang.org/u/eliassno)\
**Post date:** [July 7, 2021, 12:22am UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/2 "2021-07-07T00:22:39Z")

</div>

I can’t reproduce the error:

```julia
julia> using Clustering

julia> using Distances

julia> using Statistics

julia> X = rand(5, 1000);

julia> R = kmeans(X, 20; maxiter=200, display=:iter)
  Iters objv objv-change | affected 
-------------------------------------------------------------
      0 2.041035e+02
      1 1.453690e+02 -5.873454e+01 | 20
      2 1.369302e+02 -8.438822e+00 | 20
      3 1.339511e+02 -2.979075e+00 | 20
      4 1.320703e+02 -1.880780e+00 | 20
      5 1.311573e+02 -9.130492e-01 | 20
      6 1.304643e+02 -6.929885e-01 | 19
      7 1.298840e+02 -5.802423e-01 | 15
      8 1.293135e+02 -5.705580e-01 | 15
      9 1.287156e+02 -5.978245e-01 | 15
     10 1.277974e+02 -9.181926e-01 | 15
     11 1.272536e+02 -5.438166e-01 | 15
     12 1.269656e+02 -2.880654e-01 | 15
     13 1.266944e+02 -2.712121e-01 | 15
     14 1.263861e+02 -3.082485e-01 | 18
     15 1.262145e+02 -1.716252e-01 | 10
     16 1.261344e+02 -8.009206e-02 | 4
     17 1.261112e+02 -2.315701e-02 | 2
     18 1.260852e+02 -2.602578e-02 | 3
     19 1.260378e+02 -4.740786e-02 | 2
     20 1.260236e+02 -1.416028e-02 | 5
     21 1.259967e+02 -2.694733e-02 | 4
     22 1.259770e+02 -1.970601e-02 | 5
     23 1.259224e+02 -5.454997e-02 | 5
     24 1.258796e+02 -4.281383e-02 | 3
     25 1.258489e+02 -3.076725e-02 | 6
     26 1.257414e+02 -1.074401e-01 | 7
     27 1.255379e+02 -2.034701e-01 | 11
     28 1.252256e+02 -3.123244e-01 | 11
     29 1.250767e+02 -1.488813e-01 | 15
     30 1.248356e+02 -2.411395e-01 | 13
     31 1.245145e+02 -3.211148e-01 | 14
     32 1.240144e+02 -5.000809e-01 | 13
     33 1.236681e+02 -3.462645e-01 | 11
     34 1.234899e+02 -1.782160e-01 | 6
     35 1.234166e+02 -7.331246e-02 | 2
     36 1.234007e+02 -1.591534e-02 | 4
     37 1.232947e+02 -1.059802e-01 | 4
     38 1.231907e+02 -1.040324e-01 | 4
     39 1.231226e+02 -6.811605e-02 | 4
     40 1.230901e+02 -3.242738e-02 | 0
     41 1.230901e+02 0.000000e+00 | 0
K-means converged with 41 iterations (objv = 123.09013843381697)
KmeansResult{Array{Float64,2},Float64,Int64}([0.8097632681462386 0.5942172853403593 … 0.41041465915327047 0.2871475920064936; 0.4423317451301203 0.7727740451896872 … 0.20892369888129614 0.33620661064553803; … ; 0.2638807518254864 0.7631791027492311 … 0.2706224597515288 0.7700235299058837; 0.7422600696416772 0.787743528767687 … 0.3020438637946737 0.20631202895324988], [1, 15, 2, 19, 12, 6, 9, 20, 5, 8 … 10, 17, 9, 9, 15, 11, 14, 3, 2, 12], [0.23008209271154545, 0.0925096532284333, 0.18198522158592567, 0.0936893822488023, 0.03152772952743099, 0.1628032272375204, 0.14118752288088166, 0.1298405914640286, 0.1311384618873661, 0.07454307862249632 … 0.008549293563550275, 0.14804896139196555, 0.16342915243080292, 0.14462644500488597, 0.1275835264533618, 0.18969570228617894, 0.07135802657533619, 0.06272456250491709, 0.18269737453993606, 0.19772766113312157], [36, 55, 57, 53, 40, 51, 44, 57, 52, 48, 49, 46, 51, 56, 53, 49, 37, 63, 49, 54], [36, 55, 57, 53, 40, 51, 44, 57, 52, 48, 49, 46, 51, 56, 53, 49, 37, 63, 49, 54], 123.09013843381697, 41, true

```

Maybe check your versions:

```julia
(@v1.5) pkg> st Clustering Distances
Status `~/.julia/environments/v1.5/Project.toml`
  [aaaa29a8] Clustering v0.14.2
  [b4f34e82] Distances v0.10.3

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [July 7, 2021, 12:30am UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/3 "2021-07-07T00:30:13Z")

</div>

This is what I get (The same as yours):

```julia
(@v1.5) pkg> st Clustering Distances
Status `~/.julia/environments/v1.5/Project.toml`
  [aaaa29a8] Clustering v0.14.2
  [b4f34e82] Distances v0.10.3

```

This is very annoying. I don’t know what is the problem. It used to work but it just stopped working and giving me errors. I restarted Julia but it didn’t help.

---

<div class="post-metadata">

**Author:** ![Berz\_Qwer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/berz_qwer/32/10467_2.png) [@Berz\_Qwer](https://discourse.julialang.org/u/Berz_Qwer)\
**Post date:** [September 5, 2021, 8:15pm UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/5 "2021-09-05T20:15:12Z")

</div>

At first I faced the same issue. after updating to the latest versions I started getting

> . Illegal inttoptr  
> %37 = ptrtoint double addrspace(13)\* %36 to i64  
> Illegal inttoptr  
> %56 = inttoptr i64 %55 to i8 addrspace(13)\*

Absolutely meaningless description for me  
Only kmeans craches. The other methods (kmedoids, dbscan and so on) work correctly

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 6, 2021, 1:23pm UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/6 "2021-09-06T13:23:48Z")

</div>

If one wants, KMeans (Kmedoids, GMM,…) is also available in the [BetaML](https://github.com/sylvaticus/BetaML.jl) package:

```julia
julia> using BetaML

julia> X = rand(5,1000)
5×1000 Matrix{Float64}:
 0.0715517 0.100949 0.3514 0.503785 … 0.157863 0.0471136 0.16259 0.575626
 0.856428 0.221503 0.0362716 0.543169 0.608833 0.504425 0.197979 0.255666
 0.63662 0.202124 0.404668 0.425744 0.942191 0.649821 0.263136 0.0224259
 0.622316 0.28606 0.260308 0.217047 0.404194 0.534265 0.475211 0.22486
 0.257915 0.547401 0.655035 0.2384 0.484519 0.728864 0.908609 0.0515379

julia> R = kmeans(X,20)
([10, 10, 11, 10, 11], [0.09908971856173475 0.11528474365765297 … 0.18562366412084563 0.040032244612623044; 0.13755257307199592 0.13716311464831238 … 0.22218234341572918 0.06714182899564042; … ; 0.7914210997464357 0.5090954214895226 … 0.8436798914287494 0.5280047635069359; 0.8298839542566969 0.5309737924801818 … 0.8802385707236331 0.5551143478899533])

julia> S = kmeans(X,20,initStrategy="random")
([7, 14, 11, 6, 14], [0.6645401579066887 0.24639879500963735 … 0.35925334730152403 0.39712960954993387; 0.7034044964027021 0.28444705440179846 … 0.18323978520955944 0.2791541389879703; … ; 0.7069184750950341 0.4924644518658278 … 0.6396535310804825 0.3863272262424791; 0.5599258108009191 0.4799037791696229 … 0.616686683602457 0.5139395438797025])

```

Note in this case the importance of the centroid init strategy…

Doc: [Clustering · BetaML.jl Documentation](https://sylvaticus.github.io/BetaML.jl/stable/Clustering.html#BetaML.Clustering.kmeans-Tuple%7BAny,%20Any%7D)  
(or use the MLJ interface)

---

<div class="post-metadata">

**Author:** ![Berz\_Qwer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/berz_qwer/32/10467_2.png) [@Berz\_Qwer](https://discourse.julialang.org/u/Berz_Qwer)\
**Post date:** [September 6, 2021, 5:14pm UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/7 "2021-09-06T17:14:00Z")

</div>

The same issue with BetaML. The problem is systematic

пн, 6 сент. 2021 г., 16:29 Antonello Lobianco via JuliaLang \<[julialang@discoursemail.com](mailto:julialang@discoursemail.com)\>:

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 6, 2021, 5:59pm UTC](https://discourse.julialang.org/t/error-using-kmean-clustering/64184/8 "2021-09-06T17:59:58Z")

</div>

Then reinstall Julia and clean the Julia directory…
