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")
#Check for NA
penguins_df.isna().sum()
#Lenght of df with NA
len(penguins_df)
#Drop NA
penguins_df = penguins_df.dropna()
#Lenght of df without NA
len(penguins_df)
#Check for outliers
import seaborn as sns
sns.boxplot(x=penguins_df['culmen_length_mm'])
sns.boxplot(x=penguins_df['culmen_depth_mm'])
sns.boxplot(x=penguins_df['flipper_length_mm'])
#We got outliers in this variable
sns.boxplot(x=penguins_df['body_mass_g'])
#filter outliers in flipper_length_mm
#First calculate Q1, Q3 and IQR
Q1 = penguins_df['flipper_length_mm'].quantile(0.25)
Q3 = penguins_df['flipper_length_mm'].quantile(0.75)
IQR = Q3 - Q1
#Then calculate bellow and above
Bellow = Q1 - 1.5*IQR
Above = Q3 + 1.5*IQR
#Then filter penguins_df by this threshold
penguins_clean = penguins_df[penguins_df['flipper_length_mm'] > Bellow]
penguins_clean = penguins_clean[penguins_clean['flipper_length_mm'] < Above]
#Check the lenght of df without outliers
len(penguins_clean)
#chek again the boxplot of flipper_length_mm variable
sns.boxplot(x=penguins_clean['flipper_length_mm'])
#No putliers anymore
#Create dummies from sex
penguins_preprocessed = pd.get_dummies(penguins_clean)