<jupyter_start><jupyter_text>09. Cluster Analysis con k-Means - Book + Private Lessons [Here ↗](https://sotastica.com/reservar)- Subscribe to my [Blog ↗](https://blog.pythonassembly.com/)- Let's keep in touch on [LinkedIn ↗](www.linkedin.com/in/jsulopz) 😄  Load the Data > - Simply execute the following lines of code to load the data> - This dataset contains **statistics** (columns)> - About **Car Models** (rows)<jupyter_code>import seaborn as sns

df = sns.load_dataset(name='mpg', index_col='name')
df.sample(10)
dfclean=df.dropna()
dfclean
X=dfclean[['mpg','horsepower']]<jupyter_output><empty_output><jupyter_text>Data `preprocessing`> - Do you need to *transform* the data> - To get a **truthful insight** of the model?<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
import pandas as pd
vnorm = scaler.fit_transform(X)
X.columns
X.index
dfnorm = pd.DataFrame(vnorm, columns=X.columns, index=X.index)
dfnorm.head()<jupyter_output><empty_output><jupyter_text>`KMeans()` Model in Python  Build the Model > 1. **Necesity**: Build Model> 2. **Google**: How do you search for the solution?> 3. **Solution**: Find the `function()` that makes it happen<jupyter_code>from sklearn.cluster import KMeans
error=[]
error
model=KMeans(n_clusters=1)
model.fit(X=dfnorm)
inercia=model.score(X=dfnorm)
inercia
error.append(inercia)
error
model=KMeans(n_clusters=2)
model.fit(X=dfnorm)
inercia = model.score(X=dfnorm)
inercia
error.append(inercia)
error
error = []
for pepa in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:

    model=KMeans(n_clusters=pepa)

    model.fit(X=dfnorm)

    inercia = model.score(X=dfnorm)

    error.append(inercia)

    error
sns.scatterplot(y=error, x=list(range(1, 11)))
sns.lineplot(y=error, x=list(range(1, 11)));
import matplotlib.pyplot as plt
dfgroup = dfsel.groupby('cluster')
dfgroup.get_group(0)
dfgroup.get_group(1)
plt.scatter(x='mpg', y='horsepower', data=dfgroup.get_group(0))
plt.scatter(x='mpg', y='horsepower', data=dfgroup.get_group(1))
plt.scatter(x='mpg', y='horsepower', data=dfgroup.get_group(2))
for pepa in [0, 1, 2]:
    plt.scatter(x='mpg', y='horsepower', data=dfgroup.get_group(pepa))
error
df.mpg
df.mpg.hist();
df.mpg.sort_values()
df.mpg.quantile(q=[0.1, 0.9])
model=KMeans(n_clusters=3)<jupyter_output><empty_output><jupyter_text>Code Thinking> Which function computes the Model?> - `fit()`>> How could can you **import the function in Python**?<jupyter_code>X
model.fit(X)<jupyter_output><empty_output><jupyter_text>Separate Variables for the Model> Regarding their role:> 1. **Target Variable `y`**>> - [ ] What would you like **to predict**?>> Total number of accients? Or Alcohol?>> 2. **Explanatory Variable `X`**>> - [ ] Which variable will you use **to explain** the target?  Data Visualization to Analyize Patterns > - Visualize the 2 variables with a `scatterplot()`> - And decide *how many `clusters`* you'd like to calculate<jupyter_code>sns.scatterplot(x='mpg', y='horsepower', data=dfnorm)<jupyter_output><empty_output><jupyter_text>Finally `fit()` the Model  `predict()` the *cluster* for every row > - `model.` + `↹`<jupyter_code>pred=model.predict(X=dfnorm)
dfnorm
pred<jupyter_output><empty_output><jupyter_text>> - Create a `dfsel` DataFrame> - That contains the **columns you used for the model**<jupyter_code>dfsel=dfnorm.copy()<jupyter_output><empty_output><jupyter_text>> - Add a **new column**> - That **contains the `cluster` prediction** for every USA State<jupyter_code>dfsel['cluster'] = pred
pred
dfsel.sample(20)<jupyter_output><empty_output><jupyter_text>Model Visualization > - You may `hue=` the points with the `cluster` column<jupyter_code>dfsel.cluster
sns.scatterplot(x='mpg', y='horsepower', data=dfsel,hue=dfsel.cluster, palette='Set1');<jupyter_output><empty_output><jupyter_text>Model Interpretation<jupyter_code>%%HTML

<iframe width="560" height="315" src="https://www.youtube.com/embed/4b5d3muPQmA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><jupyter_output><empty_output><jupyter_text>> - Can you put a **`nickname` to each group**?> - Observe the `centroids` within `model.` + `↹`<jupyter_code>model.__dict__
dfcentroides = pd.DataFrame(model.cluster_centers_, columns=['mpg', 'horsepower'])
dfcentroides<jupyter_output><empty_output><jupyter_text>Model Visualization with Centroids> - I want to see the `centroid`> - with a **big `markers="X"`** in the plot<jupyter_code>sns.scatterplot(x='mpg', y='horsepower', data=dfsel,hue=dfsel.cluster, palette='Set1');
sns.scatterplot(x='mpg', y='horsepower', data=dfcentroides,hue=dfcentroides.index, palette='Set1', s=500);
a = dfnorm.sample()
a
model.predict(a)
model.__dict__<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15745568300312826, 'fraction_numerical': 0.01251303441084463, 'mean_word_length': 7.170357751277683, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 85, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '6250', 'license': 'MIT', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1778, 'n_words': 390, 'path': '#09. Cluster Analysis con k-Means/09practice.ipynb', 'repo_name': 'apoboldon/machine-learning-program', 'size': '4795'}