<jupyter_start><jupyter_text>Clustering Models Analysis
> Learn about different clustering models.

- toc: true 
- badges: true
- comments: true
- categories: [clustering]
- image: images/clustering.jpg<jupyter_code># import statements
from sklearn.datasets import make_blobs
import numpy as np
import matplotlib.pyplot as plt
from kneed import KneeLocator
from sklearn.metrics import silhouette_score


# create blobs
data = make_blobs(n_samples=200, n_features=2, centers=3, cluster_std=1.6, random_state=50)
# create np array for data points
points = data[0]
# create scatter plot
plt.scatter(data[0][:,0], data[0][:,1], c=data[1], cmap='viridis')
plt.xlim(-15,15)
plt.ylim(-15,15)<jupyter_output><empty_output><jupyter_text>KMeans Clustering<jupyter_code># import KMeans
from sklearn.cluster import KMeans
# create kmeans object
kmeans = kmeans = KMeans(init="random",n_clusters=3,n_init=10,max_iter=300,random_state=42) #fit kmeans object to data
kmeans.fit(points)

# print location of clusters learned by kmeans object
print(kmeans.cluster_centers_)

# save new clusters for chart
kmeans.fit(points)
y_km = kmeans.fit_predict(points)
# A list holds the silhouette coefficients for each k
silhouette_coefficients = []

# Notice you start at 2 clusters for silhouette coefficient
for k in range(2, 11):
    kmeans = KMeans(n_clusters=k, **kmeans_kwargs)
    kmeans.fit(points)
    score = silhouette_score(points, kmeans.labels_)
    silhouette_coefficients.append(score)
plt.style.use("fivethirtyeight")
plt.plot(range(2, 11), silhouette_coefficients)
plt.xticks(range(2, 11))
plt.xlabel("Number of Clusters")
plt.ylabel("Silhouette Coefficient")
plt.show()
kmeans_kwargs = {
    "init": "random",
    "n_init": 10,
    "max_iter": 300,
    "random_state": 42,
}

# A list holds the SSE values for each k
sse = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters=k, **kmeans_kwargs)
    kmeans.fit(points)
    sse.append(kmeans.inertia_)
 plt.style.use("fivethirtyeight")
 plt.plot(range(1, 11), sse)
 plt.xticks(range(1, 11))
 plt.xlabel("Number of Clusters")
 plt.ylabel("SSE")
 plt.show()
kl = KneeLocator(
    range(1, 11), sse, curve="convex", direction="decreasing"
)

kl.elbow

plt.scatter(points[y_km ==0,0], points[y_km == 0,1], s=100, c='red')
plt.scatter(points[y_km ==1,0], points[y_km == 1,1], s=100, c='black')
plt.scatter(points[y_km ==2,0], points[y_km == 2,1], s=100, c='blue')
plt.scatter(points[y_km ==3,0], points[y_km == 3,1], s=100, c='cyan')<jupyter_output><empty_output><jupyter_text>Hierarchical Clustering<jupyter_code># import hierarchical clustering libraries
import scipy.cluster.hierarchy as sch
from sklearn.cluster import AgglomerativeClustering
# create dendrogram
dendrogram = sch.dendrogram(sch.linkage(points, method='ward'))
# create clusters
hc = AgglomerativeClustering(n_clusters=4, affinity = 'euclidean', linkage = 'ward')
# save clusters for chart
y_hc = hc.fit_predict(points)
plt.scatter(points[y_hc ==0,0], points[y_hc == 0,1], s=100, c='red')
plt.scatter(points[y_hc==1,0], points[y_hc == 1,1], s=100, c='black')
plt.scatter(points[y_hc ==2,0], points[y_hc == 2,1], s=100, c='blue')
plt.scatter(points[y_hc ==3,0], points[y_hc == 3,1], s=100, c='cyan')<jupyter_output><empty_output><jupyter_text>MeanShift Clustering<jupyter_code>from sklearn.cluster import MeanShift
from numpy import unique
from numpy import where
model = MeanShift()

# fit model and predict clusters
yhat = model.fit_predict(points)

# retrieve unique clusters
clusters = unique(yhat)
# create scatter plot for samples from each cluster
for cluster in clusters:
	# get row indexes for samples with this cluster
	row_ix = where(yhat == cluster)
	# create scatter of these samples
	plt.scatter(points[row_ix, 0], points[row_ix, 1])
# show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>DB-Scan Clustering<jupyter_code>from sklearn.cluster import DBSCAN
model = DBSCAN(eps=0.1, min_samples=10)
# fit model and predict clusters
yhat = model.fit_predict(points)
# retrieve unique clusters
clusters = unique(yhat)
# create scatter plot for samples from each cluster
for cluster in clusters:
	# get row indexes for samples with this cluster
	row_ix = where(yhat == cluster)
	# create scatter of these samples
	plt.scatter(points[row_ix, 0], points[row_ix, 1])
# show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>Affinity Propogation<jupyter_code>from sklearn.cluster import AffinityPropagation
model = AffinityPropagation(damping=0.6)
# fit model and predict clusters
yhat = model.fit_predict(points)
# retrieve unique clusters
clusters = unique(yhat)
# create scatter plot for samples from each cluster
for cluster in clusters:
	# get row indexes for samples with this cluster
	row_ix = where(yhat == cluster)
	# create scatter of these samples
	plt.scatter(points[row_ix, 0], points[row_ix, 1])
# show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>Aggloromerative Clustering<jupyter_code>from sklearn.cluster import AgglomerativeClustering
model = AgglomerativeClustering(n_clusters=3)
# fit model and predict clusters
yhat = model.fit_predict(points)
# retrieve unique clusters
clusters = unique(yhat)
# create scatter plot for samples from each cluster
for cluster in clusters:
	# get row indexes for samples with this cluster
	row_ix = where(yhat == cluster)
	# create scatter of these samples
	plt.scatter(points[row_ix, 0], points[row_ix, 1])
# show the plot
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12011630019989097, 'fraction_numerical': 0.023986916227512266, 'mean_word_length': 6.138780804150454, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '66874', 'license': 'Apache-2.0', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1943, 'n_words': 497, 'path': '_notebooks/2021-10-02-clustering-models.ipynb', 'repo_name': 'MaksadAnnamuradov/FastPages', 'size': '5503'}