question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(kMeans, cosine) wrong number of centroids (?)

See original GitHub issue

I’m trying to cluster sentences using TF-IDF, cosine similarity and k-means algorithm. I calculate TF-IDF using natural library then I prepare vectors that look like:

[0, 0, 0, 0, 0, 1.24225242, 0, 0, 0, 0, 0] where every value represents a word and the whole vector represents a sentence (in case of this example it would be a sentence with one word).

Then I try to use talisman library to make clusters using created vectors and metrics/distance/cosine. The problem is that I get:

TypeError: Cannot read property 'length' of undefined
    at KMeans.cosine [as distance] (.../node_modules/talisman/metrics/distance/cosine.js:29:21)
    at KMeans.iterate (.../node_modules/talisman/clustering/k-means.js:164:22)
    at KMeans.run (.../node_modules/talisman/clustering/k-means.js:224:12)
    at kMeans (.../node_modules/talisman/clustering/k-means.js:243:21)
    at Object.<anonymous> (.../lev-kmeans.js:39:22)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

I tried looking into the code and the reason it fails is this piece of code (https://github.com/Yomguithereal/talisman/blob/master/src/clustering/k-means.js#L148).

for (let j = 0, m = this.dimensions; j < m; j++) {
     const d = this.distance(vector, this.centroids[j]);

     if (d < min) {
         min = d;
         minIndex = j;
     }
}

It tries to enter cosine.js functions using undefined as the second vector. For example for k equal 2 it should generate 2 centroids. It does. But this loop is trying to access centroids with very high indices like 181 (this.dimensions and thus m is equal 182).

It works fine when I tested it for simpler stuff using Euclidean distances.

To be complete I add the way I initialize the kMeans:

const clusters = kMeans({
    distance: cosine,
    k: 6,
}, vectors)

Where cosine is a const cosine = require('talisman/metrics/distance/cosine').

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:24 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
Yomguitherealcommented, Feb 15, 2017

Ok, the thing was that in the data you provided in the repo, the description key seems to always yield only three tokens.

So are we going in the good direction?

0reactions
galkowskitcommented, Feb 15, 2017

Interesting. Do you mean to keep, for instance, the top n tokens of your document using TF-IDF score?

Yes. Basically each cluster would be summarized by top n TF-IDF tokens and those would be saved. Just for auto-tagging. I was thinking that those could also be useful for classification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cosine distance as vector distance function for k-means
Let's divide cosine similarity into parts and see how and why it works. Cosine between 2 vectors - a and b - is...
Read more >
k-means cluster, How to re-calculate centroid when using ...
My problem is how I can re-calculate the centroid vector for each iteration base on cosine similarity? Can I still use average e.g.:...
Read more >
K-Means Clustering with Math - Towards Data Science
K-Means clustering is a type of unsupervised learning. The main goal of this algorithm to find groups in data and the number of...
Read more >
On clustering using k-means (cosine similarity) #1602 - GitHub
Currently, we are clustering with the following code. We are searching by L2 distance, but we want to search by cosine similarity.
Read more >
k-means clustering - MATLAB kmeans - MathWorks
For example, specify the cosine distance, the number of times to repeat the ... [ idx , C ] = kmeans(___) returns the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found