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.model_selection import train_test_split
# Loading and examining the dataset
penguins_df = pd.read_csv("penguins.csv")
penguins_df.head()df = pd.get_dummies(penguins_df, columns=["sex"])scaler = StandardScaler()
scaled_df = scaler.fit_transform(df)
scaled_dfclusters = range(2,9)
inertia = []
for k in clusters:
model = KMeans(n_clusters=k, random_state=20).fit(scaled_df)
inertia.append(model.inertia_)
plt.plot(clusters, inertia, marker="o")
plt.xlabel("Number Of Clusters")
plt.ylabel("Inertia")
plt.title("Elbow Method for Optimal Cluster")
plt.show()cluster = KMeans(n_clusters=4, random_state=20).fit(scaled_df)
penguins_df["label"] = cluster.labels_from sklearn.decomposition import PCA
pca = PCA(n_components=2).fit_transform(scaled_df)
plt.scatter(pca[:,0], pca[:,1], c=cluster.labels_, cmap="viridis", marker="o", alpha=0.8)
plt.title("PCA visualization of clusters")
plt.show()stat_penguins = penguins_df.drop("sex", axis=1).groupby("label").mean()
stat_penguins