Exploring Mushroom Classification with Decision Trees

Rutik Patel
3 min readMar 26, 2024

--

Mushrooms are an intriguing part of the natural world, with their diverse shapes, colors, and textures. However, not all mushrooms are safe to consume, as some can be poisonous and even fatal if ingested. Foragers and mushroom enthusiasts often face the challenge of distinguishing between edible and poisonous mushrooms based on their physical characteristics.

In this blog post, we’ll explore a fascinating application of machine learning to tackle this challenge. Using a dataset containing information about various attributes of mushrooms, we’ll build a decision tree model to predict whether a mushroom is edible or poisonous based on its features. We’ll delve into the concepts of entropy, information gain, and decision tree construction, providing code snippets and outputs to illustrate each step of the process.

Problem Statement:

Imagine you’re starting a company that grows and sells wild mushrooms. Since not all mushrooms are edible, you want to be able to identify which mushrooms are safe to sell and which ones are poisonous. To do this, you’ll utilize a dataset containing information about the physical attributes of mushrooms and whether they are edible or poisonous.

Dataset Overview:

The dataset consists of 10 examples of mushrooms, each with three features:
1. Cap Color (Brown or Red)
2. Stalk Shape (Tapering or Enlarging)
3. Solitary (Yes or No)

Additionally, each mushroom is labeled as either edible (1) or poisonous (0).

Decision Tree

Decision trees are powerful tools for making decisions based on data. In this practice lab, we’ll explore how to build a decision tree using a dataset.

Here are the steps involved:

  1. First begin with all examples which are at the root node.
  2. Calculate information gain for splitting on all possible features and choose the one with the highest information gain.
  3. Split the dataset based on the selected feature, creating left and right branches of the tree.
  4. Repeat the splitting process until a stopping criteria is met.

In this blog, we’ll focus on implementing key functions to facilitate the splitting process:

  1. Calculating the entropy at a node.
  2. Splitting the dataset at a node into left and right branches based on a given feature.
  3. Calculating the information gain from splitting on a specific feature.
    Choosing the feature that maximizes information gain.

By implementing these functions, we’ll be able to construct a decision tree iteratively, effectively making decisions based on the provided data.

Approach:

1. Compute Entropy: We’ll start by implementing a function to calculate the entropy at a given node, which measures the impurity of the node.
2. Split Dataset: Next, we’ll create a function to split the dataset into left and right branches based on a chosen feature.
3. Compute Information Gain: We’ll calculate the information gain resulting from splitting the dataset on each feature and choose the feature that maximizes information gain as the best split.
4. Build Decision Tree: Using the above steps, we’ll recursively build a decision tree to classify mushrooms as edible or poisonous.

Implementation:

# Import necessary libraries
import numpy as np
# Define the compute_entropy function
def compute_entropy(y):
# Compute the fraction of edible mushrooms
p_edible = np.mean(y)

# Calculate entropy
if p_edible == 0 or p_edible == 1:
entropy = 0
else:
entropy = -p_edible * np.log2(p_edible) - (1 - p_edible) * np.log2(1 - p_edible)

return entropy
# Example usage:
y_example = np.array([1, 0, 1, 1, 0])
entropy_example = compute_entropy(y_example)
print("Entropy:", entropy_example)

Conclusion:

By leveraging decision trees and the concepts of entropy and information gain, we can effectively classify mushrooms as edible or poisonous based on their attributes. This approach demonstrates the power of machine learning algorithms in solving real-world classification problems.

For more information and code

Github : https://github.com/rutikkpatel/Practice-Assignments-Machine-Learning-3

--

--