Skip to content
AI360Xpert
Beta

Variables and Expressions in ML

A variable names a value that changes, an expression combines variables and numbers into a computable recipe. Losses, features and hyperparameters are all expressions waiting for inputs.

The expression 2x squared minus 3x plus 1 labels its coefficient, variable and constant, then x equals 4 substitutes in and evaluates to 21
The expression 2x squared minus 3x plus 1 labels its coefficient, variable and constant, then x equals 4 substitutes in and evaluates to 21

Why Does This Exist?

Every formula in ML is an expression with the inputs missing. A loss w12+3w22w_1^2 + 3w_2^2 waits for weights. A feature 2x2−3x+12x^2 - 3x + 1 waits for measurements. A learning-rate schedule waits for the epoch. Training is substitution at scale: plug values into expressions, read the outputs, adjust, repeat billions of times.

Variables and expressions are also where code meets math. The math writes 2x2x, the code writes 2 * x, and fluency means seeing one in the other instantly. Expressions evaluate to numbers; equations with = signs get solved, which is the job of algebra. How whole networks of expressions evaluate node by node is in computational graphs.

The one idea you need first. A variable is a labelled box holding a number that can change. An expression is a recipe combining boxes and numbers with operations. Substituting means opening the boxes and computing.

Think of It Like This

Labelled jars in a kitchen

A kitchen has jars labelled flour, sugar, salt. A recipe says "two cups flour plus one sugar minus half salt": an expression over jars. Different jars, same recipe, different cake. The recipe never changes; only the jar contents do.

A model is a recipe book of expressions, and a training step fills the jars. The loss expression tastes the result. Backpropagation figures out which jar to adjust. Nobody rewrites the recipe per cake, just as nobody rewrites the loss per batch.

Where it stops: jars hold one thing at a time, while ML variables hold millions of values at once in tensors. The analogy shows naming and substitution; the scale needs its own page.

How It Actually Works

An expression has parts with names. In 2x2−3x+12x^2 - 3x + 1: 22 and −3-3 are coefficients (multipliers of variable terms), xx is the variable, the superscript 22 is an exponent, and 11 is the constant term (no variable attached). Terms are the chunks between plus and minus signs: 2x22x^2, −3x-3x, +1+1. Like terms share the same variable part and combine: 4x+2x=6x4x + 2x = 6x, but 4x+2x24x + 2x^2 cannot merge.

Evaluating means substituting a value for every variable, then computing with the precedence ladder. An expression has no = sign, so there is nothing to solve; it produces a number, not an answer to a question.

Worked example: evaluating at x equals 4

Expression: 2x2−3x+12x^2 - 3x + 1. Substitute x=4x = 4:

Powers first: 42=164^2 = 16, so 2×16−3×4+12 \times 16 - 3 \times 4 + 1. Products: 32−12+132 - 12 + 1. Sums left to right: 32−12=2032 - 12 = 20, then 20+1=2120 + 1 = 21. The value is 2121.

Same expression at x=0x = 0 collapses to the constant: 0−0+1=10 - 0 + 1 = 1. At x=1x = 1: 2−3+1=02 - 3 + 1 = 0. One recipe, three fillings, three results. The diagram above labels each part before the substitution runs.

Code

def evaluate(x: float) -> float:    # 2x^2 - 3x + 1 with the value plugged in for x.    return 2 * x**2 - 3 * x + 1

print(evaluate(4.0))  # -> 21.0print(evaluate(0.0))  # -> 1.0print(evaluate(1.0))  # -> 0.0

Note 2 * x, never 2x: juxtaposition is math shorthand, and code demands the explicit star. The three prints are the three hand computations, agreeing exactly.

Watch Out For

Substituting a negative without parentheses

Evaluating x2x^2 at x=−2x = -2 by writing -2**2 gives −4-4 in Python, because the power binds first. The intended (−2)2=4(-2)^2 = 4 needs the brackets. The symptom is a feature that flips sign on every negative input, and a model trained on corrupted values with no error anywhere.

Parenthesise every substituted negative: (-2)**2. Better, substitute through a variable so the value carries its own grouping.

Reading 2x as two-x in code

Math writes 2x2x and means multiply. Code written as 2x is a syntax error at best; worse is a variable actually named x2 being read as "two x". Features silently swap when a formula is transcribed carelessly.

Transcribe juxtaposition as * mechanically, and never name a variable so it reads as a coefficient times something else. The transcription step deserves its own slow pass.

The Quick Version

  • Variables name changing values; expressions combine them into computable recipes.
  • Coefficients multiply, constants stand alone, exponents shape, terms add.
  • Evaluate by substituting values, then computing down the precedence ladder.
  • 2x2−3x+12x^2 - 3x + 1 at x=4x = 4 is 2121; at x=0x = 0 it is the constant 11.
  • Parenthesise substituted negatives; transcribe 2x2x as 2 * x, never by sight.