<jupyter_start><jupyter_text>Unspervised Learning - Menemukan pola dari suatu data- Mengkompresi data (dimensional reduction<jupyter_code>from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
Y = iris.target
print(iris.DESCR)
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('iris.csv')
df.head()
X = df[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
y = df['species']
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
model.fit(X)
labels = model.predict(X)
labels
plt.scatter(df['sepal_length'], df['petal_length'], c=labels)<jupyter_output><empty_output><jupyter_text>Evaluasi Hasil Cluster<jupyter_code>evaluasi = pd.DataFrame({'labels': labels, 'species': df['species']})
evaluasi.head()
ct = pd.crosstab(evaluasi['labels'], evaluasi['species'])
ct
ks = range(1, 6)
inertias = []

for k in ks:
    # Create a KMeans instance with k clusters: model
    model = KMeans(n_clusters=k)
    
    # Fit model to samples
    model.fit(X)
    
    # Append the inertia to the list of inertias
    inertias.append(model.inertia_)
    
# Plot ks vs inertias
plt.plot(ks, inertias, '-o')
plt.xlabel('number of clusters, k')
plt.ylabel('inertia')
plt.xticks(ks)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13385826771653545, 'fraction_numerical': 0.002362204724409449, 'mean_word_length': 5.551546391752577, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '87685', 'license': 'MIT', 'n_tokens_mistral': 499, 'n_tokens_neox': 481, 'n_words': 110, 'path': 'Bagian 8 - Unsupervised Learning/1. K-Means Clustering.ipynb', 'repo_name': 'RadenMahar/DATA-SCIENCE', 'size': '1270'}