Monday, August 24, 2026

Mushroom Classification with Machine Learning

Screenshot 1
Screenshot 2
Chart 2
Download 9
Taxa
Mushroom Classification with Machine Learning

Demystifying Mushroom Classification with Machine Learning & Unsupervised Taxa Discovery

Can data science determine whether a mushroom is edible or deadly? Using the iconic UCI Mushroom Dataset containing 8,124 samples across 23 features, this exploratory Google Colab notebook explores data visualization, morphological clustering, and random forest classification to accurately identify fungal traits.

#DataScience #MachineLearning #Python #Clustering

1. Environment Setup & Data Loading

First, we fetch the dataset directly from Kaggle using kagglehub and load it into a Pandas DataFrame.

import os import pandas as pd import kagglehub # Download dataset path = kagglehub.dataset_download("uciml/mushroom-classification") df = pd.read_csv(os.path.join(path, "mushrooms.csv")) print(f"Dataset Shape: {df.shape[0]} rows, {df.shape[1]} columns")
Dataset Summary: 8,124 rows, 23 categorical features including cap shape, odor, gill color, stalk root, ring type, spore print color, and habitat.

2. Visualizing Key Morphological Features

Understanding which visual traits signal toxicity is crucial. Below, we compare key traits such as Odor, Gill Color, Ring Type, and Spore Print Color between Edible (e) and Poisonous (p) mushrooms.

import seaborn as sns import matplotlib.pyplot as plt df['class'] = df['class'].map({'e': 'Edible', 'p': 'Poisonous'}) features_to_plot = ['odor', 'gill-color', 'ring-type', 'spore-print-color'] fig, axes = plt.subplots(2, 2, figsize=(14, 10)) for idx, feature in enumerate(features_to_plot): sns.countplot(data=df, x=feature, hue='class', ax=axes.flatten()[idx], palette={'Edible': '#2ecc71', 'Poisonous': '#e74c3c'}) plt.tight_layout() plt.show()

Key Insight: Odor is a distinct indicator. For instance, mushrooms with an almond or anise odor (a, l) are predominantly edible, while foul odors (f) indicate poisonous specimens.

3. Unsupervised Taxa Discovery & Supervised Classification

We extract critical taxonomic features, apply One-Hot Encoding, and group the mushrooms into 5 morphive taxa clusters using K-Means Clustering. Additionally, we train a RandomForestClassifier to evaluate taxonomic family predictability based on spore print colors.

from sklearn.preprocessing import OneHotEncoder from sklearn.cluster import KMeans from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split taxa_features = ['cap-shape', 'cap-surface', 'gill-attachment', 'gill-color', 'stalk-shape', 'stalk-root', 'ring-type', 'spore-print-color', 'habitat'] encoder = OneHotEncoder(sparse_output=False) X_encoded = encoder.fit_transform(df[taxa_features]) # K-Means Clustering kmeans = KMeans(n_clusters=5, random_state=42, n_init=10) df['Taxonomic_Cluster'] = kmeans.fit_predict(X_encoded)

Classification Performance

The Random Forest model achieves 100% Precision, Recall, and F1-Score across all mapped spore families on the test set:

Estimated Taxa Family Precision Recall F1-Score Support
Agaricaceae (Black Spored)1.001.001.00373
Agaricaceae (Brown Spored)1.001.001.00404
Amanitaceae / Lepiotaceae (White Spored)1.001.001.00452
Bolbitiaceae (Chocolate Spored)1.001.001.00338
Coprinaceae (Buff Spored)1.001.001.008
Cortinariaceae (Orange Spored)1.001.001.009
Entolomataceae (Purple Spored)1.001.001.0014
Russulaceae (Yellow Spored)1.001.001.0013
Strophariaceae (Green Spored)1.001.001.0014

4. Visualizing Clusters: PCA vs t-SNE Projections

To inspect cluster boundaries in 2D space, linear dimensional reduction (PCA) and non-linear manifold learning (t-SNE) are applied.

from sklearn.decomposition import PCA from sklearn.manifold import TSNE # PCA (Linear) pca = PCA(n_components=2, random_state=42) X_pca = pca.fit_transform(X_encoded) # t-SNE (Non-Linear) tsne = TSNE(n_components=2, perplexity=35, random_state=42) X_tsne = tsne.fit_transform(X_encoded)

Takeaway: PCA retains 33.7% of total variance in 2D space and separates general linear groupings, whereas t-SNE provides clear, distinct clusters that perfectly isolate poisonous species from edible ones within sub-clusters.

mushroom_classification_blog_post.html Displaying mushroom_classification_blog_post.html.

No comments:

Meet the Authors
Zacharia Nyambu’s blog features multiple contributors with clear activity status.
Active ✔
πŸ§‘‍πŸ’»
Zacharia Nyambu
Lead Author
Inactive ✖
πŸ‘©‍πŸ’»
Linda Bahati
Co‑Author
Inactive ✖
πŸ‘¨‍πŸ’»
Jefferson Mwangolo
Co‑Author
Inactive ✖
πŸ‘©‍πŸŽ“
Florence Wavinya
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Esther Njeri
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Clemence Mwangolo
Guest Author

Followers