Bug 16623 - k-means() fails if centers has only one center
Summary: k-means() fails if centers has only one center
Status: RESOLVED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Models (show other bugs)
Version: R-devel (trunk)
Hardware: ix86 (32-bit) Other
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-12-09 08:33 UTC by Sarthak Kukreti
Modified: 2015-12-10 17:20 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sarthak Kukreti 2015-12-09 08:33:59 UTC
AFAICS, the k-means method **should** with a single center (and I am able to run the following command):

(Using Iris dataset as an example)
> kmeans(rewires, centers = 1)

K-means clustering with 1 clusters of sizes 150

Cluster means:
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1     5.843333    3.057333        3.758    1.199333

Clustering vector:
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [66] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[131] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Within cluster sum of squares by cluster:
[1] 681.3706
 (between_SS / total_SS =   0.0 %)

...
However, when a single center is provided instead, kmeans fails with an error. Verified using the centers generated by above command.

> x<-kmeans(rewires, centers=1)
> x<-kmeans(newiris, centers=x$centers)
Error: number of cluster centres must lie between 1 and nrow(x)

The same error is not seen for nrows(centers) > 1.
Comment 1 Martin Maechler 2015-12-10 14:53:30 UTC
yes, that behaviour is wrong .... even though not encountered much in practice.

I will fix this for R-devel  (and R-patched), so this fix will be in the next release of R... almost surely not in 2015 anymore, as *today*  R 3.2.3  has been released.