<jupyter_start><jupyter_text>KMeans Clustering<jupyter_code>import pandas as pd

df = pd.read_csv('https://github.com/gumdropsteve/datasets/raw/master/iris.csv')

df<jupyter_output><empty_output><jupyter_text>What we know<jupyter_code>import seaborn as sns

sns.pairplot(df, hue='species')<jupyter_output>/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)<jupyter_text>What the algo know<jupyter_code>import seaborn as sns

sns.pairplot(df)<jupyter_output><empty_output><jupyter_text>https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html<jupyter_code>from sklearn.cluster import KMeans
KMeans()
from sklearn.model_selection import train_test_split

X = df.drop(['species', 'target'], axis=1)
y = df.target

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.3)
km = KMeans()

km.fit(X_train)
km.labels_
km.predict(X_test)
km = KMeans()

km.fit(X)

km.labels_
from sklearn.metrics import accuracy_score

preds = km.labels_

accuracy_score(y, preds)<jupyter_output><empty_output><jupyter_text>Doing it with 3 labels...<jupyter_code>km = KMeans(n_clusters=3)

km.fit(X)

km.labels_
from sklearn.metrics import accuracy_score

preds = km.labels_

accuracy_score(y, preds)
y<jupyter_output><empty_output><jupyter_text>Elbow Plot to Help Determine Amount of Clusters if you dont know how many exist<jupyter_code>import matplotlib.pyplot as plt

inertia_ls = [] # save inertia values
for k in range(1, 11): # test different n_clusters between 1-10
    km = KMeans(n_clusters=k)
    km.fit(X) # apply KMeans
    inertia_ls.append(km.inertia_) # Append inertia to list

# Plot Elbow
plt.plot([i for i in range(1, 11)], inertia_ls, marker="o")
plt.title("Iris Elbow Plot")
plt.xlabel("Number of Clusters")
plt.ylabel("Inertia");<jupyter_output><empty_output><jupyter_text>Airlines<jupyter_code>import pandas as pd

df = pd.read_parquet('https://github.com/gumdropsteve/datasets/raw/master/airlines.parquet')

df
corr = df.corr()
corr.style.background_gradient(cmap='coolwarm')
X = df[['CRSDepTime', 'CRSArrTime', 'ActualElapsedTime', 'Distance']]
y = df.ArrDelayBinary

from sklearn.preprocessing import StandardScaler

ss = StandardScaler()
X_tf = ss.fit_transform(X)
km = KMeans(n_clusters=2)

km.fit(X)

preds = km.labels_

accuracy_score(y, preds)
km = KMeans(n_clusters=2)

km.fit(X_tf)

preds = km.labels_

accuracy_score(y, preds)
import matplotlib.pyplot as plt

inertia_ls = [] # save inertia values
for k in range(1, 11): # test different n_clusters between 1-10
    km = KMeans(n_clusters=k)
    km.fit(X) # apply KMeans
    inertia_ls.append(km.inertia_) # Append inertia to list

# Plot Elbow
plt.plot([i for i in range(1, 11)], inertia_ls, marker="o")
plt.title("Airlines Elbow Plot")
plt.xlabel("Number of Clusters")
plt.ylabel("Inertia");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1210400478182905, 'fraction_numerical': 0.012851165570830842, 'mean_word_length': 5.232774674115456, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 3, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '425534', 'license': 'MIT', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1271, 'n_words': 249, 'path': 'stack_4/day_14/01_kmeans.ipynb', 'repo_name': 'mpHarm88/learn_datascience', 'size': '3346'}