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
seed = 10

# Loading and examining the dataset
penguins_df = pd.read_csv("penguins.csv")
penguins_df.head()
# preprocessing
df = pd.get_dummies(penguins_df)
df.head()
# scaling numeric columns
numeric_cols = ['culmen_length_mm', 'culmen_depth_mm', 'flipper_length_mm', 'body_mass_g']

scaler = StandardScaler()

df[numeric_cols] = scaler.fit_transform(df[numeric_cols])

df.head()
# determine optimal number of clusters
inertia = []

for k in range(1,10):
    kmeans = KMeans(n_clusters=k, random_state=seed)
    kmeans.fit(df)
    inertia.append(kmeans.inertia_)

plt.plot(range(1,10), inertia, marker='x')
plt.title('Elbow Analysis')
plt.ylabel('Inertia')
plt.xlabel('Number of Clusters')
plt.show()

After the 3rd cluster, the inertia started to decrease more slowly so three(3) is the optimum number of clusters

# fitting the model
kmeans = KMeans(n_clusters=3, random_state=seed)
kmeans.fit(df)

df['label'] = kmeans.labels_

# visualise clusters
plt.figure(figsize=(15, 10))
for index, col in enumerate(df.columns[:-3]):
    plt.subplot(2, 2, index + 1)
    plt.scatter(df['label'], df[col], c=kmeans.labels_)
    plt.title(f'clusters in {col}')

plt.show()
# creating the submission dataframe
stat_penguins = penguins_df[numeric_cols]
stat_penguins['label'] = kmeans.labels_

stat_penguins = stat_penguins.groupby(['label'])[numeric_cols].mean()
stat_penguins