Skip to content
Project: Clustering Antarctic Penguin Species
Arctic Penguin Exploration: Unraveling Clusters in the Icy Domain with K-means clustering
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")
penguins_df.head()#Identifying Null values in the dataset
print(penguins_df.isna().sum().sort_values())
penguins_df.boxplot()
plt.show()#Removing the null values
penguins_clean = penguins_df.dropna()
#Removing the outliers
penguins_clean = penguins_clean[(penguins_clean["flipper_length_mm"] < 1000) & (penguins_clean["flipper_length_mm"] > 0)]
#Checking for null values and ourliers
print(penguins_clean.isna().sum().sort_values())
penguins_clean.boxplot()
plt.show()#Converting the Sex dataset from Categorical to Numerical data
penguins_encoded = pd.get_dummies(penguins_clean['sex'], drop_first=True)
penguins_combined = pd.concat([penguins_clean.drop(columns=['sex']), penguins_encoded], axis=1)
penguins_combined.head()
print(penguins_combined.dtypes)# PREPROCESSING THE DATA
scaler = StandardScaler()
penguins_preprocessed = scaler.fit_transform(penguins_combined)# PERFORMING PCA
pca = PCA()
pca.fit(penguins_preprocessed)
# Calculating the explained variance ratio
explained_variance_ratio = pca.explained_variance_ratio_
#Detecting the number of components with more than 10% explained variance ratio and storing the calue in n_components
n_components = (explained_variance_ratio > 0.1).sum()
#Running PCA again, optimally with specified n_components
pca_optimal = PCA(n_components = n_components)
pca_optimal.fit(penguins_preprocessed)
penguins_PCA = pca_optimal.transform(penguins_preprocessed)
# DETECTING THE OPTIMAL NUMBER OF CLUSTERS
# Empty list to store the Inertia
Inertia = []
for i in range(1, 10):
kmeans = KMeans(n_clusters = i, random_state = 42).fit(penguins_PCA)
Inertia.append(kmeans.inertia_)
X = range(1, 10)
y = Inertia
plt.plot(X, y)
plt.xlabel("n_clusters")
plt.ylabel("Inertia")
plt.show()# Saving the optimal number of clusters
n_clusters = 4
# Rerunning the KMeans algorithm for the optimal number of clusters
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 A FINAL STATISTICAL DATAFRAME FOR EACH CLUSTER
penguins_clean['label'] = kmeans.labels_
# Creating a list containing the names of the numeric columns of penguins_clean DataFrame
numeric_columns = penguins_clean.select_dtypes(include=['number']).columns.tolist()
stat_penguins = penguins_clean.groupby('label')[numeric_columns].mean()
print(stat_penguins)