News for hackers. Stuff that matters.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by avinassh on Wednesday October 17, 2018 @04:17AM (#18237156)
K - means clustering, which I learned about recently.

For non-machine learning folks, this algorithms helps in grouping relevant items together. As a practical, using this algorithm you can segment your customers based on their purchase history and interests.

The actual algorithm is very simple, imagine a lot of points on the 2D plane and each of them represent customers, whom you want to segment/cluster. Now, chose random cluster points and assign those customers to these points, by their distance. That is, whichever customer is near to a point, it belongs to that. At the end of iteration, you have lots of customers assigned to each cluster. Now, for each cluster, take the mean of those customers and the move the cluster to this mean value. Slowly, the cluster points move to the center with all the customers surrounded, belonging to that cluster.

read more - https://en.wikipedia.org/wiki/K-means_clustering

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by stevesimmons on Wednesday October 17, 2018 @07:07AM (#18237904)
The scikit-learn page on clustering [1] is a really great way to see K-means and other algorithms side-by-side. The pictures really make it easy to see how all the algorithms perform on various data distributions.

[1] http://scikit-learn.org/stable/modules/clustering.html

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by failsafe on Wednesday October 17, 2018 @04:55AM (#18237334)
The interesting part is solving the "closest pair problem" which is a part of the clustering algorithm. Hurts my head just thinking about it, god knows how someone came up with a solution like this. https://www.geeksforgeeks.org/closest-pair-of-points-using-d...
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @05:03AM (#18237366)
The closest-pair algorithm is something you might use in single-linkage clustering, not K-means clustering:

https://en.wikipedia.org/wiki/Single-linkage_clustering

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by rripken on Wednesday October 17, 2018 @01:40PM (#18241455)
Lesson two of this course does a great job explaining K-means and its connection to Expectation Maximization. https://www.udacity.com/course/machine-learning-unsupervised...
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by jacknews on Wednesday October 17, 2018 @04:27AM (#18237201)
I prefer mean-shift for it's simplicity, as it even creates it's own clusters.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @04:57AM (#18237343)
Could you explain what you mean by that a little more? I looked at the Clustering section of the Wikipedia page for Mean-Shift:

https://en.wikipedia.org/wiki/Mean_shift#Clustering

but I don't get how C and r are chosen, nor how the method is supposed to be "non-parametric" given that you need such parameters to begin with.

Also, how are you supposed to assign all points to separate clusters, and how would you determine how many clusters to use in the first place?

Having looked at the that page I also definitely don't think it's simpler than K-means!

Edit: This article gives a much more accessible descriptions than wikipedia:

https://spin.atomicobject.com/2015/05/26/mean-shift-clusteri...

To answer my own questions, the number of clusters is determined by the number of max points of the kernel density function, but the kernel bandwidth does have to be specified.

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by vanderZwan on Wednesday October 17, 2018 @09:52AM (#18238931)
Thanks for not just looking for an answer to your own question but also sharing it!

> So how does mean shift come into the picture? Mean shift exploits this KDE idea by imagining what the points would do if they all climbed up hill to the nearest peak on the KDE surface. It does so by iteratively shifting each point uphill until it reaches a peak.

I wonder if this would still work with high-dimensional data. Distance and space start to act weird in higher dimensions, right?

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @10:03AM (#18239083)
Without looking much further into it, I can't imagine it going very wrong. My intuition says that the density function will be bounded, since you're working with a finite number of points, therefore you'll always be able to climb to a local maximum.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by vanderZwan on Wednesday October 17, 2018 @10:17AM (#18239253)
The problem I see is the word "near" in "climb to nearest peak". To quote the curse of dimensionality wiki page:

> The common theme of these problems is that when the dimensionality increases, the volume of the space increases so fast that the available data become sparse. This sparsity is problematic for any method that requires statistical significance.

https://en.wikipedia.org/wiki/Curse_of_dimensionality

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @10:36AM (#18239485)
Are you concerned that in a very high dimensional space you'd need an absurd number of points in order to produce a meaninful KDE, since otherwise the points will be too sparse? If so I think I'm with you... you'll probably just end up with a very large number of small, weak clusters.

Although I wonder if you can't account for that by simply increasing the kernel bandwidth. Perhaps not. Perhaps this is why mean-shift seems to be mostly used for computer vision and image processing, where (I guess?) the number of dimensions is low.

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @06:03AM (#18237633)
K-means is not an algorithm, it's a heuristic for an Np-hard problem.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @06:45AM (#18237794)
It is absolutely an algorithm in the sense of "a set of rules to be followed". I think you mean that it doesn't guarantee an optimal solution. That just means it's a heuristic algorithm, same as simulated annealing is a heuristic algorithm for solving optimisation problems.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @08:37AM (#18238346)
Nope. An algorithm has to be effective. You can find pathological cases for k-means such that it will never converge on anything useful. So if you set your termination case to be convergence it will never terminate and if you don't then it will never be effective.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by zaphar on Wednesday October 17, 2018 @09:00AM (#18238498)
I think you might be in the minority in this opinion. Many algorithms have pathological cases but are still considered algorithms
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @10:56AM (#18239667)
Minority? This is directly from Knuth.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by bstamour on Wednesday October 17, 2018 @04:36PM (#18243550)
Knuth defines effectiveness as: "... all of the operations to be performed in the algorithm must be sufficiently basic that they can in principle be done exactly and in a finite length of time by a man using paper and pencil."

K-means and other heuristic algorithms fit that description.

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by skykooler on Wednesday October 17, 2018 @09:23AM (#18238665)
BogoSort is an algorithm. Not a very good algorithm, but an algorithm nevertheless.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @11:06AM (#18239762)
No it absolutely is not.

The lack of fundamental computer science knowledge in this thread is alarming.

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @02:12PM (#18241847)
Negative 2 points on a post saying that a computational method that possibly never terminates is not an algorithm... Oh dear...
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by wnoise on Wednesday October 17, 2018 @04:31PM (#18243492)
It never terminates with probability 0.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @10:08AM (#18239138)
As far as I can tell you're only arguing against poor implementations of K-means. If you demand that the score strictly improves at each iteration then the algorithm must terminate.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @11:05AM (#18239751)
And how do you "demand that the score strictly improves"? It's an NP-hard problem.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Wednesday October 17, 2018 @11:42AM (#18240133)
K-means implementations generally terminate once there's an iteration where the score doesn't improve. This happens when there is convergence to a local minimum or - less likely - the state hops between two nearby local minima with the same score. But it will terminate on something, and most of the time that something will be pretty good.

I saw your mention of Knuth elsewhere, I looked it up and he demanded that

> An algorithm must always terminate after a finite number of steps ... a very finite number, a reasonable number

This is a pretty niche characterization and almost certainly not what the original post was asking for. However, I concur that there is no guarantee on how quickly K-means terminates or on how good the output will be,. But... if you're going to be that strict about it you would even have to rule out the Simplex Algorithm, which everyone I've ever spoken to thinks of as an algorithm.

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by billfruit on Wednesday October 17, 2018 @09:13AM (#18238588)
In that sense kmeans may be better referred to as a 'computational method' rather than an algorithm.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @11:43AM (#18240147)
Indeed.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by wnkrshm on Wednesday October 17, 2018 @06:35AM (#18237761)
Isn't a method that gives an approximate or best-fit estimate to a problem still an algorithm, if it terminates?
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @08:38AM (#18238352)
No. You can't prove that k-means does anything useful.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by alanbernstein on Wednesday October 17, 2018 @09:50AM (#18238906)
Is the definition of "algorithm" that you're using here useful?
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @10:58AM (#18239683)
It's one of the most fundamental concepts in computer science and underpins decades of research. You can decide if it's useful.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by mindcrime on Wednesday October 17, 2018 @12:36PM (#18240766)
This isn't a classroom, and your pedantry isn't adding anything useful to the conversation. We all understand these pedantic quibbles you're arguing about... and what the community is more or less collectively saying is "in this context, we don't care about the distinction between an 'algorithm' in the textbook sense, and a 'heuristic' in the textbook sense".
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by another-cuppa on Wednesday October 17, 2018 @02:24PM (#18242003)
Nah. Most of them don't understand the difference. If you did you wouldn't can it pedantry.

I personally don't find heuristics beautiful. That's why I commented.

Re:Ask HN: What's your favorite elegant/beautiful algorithm? by n4r9 on Thursday October 18, 2018 @06:36AM (#18247242)
To be fair, you haven't explained at all clearly why you don't think k-means adheres to Knuth's notion of an algorithm.

Your objection seems to be

> You can find pathological cases for k-means such that it will never converge on anything useful

As has been pointed out more than once, a good implementation of k-means is guaranteed to terminate in a finite time. And whatever you mean by "useful" doesn't seem to appear in Knuth's definition of an algorithm.