<jupyter_start><jupyter_text>Clustering: k-means, agglomerative clustering and DBSCAN<jupyter_code># Import libraries

%matplotlib inline
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.cluster import DBSCAN
# Create the data points

X,y = make_moons(n_samples=200,noise=0.05,random_state=0)
plt.scatter(X[:,0],X[:,1])
plt.show()
# Define a figure with two subplots
f, (ax1, ax2) = plt.subplots(1,2,figsize=(8,3))

# Start with k-means
km = KMeans(n_clusters=2,random_state=0)
y_km = km.fit_predict(X)
ax1.scatter(X[y_km==0,0],X[y_km==0,1],c='lightblue',marker='o',s=40,label='cluster 1')
ax1.scatter(X[y_km==1,0],X[y_km==1,1],c='red',marker='s',s=40,label='cluster 2')
ax1.set_title('K-means clustering')

# Also perform agglomerative clustering
ac = AgglomerativeClustering(n_clusters=2,affinity='euclidean',linkage='complete')
y_ac = ac.fit_predict(X)
ax2.scatter(X[y_ac==0,0],X[y_ac==0,1],c='lightblue',marker='o',s=40,label='cluster 1')
ax2.scatter(X[y_ac==1,0],X[y_ac==1,1],c='red',marker='s',s=40,label='cluster 2')
ax2.set_title('Agglomerative clustering')

plt.legend()
plt.show()

# Both k-means and agglomerative clustering face challenges in separating the two cluster
# Let's try DBSCAN, which is density-based

db = DBSCAN(eps=0.2,min_samples=5,metric='euclidean')
y_db = db.fit_predict(X)

plt.scatter(X[y_db==0,0],X[y_db==0,1],c='lightblue',marker='o',s=40,label='cluster 1')
plt.scatter(X[y_db==1,0],X[y_db==1,1],c='red',marker='s',s=40,label='cluster 2')
plt.title('DBSCAN clustering')
plt.legend()
plt.show()
# DSBSCAN successfully detected the half-moon shapes.
# One of the strengths of DBSCAN is clustering data of arbitrary shapes<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17210846707249586, 'fraction_numerical': 0.03818483674598783, 'mean_word_length': 8.934065934065934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '437520', 'license': 'Apache-2.0', 'n_tokens_mistral': 778, 'n_tokens_neox': 728, 'n_words': 125, 'path': 'clustering_exampes.ipynb', 'repo_name': 'izavits/msc_datascience', 'size': '1807'}