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")

Loading and examining the dataset

print(penguins_df.info())
n_rows = penguins_df.shape[0]
print(n_rows)
Hidden output

Dealing with null values and outliers

# Dropping missing values
print(penguins_df.isna().sum().sort_values())
no_missing = penguins_df.dropna()
print(no_missing.isna().sum())
Hidden output
# Identifying outliers
no_missing.boxplot()
plt.xticks(rotation=45)
plt.show()
# Removing outliers
import numpy as np
column_outliers = no_missing['flipper_length_mm'].values
iq_range = np.quantile(column_outliers, 0.75) - np.quantile(column_outliers, 0.25)
upper_threshold = np.quantile(column_outliers, 0.75) + 1.5 * iq_range
lower_threshold = np.quantile(column_outliers, 0.25) - 1.5 * iq_range
penguins_clean = no_missing[(no_missing['flipper_length_mm'] < upper_threshold) & (no_missing['flipper_length_mm'] > lower_threshold)]

penguins_clean.boxplot()
plt.xticks(rotation=45)
plt.show()

Preprocessing and dummy variables

penguin_dummies = pd.get_dummies(penguins_clean, drop_first=True)
print(penguin_dummies.info())

# Using Standard Scaler method to preprocess existing data
values = penguin_dummies.values
scale = StandardScaler()
penguins_preprocessed = scale.fit_transform(values)
Hidden output

Performing PCA

model = PCA()
model.fit(penguins_preprocessed)
variance_ratio = model.explained_variance_ratio_

plt.bar(range(model.n_components_), variance_ratio)
plt.axhline(y=0.1, color='r', linestyle='--')
plt.show()

n_components = (variance_ratio > 0.1).sum()
print(n_components)

# Running the PCA with 2 components
model = PCA(n_components=n_components)
penguins_PCA = model.fit_transform(penguins_preprocessed)

Detecting the optimal number of clusters for k-means