Shannon Entropy

Shannon entropy is a key notion that sheds light on the underlying laws governing information transmission and processing in a variety of domains, such as data compression, encryption, and communication networks. Shannon entropy, which bears the name of the renowned mathematician and electrical engineer Claude Shannon, offers a deep understanding of the uncertainty or unpredictability that exists in a particular piece of data or information.

In his 1948 publication "A Mathematical Theory of Communication," Claude Shannon established the notion of entropy. Shannon sought to provide a mathematical framework in this seminal study that would allow one to estimate the capacity and efficiency of communication networks, independent of the type of data being carried. Shannon brought this idea to the study of information theory by taking inspiration from the discipline of thermodynamics, where entropy quantifies the disorder or unpredictability of a physical system.

Formula for Shannon Entropy

Fundamentally, Shannon entropy calculates the average level of surprise or uncertainty related to a random variable's result. Formally, the Shannon entropy H(X) given a discrete random variable X with probability distribution P(X) is defined as follows:

Shannon Entropy

The information content of a result is measured by its negative logarithm base 2, and this formula calculates the weighted average of the information value of each potential outcome of the random variable. Since they are less surprising or unpredictable, events with greater probability seem to add less to the total entropy.

Code:

Now we will try to calculate Shannon entropy along with running some tests on it.

Importing Libraries

Now we will create a function that effectively computes the entropy of a node in a decision tree based on the distribution of classes in the samples at that node, providing a measure of the impurity or randomness of the class distribution.

Output:

0

Now we will create a function that effectively splits the dataset at a given node based on the value of a specified feature, determining which samples belong to the left and right branches of the split.

Output: Shannon Entropy

Output:

Shannon Entropy

Output:

Shannon Entropy

Output:

Shannon Entropy

It seems it passes all the test cases that are to be tested.