How to calculate conditional probability in Python?

By Shivang Yadav Last updated : November 22, 2023

Conditional Probability

Conditional Probability is the probability of event A's occurrence given that event B has already occurred.

P(A|B) = P(Aâ‹‚B)/P(B)

Here, 

  • P(A|B) is the conditional probability for a given b to have occurred. 
  • P(Aâ‹‚B) is the probability of occurrence of A and B.
  • P(B) is the probability of occurrence of B.

Calculating Conditional Probability

To calculate conditional probability, you need to create a probability table using the crosstab() method and then extract values using the iloc[i, j] property. The margin cell of the crosstab given the probability of occurrence of both.

Example

Python program to calculate conditional probability

import pandas as pd
import numpy as np

# create pandas DataFrame with raw data
dataFrame = pd.DataFrame(
    {
        "City": np.repeat(np.array(["cityA", "cityB"]), 150),
        "sport": np.repeat(
            np.array(
                [
                    "Cricket",
                    "Kabaddi",
                    "FootBall",
                    "hockey",
                    "Cricket",
                    "Kabaddi",
                    "FootBall",
                    "hockey",
                ]
            ),
            (34, 40, 58, 18, 34, 52, 20, 44),
        ),
    }
)

cityData = pd.crosstab(
    index=dataFrame["City"], columns=dataFrame["sport"], margins=True
)
print("City Data :", cityData)

condProb = cityData.iloc[0, 0] / cityData.iloc[2, 0]
print("People from cityA preferring Cricket", condProb)

Output

The output of the above example is:

City Data : sport  Cricket  FootBall  Kabaddi  hockey  All
City                                          
cityA       34        58       40      18  150
cityB       34        20       52      44  150
All         68        78       92      62  300
People from cityA preferring Cricket 0.5

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.