## SciPy K-Means

SciPy K-Means : Package scipy.cluster.vp provides kmeans() function to perform k-means on a set of observation vectors forming k clusters. In this tutorial, we shall learn the syntax and the usage of kmeans() function with SciPy K-Means Examples.

### Syntax

`centroids,distortion = scipy.cluster.vq.kmeans(obs, k_or_guess, iter=20, thresh=1e-05, check_finite=True)`
Try Online

Values provided for the optional arguments are default values.

### SciPy K-Means Example

In this example, we shall generate a set of random 2-D points, centered around 3 centroids.

```# import numpy
from numpy import vstack,array
from numpy.random import rand

# matplotlib
import matplotlib.pyplot as plt

# scipy
from scipy.cluster.vq import kmeans,vq,whiten

data = vstack(((rand(20,2)+1),(rand(20,2)+3),(rand(20,2)+4.5)))

plt.plot(data[:,0],data[:,1],'go')
plt.show()```
Try Online
```# whiten the features
data = whiten(data)

# find 3 clusters in the data
centroids,distortion = kmeans(data,3)

print('centroids  : ',centroids)
print('distortion :',distortion)
plt.plot(data[:,0],data[:,1],'go',centroids[:,0],centroids[:,1],'bs')
plt.show()```
Try Online
```centroids  :  [[ 1.42125469  1.58213817]
[ 3.55399219  3.53655637]
[ 4.91171555  5.02202473]]
distortion : 0.35623898893```