</>
Skip to content
Data Science lessons (11/42)

Data Science — Probability

Basic probability

# P(A) = favorable outcomes / total outcomes
def probability(favorable, total):
    return favorable / total

# Probability of rolling 6
p_six = probability(1, 6)
print(f"P(6) = {p_six:.4f}")

Conditional probability

# P(A|B) = P(A and B) / P(B)
def conditional_prob(p_a_and_b, p_b):
    return p_a_and_b / p_b

# Bayes' theorem
def bayes(p_b_given_a, p_a, p_b):
    return (p_b_given_a * p_a) / p_b

Common distributions

from scipy import stats

# Normal
normal = stats.norm(0, 1)
print(f"PDF at 0: {normal.pdf(0)}")

# Binomial
binom = stats.binom(n=10, p=0.5)
print(f"P(X=5): {binom.pmf(5)}")

# Poisson
poisson = stats.poisson(mu=3)
print(f"P(X=2): {poisson.pmf(2)}")

Expected value

def expected_value(values, probabilities):
    return sum(v * p for v, p in zip(values, probabilities))

values = [1, 2, 3, 4, 5]
probs = [0.1, 0.2, 0.3, 0.2, 0.2]
print(f"E[X] = {expected_value(values, probs)}")

Mini Practice

  1. Calculate basic probabilities
  2. Apply Bayes' theorem
  3. Work with distributions
  4. Compute expected values

Up Next

Continue with Hypothesis Testing - Statistical tests.

Related Topics

Frequently Asked Questions about Probability

What is Probability in Data Science?

Probability is a fundamental concept in Data Science. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Probability?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Probability.

Why is Probability important in Data Science?

Probability is essential for Data Science development. Understanding this concept will help you write better code and solve real-world problems more effectively.