<jupyter_start><jupyter_text>Cluster AnalysisIs a subset of unsupervised learning methods  Introduction to Unsupervised Learning* Unsupervised Learning is a type of Machine learning to draw inferences from unlabelled datasets.* Model tries to find relationship between data.* Most common unsupervised learning method is clustering which is used for exploratory data analysis to find hidden patterns or grouping in data  Clustering* A learning technique to group a set of objects in such a way that objects of same group are more similar to each other than from objects of other group.* Applications of clustering are as follows  - Automatically organizing the data  - Labeling data  - Understanding hidden structure of data  - News Cloustering for grouping similar news together  - Customer Segmentation  - Suggest social groups<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import make_blobs
X,y = make_blobs(n_features=2, n_samples=1000, centers=3, cluster_std=1, random_state=3)
plt.scatter(X[:,0], X[:,1], s=5, alpha=.5)<jupyter_output><empty_output><jupyter_text>Distance or Similarity Function* Data belonging to same cluster are similar & data belonging to different cluster are different. * We need mechanisms to measure similarity & differences between data. * This can be achieved using any of the below techniques.<jupyter_code>from sklearn.metrics.pairwise import euclidean_distances,cosine_distances,manhattan_distances
X = [[0, 1], [1, 1]]
# X = np.random.normal(0,1,(100,2))
euclidean_distances(X, X)
euclidean_distances(X, [[0,0]])
cosine_distances(X,X)
manhattan_distances(X,X)<jupyter_output><empty_output><jupyter_text>4. Clustering as an Optimization Problem* Maximize inter-cluster distances* Minimize intra-cluster distances   Partitioning Method KMeans* Minimizing creteria : within-cluster-sum-of-squares. KMeans Algorithm1. Initialize k centroids.2. Assign each data to the nearest centroid, these step will create clusters.3. Recalculate centroid - which is mean of all data belonging to same cluster.4. Repeat steps 2 & 3, till there is no data to reassign a different centroid.![](https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/K-means_convergence.gif/617px-K-means_convergence.gif)<jupyter_code>from sklearn.datasets import make_blobs, make_moons
X,y = make_blobs(n_features=2, n_samples=1000, cluster_std=.5)
plt.scatter(X[:,0], X[:,1],s=10)
from sklearn.cluster import KMeans, MeanShift
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
plt.scatter(X[:,0], X[:,1],s=10, c=kmeans.predict(X))
X, y = make_moons(n_samples=1000, noise=.09)
plt.scatter(X[:,0], X[:,1],s=10)
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
plt.scatter(X[:,0], X[:,1],s=10, c=kmeans.predict(X))
kmeans = KMeans(n_clusters=4)
centers = [[1, 1], [-.75, -1], [1, -1], [-3, 2]]
X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)
plt.scatter(X[:,0], X[:,1],s=10)
kmeans = KMeans(n_clusters=4)<jupyter_output><empty_output><jupyter_text>Mean shift<jupyter_code>ms = MeanShift()
kmeans.fit(X)
ms.fit(X)
plt.scatter(X[:,0], X[:,1],s=10, c=ms.predict(X))
plt.scatter(X[:,0], X[:,1],s=10, c=kmeans.predict(X))<jupyter_output><empty_output><jupyter_text>More complex data<jupyter_code>X, y = make_moons(n_samples=1000, noise=.05)
plt.scatter(X[:,0], X[:,1],s=10)<jupyter_output><empty_output><jupyter_text>Agglomerative Clustering<jupyter_code>from sklearn.cluster import AgglomerativeClustering
agc = AgglomerativeClustering(linkage='single')
agc.fit(X)
plt.scatter(X[:,0], X[:,1],s=10,c=agc.labels_)<jupyter_output><empty_output><jupyter_text>DBSCAN<jupyter_code>centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4,
                            random_state=0)
plt.scatter(X[:,0], X[:,1],s=10)
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X)

db = DBSCAN(eps=0.3, min_samples=10).fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_
plt.scatter(X[:,0], X[:,1],s=10,c=labels)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup