import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans

# generate 2-D dataset with four blobs.
from sklearn.datasets.samples_generator import make_blobs
X, y_true = make_blobs(n_samples = 500, centers = 4,
            cluster_std = 0.40, random_state = 0)
# Plot four blobs
plt.scatter(X[:, 0], X[:, 1], s = 50);
plt.show()

# k-mans with 4 clusters
kmeans = KMeans(n_clusters = 4)
# trains k-means with input data.
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c = y_kmeans, s = 50, cmap = 'viridis')
centers = kmeans.cluster_centers_

# plot 
plt.scatter(centers[:, 0], centers[:, 1], c = 'black', s = 200, alpha = 0.5);
plt.show()
7b5bb8bd9082dedcee68d25f4f595424d457cfb9
<mask token>

<mask token>
sns.set()
<mask token>
plt.scatter(X[:, 0], X[:, 1], s=50)
plt.show()
<mask token>
kmeans.fit(X)
<mask token>
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
<mask token>
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5)
plt.show()

<mask token>
sns.set()
<mask token>
X, y_true = make_blobs(n_samples=500, centers=4, cluster_std=0.4,
    random_state=0)
plt.scatter(X[:, 0], X[:, 1], s=50)
plt.show()
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5)
plt.show()

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import numpy as np
from sklearn.cluster import KMeans
from sklearn.datasets.samples_generator import make_blobs
X, y_true = make_blobs(n_samples=500, centers=4, cluster_std=0.4,
    random_state=0)
plt.scatter(X[:, 0], X[:, 1], s=50)
plt.show()
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5)
plt.show()

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans

# generate 2-D dataset with four blobs.
from sklearn.datasets.samples_generator import make_blobs
X, y_true = make_blobs(n_samples = 500, centers = 4,
            cluster_std = 0.40, random_state = 0)
# Plot four blobs
plt.scatter(X[:, 0], X[:, 1], s = 50);
plt.show()

# k-mans with 4 clusters
kmeans = KMeans(n_clusters = 4)
# trains k-means with input data.
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c = y_kmeans, s = 50, cmap = 'viridis')
centers = kmeans.cluster_centers_

# plot 
plt.scatter(centers[:, 0], centers[:, 1], c = 'black', s = 200, alpha = 0.5);
plt.show()