Skip to content
Project: Clustering Antarctic Penguin Species
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.
Column | Description |
---|---|
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 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()
#investigate dataset
len(penguins_df)
penguins_df.isnull().sum()
#visualize
import numpy as np
import seaborn as sns
sns.pairplot(penguins_df, hue= "sex")
plt.show()
plt.figure(figsize= (10,6))
sns.boxplot(data= penguins_df, x= "culmen_depth_mm", y= "body_mass_g")
plt.show()
#preprocess data
#cleaning data
penguins_df.isnull().sum().sort_values()
#Scale the data
from sklearn.preprocessing import StandardScaler
data= penguins_df.drop("sex",axis=1)
scaled_penguins_df= StandardScaler().fit_transform(data)
#Create and fit model
model= KMeans(n_clusters= 3, random_state= 15)
model.fit(scaled_penguins_df)
#predict using the model
clusters= model.predict(scaled_penguins_df)
print(clusters)
#measuring clustering quality
model.inertia_
#create the clusters column in the data
data["clusters"]= pd.DataFrame(clusters)
data.head()
culmen_length= data.groupby("clusters")["culmen_length_mm"].mean()
culmen_depth= data.groupby("clusters")["culmen_depth_mm"].mean()
flipper_length= data.groupby("clusters")["flipper_length_mm"].mean()
body_mass= data.groupby("clusters")["body_mass_g"].mean()