This repository showcases essential machine learning workflows in Python using the famous Palmer Penguins dataset. The focus is on data preprocessing, exploration, visualization, and classification of penguin species with a KNeighborsClassifier.
The aim is to demonstrate the basic steps of a data science project—from data exploration and cleaning to model training and evaluation, entirely in Python.
- Python 3.x
- numpy
- pandas
- seaborn
- scikit-learn
note that sklearn is decprecated, use scikit-learn instead
To install required packages:
pip install numpy pandas seaborn scikit-learn
The Palmer Penguins dataset provides physiological measurements and contextual information about three Antarctic penguin species: Adelie, Chinstrap, and Gentoo. The dataset can be loaded directly via Seaborn.
import numpy as np
import pandas as pd
import seaborn as sns
df = sns.load_dataset("penguins")
- Overview and handling of missing values (via
dropna) - Visualization: pairplot colored by
speciesusing Seaborn
sns.pairplot(data=df, hue="species")
- Remove non-numerical columns (
island,sex) - Use only numerical features
df = df.drop(['island', 'sex'], axis=1)
df = df.dropna()
- Target variable:
species - Features: bill length/depth, flipper length, body mass
from sklearn.model_selection import train_test_split
X = df.drop("species", axis=1)
y = df["species"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=119)
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(X_train, y_train)
- Predictions and accuracy
from sklearn.metrics import accuracy_score
predictions = clf.predict(X_test)
print(accuracy_score(y_test, predictions)) # about 0.88
The KNN model classifies penguin species in the test set with approximately 88% accuracy.
Created by Klaus Fischer.
- Palmer Penguins Dataset: https://allisonhorst.github.io/palmerpenguins/
- Seaborn Documentation: https://seaborn.pydata.org/
- scikit-learn Documentation: https://scikit-learn.org/