# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/mohmaed7777/The-Ultimate-Halloween-Candy-Power-Ranking/blob/main/K%20means%20for%20Clustering.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="H3A8u2Us-l10" colab={"base_uri": "https://localhost:8080/"} outputId="d4a43fa0-f82c-406e-9f31-50c7bc930b82"
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns
sns.set()
# %matplotlib inline 
# %config InlineBackend.figure_formats=['retina']
# %pylab inline 
print('Setup Complete$$')

# + colab={"base_uri": "https://localhost:8080/", "height": 223} id="EgARAXnN-5eM" outputId="642b9425-b984-47e4-a2cd-0199cb322361"
df = pd.read_csv('/content/candy-data.csv')
df.head()

# + id="eSEMC5nX_SKl" colab={"base_uri": "https://localhost:8080/"} outputId="26bacd21-72f8-4640-827d-6738dda933b0"
df.info()

# + id="0CkQ5gOCqf7Z" colab={"base_uri": "https://localhost:8080/"} outputId="e849a091-ddc0-48c1-8c3d-2f3e8af2c9c8"
df.dtypes

# + id="svWeCJpOq5JI" colab={"base_uri": "https://localhost:8080/"} outputId="deb09818-1759-493a-a7ca-4cc4f4e18a09"
df.dtypes.value_counts()

# + id="HoNtKZmPrOzq" colab={"base_uri": "https://localhost:8080/"} outputId="be533057-b728-48a5-f613-c4a4d710ab20"
print(len(df.competitorname.unique()))

# + id="hEZ3bFqorfcA" colab={"base_uri": "https://localhost:8080/"} outputId="fe508e4b-3fe0-47dc-bcbf-1fec30581fca"
df.shape

# + id="k1Y-ZderLhXK"
df.set_index(df['competitorname'], inplace=True)

# + id="Csy7hxVcT63z"
df.drop(['competitorname'], axis=1, inplace=True)

# + id="dlC5R3bpT1UK" outputId="e4cf06cc-604d-4573-b2b2-4bcd27a3c3b0" colab={"base_uri": "https://localhost:8080/", "height": 254}
df.head()

# + id="WY2ZOw00T23N" outputId="5aef4e62-e5fb-4557-c5d4-1c077280f473" colab={"base_uri": "https://localhost:8080/"}
df.shape

# + id="BWaekkzDVDha" outputId="12c1ce29-f3cb-4002-b11a-4038b822697e" colab={"base_uri": "https://localhost:8080/"}
# Check the missing values : 
df.isnull().sum()

# + id="FQNEB9qZUEU-" outputId="59cb6895-3d47-4a60-f0fa-289481fb9281" colab={"base_uri": "https://localhost:8080/", "height": 315}
df.describe()

# + id="Z-3N_Xy0UZZb"
X = df
y = df['sugarpercent']

# + id="BuUnj91VU6S6" outputId="59c78986-f750-4591-ab7d-56c0c9101ae6" colab={"base_uri": "https://localhost:8080/"}
X.info()

# + [markdown] id="BxXJbiWPVSex"
# **Feature Scaling:-**

# + id="65vbnqSAU9tg"
from sklearn.preprocessing import MinMaxScaler

# + id="vyWOo14IVjww"
cols = X.columns

# + id="0WKmUqn_VpYu"
min_max = MinMaxScaler()

# + id="n3P-jhxSVu4G"
X = min_max.fit_transform(X)

# + id="SJeQP6xRV3YD"
X = pd.DataFrame(X, columns=[cols])

# + id="TGTLFsnGV8ym" outputId="d2eea56c-b6c9-4eda-c989-863bdbb8dbec" colab={"base_uri": "https://localhost:8080/", "height": 223}
X.head()

# + [markdown] id="i_FgmFX3WD6N"
# **K means Model With Two Clusters**

# + id="InBxF1tYV-Ke"
from sklearn.cluster import KMeans

# + id="8cpVVC-VWRio"
k_model = KMeans(n_clusters=2, random_state=0)

# + id="l3Hfqc2GWc6J" outputId="935039b3-3b83-4c86-df01-90e3a766ce9e" colab={"base_uri": "https://localhost:8080/"}
k_model.fit(X)

# + id="0t8yCHLyWfvX" outputId="b1fec1f8-fe87-47c9-a1ab-6d67173bd2e1" colab={"base_uri": "https://localhost:8080/"}
k_model.cluster_centers_

# + id="BnqPI6uzWo5u" outputId="7eb63acc-6e27-490a-b31a-7ac5bc66097a" colab={"base_uri": "https://localhost:8080/"}
k_model.inertia_

# + id="Ck8AOjbZW06B"


StarcoderdataJupyterScriptsDedupFiltered