<jupyter_start><jupyter_text>EDA-04: Unsupervised Learning - Clustering Bagian ke-02(C) <NAME> - 2020tau-data Indonesia ~ https://tau-data.id/eda-04/<jupyter_code># Run this cell ONLY if this notebook run from Google Colab
# Kalau dijalankan lokal (Anaconda/WinPython) maka silahkan install di terminal/command prompt 
# Lalu unduh secara manual file yang dibutuhkan dan letakkan di folder Python anda.
!pip install --upgrade umap-learn
!wget https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/tau_unsup.py
# Importing Modules untuk Notebook ini
import warnings; warnings.simplefilter('ignore')
import time, umap, numpy as np, tau_unsup as tau, matplotlib.pyplot as plt, pandas as pd, seaborn as sns
from matplotlib.colors import ListedColormap
from sklearn import cluster, datasets
from sklearn.metrics.pairwise import pairwise_distances_argmin
from sklearn.preprocessing import StandardScaler
from itertools import cycle, islice
from sklearn.metrics import silhouette_score as siluet
from sklearn.metrics.cluster import homogeneity_score as purity
from sklearn.metrics import normalized_mutual_info_score as NMI 

sns.set(style="ticks", color_codes=True)
random_state = 99<jupyter_output><empty_output><jupyter_text>Review EDA-03* Pendahuluan Unsupervised Learning* k-Means, k-Means++, MiniBatch k-Means* internal & External Evaluation* Parameter Tunning EDA-04* Hierarchical Clustering* Spectral Clustering* DBScan* Clustering Evaluation Revisited  Linkages Comparisons* single linkage is fast, and can perform well on non-globular data, but it performs poorly in the presence of noise.* average and complete linkage perform well on cleanly separated globular clusters, but have mixed results otherwise.* Ward is the most effective method for noisy data.* http://scikit-learn.org/stable/auto_examples/cluster/plot_linkage_comparison.htmlsphx-glr-auto-examples-cluster-plot-linkage-comparison-py<jupyter_code>tau.compare_linkages()<jupyter_output><empty_output><jupyter_text>Pros* No assumption of a particular number of clusters (i.e. k-means)* May correspond to meaningful taxonomies Cons* Once a decision is made to combine two clusters, it can’t be undone* Too slow for large data sets, O(𝑛2 log(𝑛))<jupyter_code># Kita akan menggunakan data yang sama dengan EDA-03
df = sns.load_dataset("iris")
X = df[['sepal_length','sepal_width','petal_length','petal_width']].values
C = df['species'].values
print(X.shape)
df.head()
# Hierarchical http://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html#sklearn.cluster.AgglomerativeClustering
hierarchical = cluster.AgglomerativeClustering(n_clusters=3, linkage='average', affinity = 'euclidean')
hierarchical.fit(X) # Lambat .... dan menggunakan banyak memori O(N^2 log(N))
C_h = hierarchical.labels_.astype(np.int)
C_h[:10]
# Dendogram Example
# http://seaborn.pydata.org/generated/seaborn.clustermap.html
g = sns.clustermap(X, method="single", metric="euclidean")
# Scatter Plot of the hierarchical clustering results
X2D = umap.UMAP(n_neighbors=5, min_dist=0.3, random_state=random_state).fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X2D[:,0], X2D[:,1], c=C_h)
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluasi Hierarchical Clustering?* Silhoutte Coefficient, Dunn index, or Davies–Bouldin index* Domain knowledge - interpretability* External Evaluation Read more here: https://www.ims.uni-stuttgart.de/document/team/schulte/theses/phd/algorithm.pdf<jupyter_code># Spectral : http://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html

spectral = cluster.SpectralClustering(n_clusters=3)
spectral.fit(X)
C_spec = spectral.labels_.astype(np.int)
sns.countplot(C_spec)
C_spec[:10]
fig, ax = plt.subplots()
ax.scatter(X2D[:,0], X2D[:,1], c=C_spec)
plt.show()
# DBSCAN http://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html
# tidak membutuhkan input parameter k!!!... sangat bermanfaat untuk clustering data yang besar
dbscan = cluster.DBSCAN(eps=0.8, min_samples=5, metric='euclidean')
dbscan.fit(X)
C_db = dbscan.labels_.astype(np.int)
sns.countplot(C_db)
C_db[:10]
# apa makna cluster label -1?
sum([1 for i in C_db if i==-1])
fig, ax = plt.subplots()
ax.scatter(X2D[:,0], X2D[:,1], c=C_db)
plt.show()
try:
    # Should work in Google Colab
    !wget https://raw.githubusercontent.com/christopherjenness/DBCV/master/DBCV/DBCV.py
except:
    pass # Download manually on windows

import dbcv
dbcv.DBCV(X, C_db)<jupyter_output><empty_output>