In [25]:
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
In [10]:
X, y = make_blobs(n_samples=1000, centers=3, n_features=2)
df = pd.DataFrame(X)
df = df.rename(columns={0: 'x1', 1: 'x2'})
df['y'] = y
df.head()
Out[10]:
In [31]:
k_means = KMeans(init='k-means++', n_clusters=3, n_init=10)
k_means.fit(df[['x1', 'x2']])
df['y_pred'] = k_means.labels_
k_means.cluster_centers_
Out[31]:
In [32]:
sns.pairplot(df, hue='y_pred', vars=('x1', 'x2'), diag_kind="kde", plot_kws=dict(alpha=0.1, edgecolor=None))
Out[32]: