Skip to content
AI360Xpert
Beta

Functions and Graphs for ML

A function turns each input into exactly one output, and its graph draws that promise as a shape. Models are functions; training sculpts their graphs to fit data.

The line 2x plus 1 passes through (minus 1, minus 1), (0, 1), (1, 3) and (2, 5), one output per input, which is why no vertical line can hit it twice
The line 2x plus 1 passes through (minus 1, minus 1), (0, 1), (1, 3) and (2, 5), one output per input, which is why no vertical line can hit it twice

Why Does This Exist?

A model is a function: features in, prediction out, exactly one prediction per input. Training chooses among functions; inference evaluates the chosen one; the graph shows what was chosen. Every later idea, slope, curvature, activation shape, loss landscape, is a statement about some function's graph.

The contract has one clause: each input gets exactly one output. That single rule separates functions from general relations and makes evaluation well-defined. Slopes of these graphs are the job of derivatives and partial derivatives; evaluating whole networks of them is computational graphs.

The one idea you need first. A variable holds a number; an expression computes with it. A function names the whole input-to-output mapping so it can be reused, graphed and studied. Variables and expressions covers the pieces; this page names the mapping.

Think of It Like This

A vending machine with honest buttons

Press B4 and the same coil turns every time, dropping the same snack. That reliability is the function contract: one button, one outcome, repeatable. A machine that drops crisps or chocolate at random from B4 is a relation, not a function, and nobody trusts it.

Training stocks the machine: it decides which snack each button yields. Inference presses buttons. The graph is the stocking diagram, every button-output pair drawn at once, and reading it tells you what any press will do before you spend the coin.

Where it stops: vending machines have finitely many buttons, while math functions take infinitely many inputs. The analogy shows the contract; the graph below shows the continuum.

How It Actually Works

A function ff maps each input xx to exactly one output f(x)f(x). Three views show the same mapping: the formula f(x)=2x+1f(x) = 2x + 1, a table of pairs, and the graph of plotted points (x,f(x))(x, f(x)). Graphs reveal at a glance what formulas hide: slope, intercepts, roots, bends.

The vertical line test enforces the contract visually: if any vertical line hits the graph twice, one input has two outputs and the relation is not a function. The circle x2+y2=1x^2 + y^2 = 1 fails at x=0x = 0 with y=1y = 1 and y=−1y = -1. Every function graph passes.

Worked example: tabulate and plot f(x) equals 2x plus 1

Evaluate at four inputs. f(−1)=−2+1=−1f(-1) = -2 + 1 = -1. f(0)=0+1=1f(0) = 0 + 1 = 1. f(1)=2+1=3f(1) = 2 + 1 = 3. f(2)=4+1=5f(2) = 4 + 1 = 5. Table: (−1,−1)(-1, -1), (0,1)(0, 1), (1,3)(1, 3), (2,5)(2, 5).

Plot them: each step right adds 2 up, a constant slope of 2. The yy-intercept is 11, the value at x=0x = 0. The root solves 2x+1=02x + 1 = 0, giving x=−0.5x = -0.5. The diagram above draws this line through all four points: straight, rising, crossing yy at 1.

Code

def f(x: float) -> float:    return 2 * x + 1

xs: list[float] = [-1.0, 0.0, 1.0, 2.0]print([f(x) for x in xs])  # -> [-1.0, 1.0, 3.0, 5.0]print(f(1.0) == 3.0)  # -> True

The function is the mapping, the list comprehension is the table, and the last line asserts one entry of it. Models scale this pattern: the function grows to millions of parameters, the loop to millions of inputs.

Watch Out For

Calling a circle a function

x2+y2=1x^2 + y^2 = 1 looks graphable and fails the contract: at x=0x = 0 both y=1y = 1 and y=−1y = -1 satisfy it. Code that solves for yy and keeps one branch silently discards half the curve, and downstream geometry inherits the cut.

Split relations into function branches explicitly (upper and lower semicircle) or parametrize. Never let a square root silently choose the positive half.

Reading f(x) as f times x

In f(x)=2x+1f(x) = 2x + 1, the f(x)f(x) is one symbol, "the output at xx", while 2x2x multiplies. Beginners distribute the ff or cancel it, producing artefacts like f=2+1/xf = 2 + 1/x. The transcription error then compiles fine in code as a call on a wrong-named function.

Read f(x)f(x) as "f of x" aloud until it sticks. In code, the function name and the call parentheses are the same idea made executable.

The Quick Version

  • A function gives each input exactly one output; the graph draws every pair at once.
  • Formula, table and graph are three views of one mapping; graphs show slope and shape fastest.
  • The vertical line test: two hits means not a function, like the circle.
  • f(x)=2x+1f(x) = 2x + 1 through (−1,−1),(0,1),(1,3),(2,5)(-1,-1), (0,1), (1,3), (2,5): slope 2, intercept 1, root −0.5-0.5.
  • Models are functions; training picks the function, inference evaluates it.