7 ms·
Generalized K-Means Clustering
- derrickrburns 3y agoAI has sparked new interest in high dimensional embeddings for approximate nearest neighbor search. Here is a highly scalable, implementation of a companion technique, k-means clustering that uses Spark 1.1 written in Scala. Please let me know if you fork this library and update it to the latter versions of Spark.
- bravura 3y agoJust curious, have you actually profiled this against running on a single large-memory machine?
- staticautomatic 3y agoWhat are people using k-means for? I can count on one hand the number of times I’ve had a good a priori rationale for the value of k.
- boredemployee 3y agoknowledge graphs
- trc001 3y agoReal estate price estimates would be the classic, and frankly still common, example
- JackeJR 3y agoSMOTE
- dilyevsky 3y agodata segmentation (e.g Voronoi), grouping (like search query results), anomaly detection. lots of different things
- theophrastus 3y agoAdditional small molecule pharmaceutical candidates via molecular descriptors
- brianleb 3y agoSo after re-reading your comment a few times, I am left with this thought: either you don't understand what k-means clustering is, or I don't understand what k-means clustering is. I wouldn't describe myself as a machine learning expert, but I have taken some grad level classes in statistics, analytics, and methods like this/related to this. So my question is... could you elaborate?
- bagels 3y agoYou have to choose the number of clusters, before using k-means. Imagine that you have a dataset, where you think there are likely meaningful clusters, but you don't know how many, especially where it's many-dimensioned. If you pick a k that is too small, you lump unrelated points together. If k is too large, your meaningful clusters will be fragmented/overfitted. There are some algorithms that try to estimate the number of clusters or try to find the k with the best fit to the data to make up for this.
- keenmaster 3y agoCouldn’t you make some educated guesses and then stop when you arrive at a K that gives you meaningful clusters that are neither too high level nor too atomized.
- yarg 3y agoProbably not the best in terms of efficiency. Easier just to deliberately overshoot (with a too high k) and then merge any clusters with too much overlap.
- dilippkumar 3y agoNot GP, but I understood their question as follows. Assume you collect some kindergartners and top NBA players into a room and collect their heights. Now say you pass these to two hapless grad students and ask them to perform K-means clustering. Suppose one of the grad students knew the composition of the people you measured and can guess these height should clump into 2 nice clusters. The other student who doesn't know the composition of the class - what should they guess K to be? I understood the GP's comment to refer to the state of the second grad student. How useful is K-means clustering without knowing K in advance?
- derwiki 3y agoUsed it in college to downscale an X color image to Y number of colors. Sure, Photoshop does it, but it was informative to do it manually.
- refulgentis 3y agoGoogle's Material You uses this to initiate color theming (n.b. Celebi's, note usage of Lab / notHSL, respect Cartesian / polar nature of inputs / outputs, and ask for high-K, 128 is what we went with but it's arbitrary. Can get away with as few as 32 if you're ex. Doing brand color from favicon)
- girvo 3y agoOoh this is a much nicer approach than the kind of brute force approach we took at work for theme gen
- Waterluvian 3y agoConstantly in remote sensing and GIS work.
- bagels 3y agoMeasuring multiple physical objects with the same sensor, you can use k-means to separate the measurements from each object, given that you know how many objects are being sensed. I can't get more specific than that.
- tim-fan 3y agoSounds like you're describing my carpet-color classification project! [1] Built as part of a larger carpet based localisation project [2] 1: https://nbviewer.org/github/tim-fan/carpet_color_classification/blob/main/notebooks/train_classifier.ipynb https://nbviewer.org/github/tim-fan/carpet_color_classificat... 2: https://github.com/tim-fan/carpet_localisation/wiki/Carpet-Localisation https://github.com/tim-fan/carpet_localisation/wiki/Carpet-L...
- sshumaker 3y agoSometimes you can use a heuristic to estimate K, or use a variant that terminates at some distance threshold. That said, something like hdbscan doesn’t suffer from this problem.
- minimaxir 3y agok-means is good for fast unsupervised clustering on an unknown low-dimensional dataset. It's helpful for EDA. If you want accuracy at an order of magnitude more compute, you can use something like DBSCAN.
- icelancer 3y agoBaseball pitch types based on physics profiles.
- jncfhnb 3y agoK is 3. It’s honestly fine for just finding key differences like a principal component for light storytelling. They don’t need to be distinct clusters
- dhosek 3y agoI did a modified version of this once for a map of auto dealerships, although rather than working with a fixed k, I used a fix threshold for cluster distance. The algorithm I was working with had O(n³) complexity so to keep the pregeneration of clusters manageable, I partitioned data by state. The other fun part was finding the right metric formula for measuring distances. Because clusters needed to correspond to the rectangular view window on the map, rather than a standard Euclidean distance, I used d = max(Δx,Δy) which gives square neighborhoods rather than round ones.
- calvinmorrison 3y agoWe used k means clustering on a project used to track fruit fly memory and learning behaviors http://git.ceux.org/FlyTracking.git/ http://git.ceux.org/FlyTracking.git/
- web9ed 3y agoI would recommend checking out DBSCAN as it is similar without having to provide a number k https://en.m.wikipedia.org/wiki/DBSCAN https://en.m.wikipedia.org/wiki/DBSCAN
- Konnstann 3y agoI've used something similar for tissue segmentation from hyperspectral images of animals where I know there should be K different tissue types I care about.
- missingrib 3y agoI've used it for identifying dominant colors in images.
- hansvm 3y agoThe kmeans metric is exactly the metric you would want to optimize the performance of an algorithm like [bolt](https://arxiv.org/abs/1706.10283 https://arxiv.org/abs/1706.10283). In that and other discretization routines, the value of k is a parameter related to compression ratio, efficiency, and other metrics more predictable than some ethereal notion of how many clusters the data "naturally" has.
- magicalhippo 3y agoI implemented an algorithm which used k-means to reduce noise in a path tracer. For each pixel instead of a single color value it generated k mean color values, using an online algorithm. These were then combined to produce the final pixel color. The idea was that a pixel might have several distinct contributions (ie from different light sources for example), but due to the random sampling used in path tracing the variance of sample values is usually large. The value k then was chosen based on scene complexity. There was also a memory trade-off of course, as memory usage was linear in k.
- le-mark 3y ago> For each pixel instead of a single color value it generated k mean color values, using an online algorithm. What does online mean here?
- magicalhippo 3y agoOnline means it processes the items as they come[1]. This means the algorithm can't consider all the items at once, and has to try to be clever on the spot. In my case the algorithm uswd would use the first k samples as the initial means, and would then find which of the k means were closest to the current sample, and update that mean[2]. Given that in parh tracing one would typically use a fairly large number of samples per pixel relative to k, this approach did a reasonable job of approximating the k means. [1]: https://en.wikipedia.org/wiki/Online_algorithm https://en.wikipedia.org/wiki/Online_algorithm [2]: https://yokolet.com/2017/05/29/online-algorithm.html https://yokolet.com/2017/05/29/online-algorithm.html
- tomnipotent 3y agoFrequently used in e-commerce, such as RFM clustering for targeted marketing.
- patcon 3y agoPolis (and Twitter's community notes, I believe) Participation At Scale Can Repair The Public Square https://www.noemamag.com/participation-at-scale-can-repair-the-public-square/ https://www.noemamag.com/participation-at-scale-can-repair-t... Polis: Scaling deliberation by mapping high dimensional opinion spaces https://scholar.google.com/scholar?q=Polis:+Scaling+deliberation+by+mapping+high+dimensional+opinion+spaces&hl=en&as_sdt=0&as_vis=1&oi=scholart https://scholar.google.com/scholar?q=Polis:+Scaling+delibera... Restricting clustering to 2-5 groups impacts group aware/informed consensus and comment routing https://github.com/compdemocracy/polis/issues/1289 https://github.com/compdemocracy/polis/issues/1289
- minimaxir 3y agoI built a pipeline to automatically cluster and visualize large amounts of text documents in a completely unsupervised manner: - Embed all the text documents. - Project to 2D using UMAP which also creates its own emergent "clusters". - Use k-means clustering with a high cluster count depending on dataset size. - Feed the ChatGPT API ~10 examples from each cluster and ask it to provide a concise label for the cluster. - Bonus: Use DBSCAN to identify arbitrary subclusters within each cluster. It is extremely effective and I have a theoetical implementation of a more practical use case to use said UMAP dimensionality reduction for better inference. There is evidence that current popular text embedding models (e.g. OpenAI ada, which outputs 1536D embeddings) are way too big for most use cases and could be giving poorly specified results for embedding similarity as a result, in addition to higher costs for the entire pipeline.
- potatoman22 3y agoInteresting. What do you use the visualization for? Looking at trends in the documents?
- minimaxir 3y agoLet's say you want to look at a large dataset of user-submitted reviews for you app. User reviews are written extremely idiosyncratic so all traditional NLP methods will likely fail. With the pipeline mentioned, it's much easier to look at cluster density to identify patterns and high-level trends.
- jncfhnb 3y agoWhy not just use DBSCAN though
- minimaxir 3y agoYou can use DBSCAN instead of k-means, but DBSCAN has a worst-case memory complexity of O(n^2) so things can get spicy with large datasets, which is why I opt it to only use it for subclusters. k-means also fixes the number of clusters, which is good for visualization sanity. https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html https://scikit-learn.org/stable/modules/generated/sklearn.cl...
- milofeynman 3y agoI applied to a certain scraping fintech in the Bay Area around 5 years ago and was asked to open the Wikipedia page to k-means squared clustering and implement the algorithm with tests from scratch. I was applying for an android position. I still laugh thinking about how they paid to fly me out and ask such a stupid interview question.
- alluro2 3y agoI see how it might not have anything to do with usual Android development, but why do you consider it a stupid question? K-means is not that complicated and naive implementation with e.g. Euclidean distance is a couple of dozens of lines of code, so should be practical enough for an interview.
- zoogeny 3y agoThere is a Twitch streamer Tsoding who posted a video of himself implementing K-means clustering in C recently [1]. He also does a follow up 3d visualization of the algorithm in progress using raylib [2]. 1. https://www.youtube.com/watch?v=kH-hqG34ylA&t=4788s&ab_channel=TsodingDaily https://www.youtube.com/watch?v=kH-hqG34ylA&t=4788s&ab_chann... 2. https://www.youtube.com/watch?v=K7hWqxC_7Mw&ab_channel=TsodingDaily https://www.youtube.com/watch?v=K7hWqxC_7Mw&ab_channel=Tsodi...
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- atum47 3y agoI remember when i first learned k-means, it opened the door for so many projects. Two that are on my GitHub to this day are a python script that groups your images by similarly (histogram) and one that classify your expenses based on previous data. I had so much fun working on those.
- namuol 3y agoHere’s a very simple toy demonstration of how K-Means works that I made for fun years ago while studying machine learning: https://k-means.stackblitz.io/ https://k-means.stackblitz.io/ Essentially K-Means is a way of “learning” categories or other kinds of groupings within an unlabeled dataset, without any fancy deep learning. It’s handy for its simplicity and speed. The demo works with simple 2D coordinates for illustrative purposes but the technique works with any number of dimensions. Note that there may be some things I got wrong with the implementation and that there are other variations of the algorithm surely, but it still captures the basic idea well enough for an intro.
- anArbitraryOne 3y agoFun fact: K-Means is the least interesting clustering algorithm known to humans, but is quite fast and therefore useful in certain applications
- snovv_crash 3y agoIt's boring, in a sense that it always gives reasonable results and is easy to implement. It also scales well in N, D and K, and from my experience converges in just a few iterations from anything better than a pure random initialisation strategy. IMO it is very good as a final clustering algorithm once you've already applied some more complex transformations on your data to linearise it and account for deeper knowledge of the problem. This might be a spectral space transformation (you care about connectedness), or an embedding (you care about whatever the network was trained on) or descriptor (you care about the algorithm's similarity). But once you've applied the transform, you then have a minimalist fast scalable clustering that just does clustering and doesn't need to know anything more about the problem being solved. Very unix-y feeling.
- fiddlerwoaroof 3y agoDoes the “curse of dimensionality” affect the usefulness of k-means?
- Scene_Cast2 3y agoCheck out sampling with lightweight coresets if your data is big - it's a principled approach with theoretical guarantees, and it's only a couple of lines of numpy. Do check if the assumptions hold for your data though, as they are stronger than with regular coresets.
- tomtom1337 3y agoDo you have a link to any implementations for this?
- Scene_Cast2 3y agohttps://github.com/OOub/coreset/blob/main/coreset/coreset.py https://github.com/OOub/coreset/blob/main/coreset/coreset.py
- Scene_Cast2 3y agoAlthough K-means clustering is often the correct approach given time crunch and code complexity constraints, I don't like how it's hard to extend and how it's not principled. By not principled, I mean that it feels more like an algorithm (that happens to optimize) rather than an explicit optimization with an explicit loss function. And I found that in practice, modifying the distance function to anything more interesting doesn't work.
- blackbear_ 3y agoK-means clustering is very well principled actually as an instance of the expectation maximization algorithm with "hard" cluster assignment. Turns out it's just good old maximum likelihood: https://alliance.seas.upenn.edu/~cis520/dynamic/2022/wiki/index.php?n=Lectures.EM https://alliance.seas.upenn.edu/~cis520/dynamic/2022/wiki/in...
- Scene_Cast2 3y agoThere are two issues I had in mind. One is that the link between argmin and the algorithm (k-means in this case) feels too "tied to the algorithm" and less explicit than in other algorithms. The other is that in practice, you typically want to bring your true optimization objective as close as possible to what the algorithm is optimizing, and what k-means is optimizing for is usually pretty far removed. Even small tweaks (lets say, augmenting data with some sparse labels, or modifying the loss function weight based on some aspect of embedding values) are difficult to do with k-means.
- selimthegrim 3y agoIsn’t it also formally equivalent to a Gaussian mixture model? https://timydaley.github.io/kmeans_gmm/gmm_vs_kmeans.html https://timydaley.github.io/kmeans_gmm/gmm_vs_kmeans.html