🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #42

Random Variables

A random variable is a variable whose value is the numeric outcome of a random process. It's the formal bridge between "something uncertain happens" and "here's a number I can compute with."

Discrete vs Continuous

Discrete Random VariableContinuous Random Variable
Possible valuesCountable (finite or countably infinite list)Any value in a range (uncountably many)
ExampleNumber of defective items in a batch of 10Time until a server responds to a request
Described byProbability Mass Function (PMF): \(P(X=x)\)Probability Density Function (PDF): \(f(x)\)
ML exampleA predicted class label (0 or 1)A predicted house price

Notation

\[ X = \text{the random variable (e.g. "number of heads in 3 coin flips")} \] \[ P(X = 2) = \text{the probability that } X \text{ takes the specific value } 2 \]

Numerical Example

Let \(X\) = number of heads in 2 fair coin flips. Possible outcomes: HH, HT, TH, TT (each with probability 0.25).

# X can be 0, 1, or 2
# P(X=0) = P(TT) = 0.25
# P(X=1) = P(HT) + P(TH) = 0.5
# P(X=2) = P(HH) = 0.25

import numpy as np
outcomes = ["HH", "HT", "TH", "TT"]
X = [outcome.count("H") for outcome in outcomes]
print(X)   # [2, 1, 1, 0]

Why This Matters for ML

  • A classifier's predicted label is modeled as a random variable — that's what makes predict_proba() meaningful in the first place
  • Every feature in your dataset can be thought of as a sample drawn from some underlying random variable — this framing underlies statistical assumptions many models make

Common Mistakes

  • Treating a continuous variable's PDF value \(f(x)\) as a probability directly — for continuous variables, only the area under the curve over a range gives a probability; \(f(x)\) at a single point is a density, not a probability.

Interview Relevance

Q: "Is a predicted class label from a classifier discrete or continuous?" Discrete — it takes one of a finite, countable set of values (the class labels); the model's underlying probability output, however, is continuous (any value in [0,1]).

Practice Question

Is "number of customer support tickets filed in a day" a discrete or continuous random variable? What about "time between two consecutive tickets"?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Random Variables – FAQs

Quick answers about learning Random Variables in Machine Learning.

This free note from CodingNow 2.0 explains Random Variables in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Random Variables, is 100% free with no signup required.
With focused practice, most students grasp Random Variables in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now