Skip to content

Arctic Penguin Exploration: Unraveling Clusters in the Icy Domain with K-means clustering

Alt text source: @allison_horst https://github.com/allisonhorst/penguins

You have been asked to support a team of researchers who have been collecting data about penguins in Antartica!

Origin of this data : Data were collected and made available by Dr. Kristen Gorman and the Palmer Station, Antarctica LTER, a member of the Long Term Ecological Research Network.

The dataset consists of 5 columns.

  • culmen_length_mm: culmen length (mm)
  • culmen_depth_mm: culmen depth (mm)
  • flipper_length_mm: flipper length (mm)
  • body_mass_g: body mass (g)
  • sex: penguin sex

Unfortunately, they have not been able to record the species of penguin, but they know that there are three species that are native to the region: Adelie, Chinstrap, and Gentoo, so your task is to apply your data science skills to help them identify groups in the dataset!

# Import Required Packages
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# Loading and examining the dataset
penguins_df = pd.read_csv("data/penguins.csv")
'''
Notes:
- Reduce dimensionality
Steps:
- Clean the dataset -> penguins_clean
- Preprocess -> penguins_preprocessed
- Perform PCA -> penguins_PCA
- Employ kmeans clustering, perform elbow analysis -> n_clusters
- Fit kmeans cluster model -> kmeans
- Visualize
- Add the label to penguins_clean
- Create a statistical table -> stat_penguins
'''
# Cleaning
# Dealing with null values and outliers

penguins_df.dropna(inplace=True)

# ERROR SUBMITING PROJECT
# print(penguins_df["sex"].unique())
# penguins_df = penguins_df[penguins_df["sex"] != "."]

# Boxplot visualization

def plot_boxplots(df):

    fig, axs = plt.subplots(2, 2, figsize=(10, 8))

    columns = ['culmen_length_mm', 'culmen_depth_mm', 'flipper_length_mm', 'body_mass_g']

    for i in range(2):
        for j in range(2):
            column_name = columns[i * 2 + j]
            axs[i, j].boxplot(df[column_name])
            axs[i, j].set_title(column_name.replace('_', ' '))

    plt.tight_layout()
    plt.show()
    
plot_boxplots(penguins_df)

# Removing outliers

# Calculate the percentiles
q1 = penguins_df['flipper_length_mm'].quantile(0.25)
q3 = penguins_df['flipper_length_mm'].quantile(0.75)

# Calculate IQR
IQR = q3 - q1

# Set Multiplying factor
factor = 1.5

# Calculate the limits
lower_limit = q1 - (IQR * factor)
upper_limit = q3 + (IQR * factor)

# Filter
penguins_clean = penguins_df[(penguins_df['flipper_length_mm'] >= lower_limit) & (penguins_df['flipper_length_mm'] <= upper_limit)]

plot_boxplots(penguins_clean)
# Preprocessing
# Creating dummy variables for categorical feature (sex)

df = pd.get_dummies(penguins_clean).drop(columns="sex_.")
print(df.head())

# Scaling

scaler = StandardScaler()
penguins_preprocessed = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)

print(penguins_preprocessed.head())
# Performing PCA

model = PCA()
model.fit(penguins_preprocessed.values)

print(model.explained_variance_ratio_)
n_components = 2

pca = PCA(n_components=n_components)
penguins_PCA = pca.fit_transform(penguins_preprocessed.values)


# Performing Elbow analysis

inertia = []

for i in range(1,10):
    kmeans = KMeans(n_clusters=i, random_state=42)
    kmeans.fit(penguins_PCA)
    inertia.append(kmeans.inertia_)

    
# Plot the elbow

plt.plot(range(1, 10), inertia, marker='o')
plt.title('Elbow Analysis')
plt.xlabel('Number of Clusters')
plt.ylabel('Inertia')
plt.show()

n_clusters = 4
# k-means clustering algorithm
kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(penguins_PCA)

plt.scatter(penguins_PCA[:,0], penguins_PCA[:,1], c = kmeans.labels_)
plt.show()

# Creating final statistical DataFrame

penguins_clean["label"] = kmeans.labels_

numeric_columns = ['culmen_length_mm', 'culmen_depth_mm', 'flipper_length_mm', 'body_mass_g']

stat_penguins = penguins_clean.groupby('label')[numeric_columns].mean()

print(stat_penguins)