<jupyter_start><jupyter_text>unsupervised learning soal nomor 1 Nama: <NAME> Nim: 170411100090 Kelas: DSI UTM<jupyter_code>import pandas as pd
from pandas import read_csv
import numpy as np
import matplotlib.pyplot as plt

from sklearn import preprocessing
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_samples, silhouette_score
df = read_csv("../dataset.csv",index_col=0)
df.head()
df.info()
np.sum(df.isnull())<jupyter_output><empty_output><jupyter_text>data tidak ada yg null  melakukan praproses pada dataset<jupyter_code>minmax_form = preprocessing.MinMaxScaler(feature_range = (0,10))
normalized_data = minmax_form.fit_transform(df)

normalized_data
#dataLatih dengan algoritma K-means
#belumDiNormalisasi

sum_of_squared_distances = {}
range_n_clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for n_cluster in range_n_clusters:
    model = KMeans(n_clusters = n_cluster,
                  init = 'random',
                  n_init = 10,
                  random_state = 0)
    
    # proses training
    model.fit(df)
    
    sum_of_squared_distances[n_cluster] = model.inertia_
sum_of_squared_distances
#menggunakan chart elbow
plt.figure(figsize = (8,8))

plt.title("Elbow Method for Optimal K")
plt.xlabel("Number of k Cluster")
plt.ylabel("Sum of Within-Cluster Variance")
plt.xticks(range_n_clusters)

plt.plot(range_n_clusters, 
        sum_of_squared_distances.values(),
        '-o', color = "Blue")

plt.show()
#dataSetelahDiNormalisasi

sum_of_squared_distances = {}
range_n_clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for n_cluster in range_n_clusters:
    model = KMeans(n_clusters = n_cluster,
                  init = 'random',
                  n_init = 10,
                  random_state = 0)
    
    # proses training
    model.fit(normalized_data)
    
    sum_of_squared_distances[n_cluster] = model.inertia_
sum_of_squared_distances
#visualkan chart dengan elbow lagi
plt.figure(figsize = (8,6))

plt.title("Elbow Method for Optimal K")
plt.xlabel("Number of k Cluster")
plt.ylabel("Sum of Within-Cluster Variance")
plt.xticks(range_n_clusters)

plt.plot(range_n_clusters, 
        sum_of_squared_distances.values(),
        '-o', color = "Blue")

plt.show()<jupyter_output><empty_output><jupyter_text>selanjutnya di evaluasi dengan silhouette score<jupyter_code>#data sudah dinormalisasi
sil = {}
range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]

for n_cluster in range_n_clusters:
    clusterer = KMeans(n_clusters = n_cluster,
                      init = 'random',
                      n_init = 10,
                      random_state = 0)
    
    preds = clusterer.fit_predict(normalized_data)
    score = silhouette_score(df, preds, metric = "euclidean")
    
    sil[n_cluster] = score
for n_cluster, sil_score in sil.items():
    print(n_cluster, ":", sil_score)
#visualkan data lagi
plt.figure(figsize = (8,6))

plt.title("Choosing Optimal Cluster")
plt.xlabel("Number of k Cluster")
plt.ylabel("Silhouette")
plt.xticks(range_n_clusters)

plt.plot(range_n_clusters, 
        sil.values(),
        '-o', color = "Blue")

plt.show()
#data yang belom dinormalisasi
sil = {}
range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]

for n_cluster in range_n_clusters:
    clusterer = KMeans(n_clusters = n_cluster,
                      init = 'random',
                      n_init = 10,
                      random_state = 0)
    
    preds = clusterer.fit_predict(df)
    score = silhouette_score(df, preds, metric = "euclidean")
    
    sil[n_cluster] = score
for n_cluster, sil_score in sil.items():
    print(n_cluster, ":", sil_score)
plt.figure(figsize = (8,6))

plt.title("Choosing Optimal Cluster")
plt.xlabel("Number of k Cluster")
plt.ylabel("Silhouette")
plt.xticks(range_n_clusters)

plt.plot(range_n_clusters, 
        sil.values(),
        '-o', color = "Blue")

plt.show()<jupyter_output><empty_output><jupyter_text>tahap clustering setelah data melewati praproses<jupyter_code>kmins = KMeans(n_clusters = 5, 
              init = 'random',
              n_init = 10,
              random_state = 0)

kmins.fit(df)
y_kmins = kmins.predict(df)
kmins_df = pd.DataFrame(y_kmins, columns = ['cluster'])
kmins_df
df['segmen'] = y_kmins
df
df['segmen'].value_counts().plot(kind = 'bar')
df
df.to_csv (r'export_dataframe2.csv', index = False, header=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11461449942462601, 'fraction_numerical': 0.019332566168009206, 'mean_word_length': 3.531803962460897, 'pattern_counts': {'":': 2, '<': 18, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '509573', 'license': 'MIT', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1577, 'n_words': 338, 'path': 'Pertemuan 12 - Projek 3/Jawaban terkumpul/Mochamad Dani Hartanto/soal-1.ipynb', 'repo_name': 'Rakhid16/data-science-with-python', 'size': '4361'}