Skip to content

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! The data is available in csv-Format as penguins.csv

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.

ColumnDescription
culmen_length_mmculmen length (mm)
culmen_depth_mmculmen depth (mm)
flipper_length_mmflipper length (mm)
body_mass_gbody mass (g)
sexpenguin sex

Unfortunately, they have not been able to record the species of penguin, but they know that there are at least three species that are native to the region: Adelie, Chinstrap, and Gentoo. 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.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# Loading and examining the dataset
penguins_df = pd.read_csv("penguins.csv")
penguins_df.head()

# Create dummy variable for categorical feature then drop the original column
penguin_df = pd.get_dummies(penguins_df, drop_first=True, dtype=int)

# Scaling the dataset for clustering
scaler = StandardScaler()
X = scaler.fit_transform(penguin_df)
penguins_preprocessed = pd.DataFrame(data=X, columns=penguins_df.columns)
penguins_preprocessed.head(5)

# Detect the optimal number of clusters to use for K-means clustering
inertia = []
for k in range(1, 10):  # Elbow Analysis
    kmeans = KMeans(n_clusters=k, random_state=42)
    kmeans.fit(penguins_preprocessed)
    inertia.append(kmeans.inertia_)

# Visualize the list of inertia values to determine the optimal no. of clusters
plt.plot(range(1, 10), inertia, '-o')
plt.xlabel('Number of clusters, k')
plt.ylabel('Inertia')
plt.xticks(range(1, 10))
plt.title('Elbow Method')

# Optional, highlight optimal k by adding a marker and annotation on the existing elbow plot
optimal_k = 4
plt.plot(optimal_k, inertia[optimal_k - 1], 'ro')  # Red circle marker
plt.annotate(f'Optimal k = {optimal_k}',
             xy=(optimal_k, inertia[optimal_k - 1]),
             xytext=(optimal_k + 0.5, inertia[optimal_k - 1] + 100),
             arrowprops=dict(facecolor='black', shrink=0.05),
             fontsize=10,
             color='black')

plt.grid(True)
plt.show()

# Run the k-means clustering algorithm
kmeans = KMeans(n_clusters=optimal_k, random_state=42)
kmeans.fit(penguins_preprocessed)
penguins_df['label'] = kmeans.labels_

# Visualize the k-means clustering using scatterplot
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_)
plt.xlabel('Cluster')
plt.ylabel('culmen_length_mm')
plt.title(f'K-means Clustering (K={optimal_k})')
plt.show()

# Create the final "stat_penguins" DataFrame
numeric_columns = [
    col for col in penguins_df.select_dtypes(include='number').columns
    if penguins_df[col].nunique() > 2
]

penguins_df['labels'] = kmeans.labels_
stat_penguins = pd.DataFrame(penguins_df.groupby('labels')
                             [numeric_columns].mean())
stat_penguins