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()import numpy as np
from sklearn.manifold import TSNE# check data for missing values
penguins_df.isna().sum()# convert gender to dummy variable
df1 = pd.get_dummies(penguins_df['sex'])
penguins_df2 = penguins_df.drop(columns = 'sex')
penguins_df2 = pd.concat([penguins_df2, df1], axis=1)
penguins_df2.headfeatures_num = penguins_df2.drop(columns = ['FEMALE', 'MALE']).values
print(features_num)Scaled data
scaler = StandardScaler()
features_num_scaled = scaler.fit_transform(features_num)# use t-SNE to get estimate of number of clusters
# Create a TSNE instance: model
model = TSNE(learning_rate = 200)
# Apply fit_transform to samples: tsne_features
tsne_features = model.fit_transform(features_num_scaled)
# Select the 0th feature: xs
xs = tsne_features[:,0]
# Select the 1st feature: ys
ys = tsne_features[:,1]
# Scatter plot
plt.scatter(xs, ys)
plt.show()it seems there are at least 2 clusters based on numeric features only. Likely 3 clusters.
KMeans
# use kmeans inertia elbow plot to check how many clusters are there
ks = range(1, 6)
inertias = []
for k in ks:
model = KMeans (n_clusters = k)
model.fit(features_num_scaled)
inertias.append(model.inertia_)
# Plot ks vs inertias
plt.plot(ks, inertias, '-o')
plt.xlabel('number of clusters, k')
plt.ylabel('inertia')
plt.xticks(ks)
plt.show()Inertia seems to slow decreasing after 3 clusters. Let's go with 3.
The model