Author - Mohit Rathore mrmohitrathoremr@gmail.com - markroxor.in
Licensed under The MIT License - https://opensource.org/licenses/MIT

In [1]:
import numpy as np

from fromscratchtoml.toolbox import binary_visualize
from fromscratchtoml.cluster import KMeans as KMeans

from fromscratchtoml.toolbox.random import Distribution
%matplotlib inline

KMeans unsupervised clustering algorithm.

In [2]:
X1 = Distribution.linear(pts=500, covr=[[1.2, -1],[-1, 1]], mean=[0, 0])
X2 = Distribution.linear(pts=500, covr=[[1.2, -1],[-1, 1]], mean=[-1, -2])
X3 = Distribution.linear(pts=500, covr=[[1.2, -1],[-1, 1]], mean=[6, -3])

X = np.vstack([X1, X2, X3])
In [3]:
y_pred = KMeans(n_clusters=3).fit_predict(X)

binary_visualize(X, y_pred)
In [ ]:
 
In [4]:
X1 = Distribution.linear(pts=500, covr=[[0, -1],[-1, 0]], mean=[0, 0])
X2 = Distribution.linear(pts=500, covr=[[0, -1],[-1, 0]], mean=[-0, -3])
X3 = Distribution.linear(pts=500, covr=[[0, -1],[-1, 0]], mean=[6, -3])

X = np.vstack([X1, X2, X3])
/home/markroxor/.local/lib/python2.7/site-packages/fromscratchtoml-0.0.2-py2.7.egg/fromscratchtoml/toolbox/random.py:50: RuntimeWarning: covariance is not positive-semidefinite.
  return np.random.multivariate_normal(mean, covr, pts)
In [5]:
y_pred = KMeans(n_clusters=3).fit_predict(X)

binary_visualize(X, y_pred)
In [ ]:
 
In [6]:
X1 = Distribution.linear(pts=500, covr=[[0, -1],[-1, 0]], mean=[0, 0])
X2 = Distribution.linear(pts=500, covr=[[0, -1],[-1, 0]], mean=[0, -6])
X3 = Distribution.linear(pts=500, covr=[[0, -1],[-1, 0]], mean=[6, -3])

X = np.vstack([X1, X2, X3])
In [7]:
km = KMeans(n_clusters=3)
km.fit(X)
Out[7]:
KMeans({'max_iter': 500, 'n_clusters': 3})
In [8]:
binary_visualize(X, km.labels)
In [9]:
km = KMeans(n_clusters=100)
km.fit(X)
Out[9]:
KMeans({'max_iter': 500, 'n_clusters': 100})
In [10]:
binary_visualize(X, km.labels)
In [ ]:
 
In [11]:
km = KMeans(n_clusters=10)
km.fit(X)
Out[11]:
KMeans({'max_iter': 500, 'n_clusters': 10})
In [12]:
binary_visualize(X, km.labels)
In [ ]: