Professional Documents
Culture Documents
Contoh Code Klastering Alur Hirarkial
Contoh Code Klastering Alur Hirarkial
#delete var1
del df['Var_1']
#IMPORT LIBRARY
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.metrics.pairwise import cosine_similarity
#
scaler = StandardScaler()
scaled_data = scaler.fit_transform(df)
df = pd.DataFrame(scaled_data, columns= df.columns)
df
#Grafik
plt.figure(figsize=(15,10))
dendrogram(linkage(df, method="ward"), leaf_rotation=90, p=5, color_threshold=20,
leaf_font_size=10, truncate_mode='level')
plt.show()
#
agglo = AgglomerativeClustering(n_clusters = 3)
agglo.fit(df)
labels = agglo.labels_
df = pd.concat([df, pd.DataFrame({'cluster' : labels})], axis=1)
df.head(5)
#
for i in df :
grid = sns.FacetGrid(df, col='cluster')
grid.map(plt.hist, i)
#
#DECOMPOSISI PCA
dist = 1 - cosine_similarity(df)
pca = PCA(n_components = 2)
pca = pca.fit_transform(dist)
#
#VISUALISASI
x, y = pca[:,0], pca[:,1]
warna = {
0 : 'red',
1 : 'green',
2 : 'yellow'
}
label_pca = {
0 : 'cluster 1',
1 : 'cluster 2',
2 : 'cluster 3'
}
df = pd.DataFrame({'x' : x, 'y' : y, 'label' : labels})
groups = df.groupby('label')
fig, ax = plt.subplots(figsize=(15,10))
for name, group in groups :
ax.plot(group.x, group.y, marker='o', linestyle='', ms=5,
color=warna[name], label = label_pca[name], mec='none')
ax.set_aspect('auto')
ax.tick_params(axis='x', which = 'both', bottom = 'off', top = 'off',
labelbottom = 'off')
ax.tick_params(axis='y', which = 'both', bottom = 'off', top = 'off',
labelbottom = 'off')
ax.legend()
ax.set_title("Visualisasi Agglomerative Clustering")
plt.show()