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()Check the column data types.
penguins_df.info()Check the for missing values.
penguins_df.describe()Check for any duplicate values.
penguins_df[penguins_df.duplicated].shapepenguins_df_with_dummy = pd.get_dummies(penguins_df)
penguins_df_with_dummy.head(5)Identify the optimal number of clusters by visualizing the value of inertia for specific values of n_cluster.
ks = range(1,10)
inertias = []
for k in ks:
model = KMeans(n_clusters=k, random_state=42)
model.fit(penguins_df_with_dummy)
inertias.append(model.inertia_)
plt.plot(ks,inertias,'-o')
plt.xlabel('Clusters')
plt.ylabel('intertia')
plt.xticks(ks)
plt.show()Create a Pipeline that to combine StandardScaler() and model training.
from sklearn.pipeline import Pipeline
scaler = StandardScaler()
kmeans = KMeans(n_clusters=3, random_state=42)
steps = [('scaler',scaler),
('kmeans',kmeans)]
pipeline = Pipeline(steps)
pipeline.fit(penguins_df_with_dummy)
clusters = pipeline.predict(penguins_df_with_dummy)Add a new column containing the cluster labels predicted by the model, and calculate the average culmen length, culmen depth, flipper length, and body mass for each cluster.
While we were able to cluster the species and identify their features, using K-Means clustering does not reveal the actual species. Researchers would still need to manually assign the labels.