Math for Machine Learning
Contents

01 Foundations

Functions

Learn function notation, inputs and outputs, domain and range, and the behaviour of linear functions.

mathematics · functions

Functions

A function is a rule that takes an input and produces exactly one output.

Function notation is written as:

\[f(x)\]

This is read as "\(f\) of \(x\)." The expression \(f(x)\) represents the output produced by the function when the input is \(x\).

For example:

\[f(x)=3x+2\]

To calculate \(f(4)\), substitute 4 wherever \(x\) appears:

\[f(4)=3(4)+2=14\]

A function must assign exactly one output to each input. Different inputs can produce the same output. For example:

\[f(x)=x^2\]

Then:

\[f(2)=4\]

and:

\[f(-2)=4\]

Both inputs produce the same output, which is allowed.

4.1 Domain and range

The domain is the set of allowed inputs to a function.

The range is the set of outputs the function can produce.

Consider:

\[f(x)=x^2+2\]

Any real number can be used as an input, so the domain is all real numbers.

Because \(x^2\) cannot be negative:

\[x^2\ge0\]

Therefore:

\[x^2+2\ge2\]

The smallest possible output is 2. The range is:

\[[2,\infty)\]

Now consider:

\[f(x)=\frac{1}{x}\]

The input cannot be zero because division by zero is undefined.

Its domain is all real numbers except zero. Its range is also all real numbers except zero.

For:

\[f(x)=\frac{2}{x-3}\]

the denominator cannot be zero:

\[x-3\ne0\]

Therefore, \(x\ne3\).

4.2 Linear functions

A linear function has the general form:

\[f(x)=mx+c\]

where:

  • \(m\) is the slope.
  • \(c\) is the \(y\)-intercept.

The slope describes how quickly the output changes as the input changes.

The \(y\)-intercept is the output when \(x=0\).

For example:

\[f(x)=2x+3\]

Its slope is 2, and its \(y\)-intercept is 3.

When \(x=0\):

\[f(0)=2(0)+3=3\]

So the graph crosses the \(y\)-axis at \((0,3)\).

4.3 Plotting a linear function

Consider:

\[f(x)=x+2\]

Some input-output pairs are:

\[\begin{aligned} x=-2&\implies y=0\\ x=0&\implies y=2\\ x=2&\implies y=4 \end{aligned}\]

These give the coordinates:

\[(-2,0),\quad(0,2),\quad(2,4)\]

Plotting these points and connecting them produces a straight line.

4.4 Calculating slope from two points

Given two points:

\[(x_1,y_1),\qquad(x_2,y_2)\]

the slope is:

\[\boxed{m=\frac{y_2-y_1}{x_2-x_1}}\]

For example, using the points \((-2,0)\) and \((0,2)\):

\[m=\frac{2-0}{0-(-2)}\]
\[m=\frac{2}{2}=1\]

The slope is 1.

4.5 Positive, negative, and zero slopes

A positive slope means the line increases as \(x\) increases.

A negative slope means the line decreases as \(x\) increases.

A zero slope means the function is constant: its output does not change as the input changes.

For example:

\[f(x)=5\]

has slope 0.

A function such as:

\[f(x)=-2x\]

has slope \(-2\) and \(y\)-intercept 0.

4.6 The x-intercept and y-intercept

The \(y\)-intercept is found by setting \(x=0\).

The \(x\)-intercept is found by setting \(y=0\), or equivalently solving \(f(x)=0\).

For:

\[f(x)=2x+4\]

The \(y\)-intercept is:

\[(0,4)\]

To find the \(x\)-intercept:

\[2x+4=0\]
\[2x=-4\]
\[x=-2\]

So the \(x\)-intercept is:

\[(-2,0)\]

4.7 Parallel and perpendicular lines

Two non-vertical lines are parallel when they have the same slope.

For example:

\[f(x)=3x+2\]

and:

\[g(x)=3x+5\]

Both have slope 3, so they are parallel.

Two non-vertical lines are perpendicular when the product of their slopes is \(-1\):

\[m_1m_2=-1\]

For example, a line with slope 2 is perpendicular to a line with slope:

\[-\frac12\]

A horizontal line has slope 0. A vertical line has an undefined slope.