text_plain_output_1.png
from collections import Counter

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

# Code to plot a function. Borrowed from fastai library.
def plot_func(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)):
    x = np.linspace(min,max)
    fig,ax = plt.subplots(figsize=figsize)
    ax.plot(x,f(x))
    if tx is not None: ax.set_xlabel(tx)
    if ty is not None: ax.set_ylabel(ty)
    if title is not None: ax.set_title(title)

red_bag = np.array(['Apple'] * 4 + ['Orange'])
green_bag = np.array(['Orange'] * 9 + ['Apple'])
white_bag = np.array(['Orange'] * 5 + ['Apple'] * 5)

def surprise(probability):
    return np.log2(1 / probability)

table = pd.DataFrame({'Heads': [0.15, 0.9], 'Tails': [3.32, 0.1]}, index=['S(x)', 'P(x)'])
table

Entropy = table.loc['P(x)', 'Heads'] * table.loc['S(x)', 'Heads'] + table.loc['P(x)', 'Tails'] * table.loc['S(x)', 'Heads']
Entropy

P_e = np.array([0.9, 0.1])
S_e = np.array([0.15, 3.32])
Entropy = np.dot(P_e, S_e)
Entropy

def entropy(arr):
    ent = 0
    probs = dict()
    ctr = Counter(arr)
    for e in ctr:
        probs[f'P_{e}'] = ctr[e] / len(arr)
    for p in probs:
        ent += -1 * probs[p] * np.log2(probs[p])
        return round(ent, 2)

def show_entropy(bag_type=None, bag_type_str=None):
    ctr = Counter(bag_type)

show_entropy(bag_type=red_bag, bag_type_str='Red Bag')