kip

kip

ex_cldr Core Team

Silhouette score of Scholar.Cluster.KMeans.fit/2 output

I’m working to do color clustering (aka color palette, common colors, dominant colors) of images. One very useful technique is k-means clustering which is implemented in Scholar.

After k-means clustering I’d like to be able to get the silhouette_score which is implemented at Scholar.Metrics.Clustering.silhouette_score/3. That would seem to be a very natural step to validate or refuge the selection of the cluster size that was used (num_clusters: 16 below).

However the output generated by fit/2 doesn’t seem to play well with with silhouette_score/2 despite them being in a similar domain.

iex> kmeans = Image.Nx.kmeans(i, num_clusters: 16)
%Scholar.Cluster.KMeans{
  num_iterations: #Nx.Tensor<
    s64
    EXLA.Backend<host:0, 0.2302565275.2944008210.1119>
    24
  >,
  clusters: #Nx.Tensor<
    f32[16][3]
    EXLA.Backend<host:0, 0.2302565275.2944008210.1120>
    [
      [29.555273056030273, 22.40241813659668, 18.988496780395508],
      [221.6188507080078, 186.17681884765625, 115.16548919677734],
      [158.05177307128906, 123.88009643554688, 53.33770751953125],
      [127.1727294921875, 95.57713317871094, 41.13895034790039],
      [62.95863342285156, 47.129539489746094, 33.71294021606445],
      [97.46968841552734, 79.16098022460938, 61.60565185546875],
      [204.48558044433594, 216.12306213378906, 231.07981872558594],
      [161.0527801513672, 182.42922973632812, 208.91531372070312],
      [131.0211944580078, 107.85687255859375, 78.05144500732422],
      [190.57359313964844, 153.97003173828125, 62.70090103149414],
      [193.35650634765625, 161.45425415039062, 100.00911712646484],
      [162.95822143554688, 135.37939453125, 89.99324035644531],
      [222.13687133789062, 185.55056762695312, 75.35253143310547],
      [95.11575317382812, 67.52027893066406, 28.772855758666992],
      [250.12782287597656, 223.23638916015625, 149.09202575683594],
      [244.928466796875, 210.7081298828125, 104.9043960571289]
    ]
  >,
  inertia: #Nx.Tensor<
    f32
    EXLA.Backend<host:0, 0.2302565275.2944008210.1121>
    33850068.0
  >,
  labels: #Nx.Tensor<
    s64[91767]
    EXLA.Backend<host:0, 0.2302565275.2944008210.1122>
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
  >
}

The function Image.Nx.kmeans/2 is just:

    def kmeans(%Vimage{} = image, options \\ []) do
      {_count, colors} = unique_colors(image)
      Scholar.Cluster.KMeans.fit(colors, options)
    end

It feels like it would be reasonable to be able to call Scholar.Metrics.Clustering.silhouette_score(colors, kmeans.clusters) but that’s very definitely not the signature.

Any advice or guidance on how to apply the results from fit/2 to silhouette_score/2 would be greatly appreciated.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement