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
from sklearn.pipeline import make_pipeline
# Loading and explorong variables from the dataset
penguins_df = pd.read_csv("penguins.csv")
penguins_df.head()# Assing standard scaler process
scalar = StandardScaler()
# Assing cluster method
kmeans = KMeans(n_clusters=3)
# Make pipeline
pipeline = make_pipeline(scalar,kmeans)
# DF to 2D array
penguins_np=penguins_df.drop('sex',axis=1).values
# Fit model with the data
pipeline.fit(penguins_np)
# Predict clustesrs
labels = pipeline.predict(penguins_np)
# Accessing to the clustering step in the pipeline to get the cluster centers
cluster_centers = pipeline.named_steps['kmeans'].cluster_centers_# Plot clusters from kmeans
plt.scatter(penguins_df['culmen_length_mm'],penguins_df['culmen_depth_mm'],c=labels)
plt.title('Visual clustering check')
plt.xlabel('Culmen length in mm')
plt.ylabel('Culmen depth in mm')
plt.show()penguins_df['cluster']=labels # Assigning cluster to original DF
# Group data by cluster and measure the average form numeric variables
stat_penguins=penguins_df.groupby('cluster')[['culmen_length_mm','culmen_depth_mm','flipper_length_mm','body_mass_g']].mean()
stat_penguins