Mastering AI/ML from First Principles - Part 3


Updated on: AI-ML

Functions — The Language of Machine Learning

Series: Mastering AI/ML from First Principles
Phase: Math Foundations
Blog #: 03
Topic: Algebra for Machine Learning → Functions - The Language of Machine Learning

1. Why This Matters

In my previous post, I got comfortable with numbers and variables.

But something still felt incomplete.

I could represent data…

but I didn’t yet understand:

  • How do we go from input → output?
  • How does a model actually “predict” something?

That’s when it clicked:

Machine learning is nothing but learning a function.

Not a metaphor. Not an analogy.

Literally a function.

If I don’t deeply understand functions, I won’t understand ML.

2. Intuition — What Is a Function, Really?

The simplest way I now think about a function:

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

$$
\text{Input} \rightarrow \text{Function} \rightarrow \text{Output}
$$

Examples from daily life:

  • Enter temperature → get recommendation
  • Enter hours studied → get expected score
  • Enter house features → get price

Each of these is:

  • A mapping
  • From input → output
  • With some underlying rule

That rule is the function.

3. Core Explanation

Mathematically, we write a function like this:

$$
f(x) = \text{output}
$$

Where:

  • $x$ = input
  • $f$ = the rule
  • $f(x)$ = output

Example

$$
f(x) = 2x + 3
$$

If:

$$
f(1) = 2(1) + 3 = 5
$$

$$
f(2) = 2(2) + 3 = 7
$$

This defines a relationship between input and output.

In Machine Learning Terms

Math Term

ML Equivalent

Input ($x$)

Features

Output ($f(x)$)

Prediction

Function ($f$)

Model

 

So when we say:

“Train a model”

What we really mean is:

$$
\text{Find } f \text{ such that } f(x) \approx y
$$

4. Real-World Analogy

Think of a coffee machine 

You input:

  • Coffee beans
  • Water
  • Settings

The machine applies a process → gives coffee.

That process = function

Different machines → different functions → different outputs.

5. Types of Functions (Important for ML)

1. Linear Function

$$
f(x) = mx + b
$$

  • Straight line
  • Simple relationship
  • Used in Linear Regression

2. Non-Linear Function

$$
f(x) = x^2
$$

  • Curved relationship
  • More realistic in real-world data

3. Multi-variable Function

$$
f(x_1, x_2, x_3, \dots, x_n)
$$

Example:

$$
\text{House Price} = f(\text{size}, \text{bedrooms}, \text{location})
$$

This is where real ML lives.

6. Worked Examples

Example 1: Simple Function

$$
f(x) = 3x + 2
$$

If $x = 4$:

$$
f(4) = 3 \cdot 4 + 2 = 14
$$

Example 2: Multi-variable Function

$$
f(\text{size}, \text{bedrooms}) = 100 \cdot \text{size} + 5000 \cdot \text{bedrooms}
$$

Input:

  • size = 1000
  • bedrooms = 2

Output:

$$
= 100 \cdot 1000 + 5000 \cdot 2
$$
$$
= 100000 + 10000 = 110000
$$

7. Math (Only What’s Needed)

Domain

All possible inputs:

$$
\text{Domain} = {x \mid x \text{ is allowed input}}
$$

Range

All possible outputs:

$$
\text{Range} = {f(x) \mid x \in \text{Domain}}
$$

Mapping Rule

Each input maps to exactly one output:

$$
x \rightarrow f(x)
$$

8. Python Implementation

# Simple function
def linear_function(x):
    return 2 * x + 3

print(linear_function(5))  # Output: 13

Multi-variable function

def house_price(size, bedrooms):
    return 100 * size + 5000 * bedrooms

print(house_price(1000, 2))

Using NumPy (vectorized)

import numpy as np

x = np.array([1, 2, 3, 4])
y = 2 * x + 3

print(y)

9. Visualization

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = 2 * x + 3

plt.plot(x, y)
plt.scatter([2], [7], color='red')
plt.title("Function Visualization")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.show()

10. Key Takeaways

  • A function is a mapping from input → output
  • Machine learning = learning this mapping
  • Models are just functions
  • Linear vs non-linear functions matter
  • Real ML uses multi-variable functions

11. What Confused Me

I initially thought:

“Models are complex systems, not simple functions.”

That was wrong.

Even advanced neural networks are:

$$
f(x; \theta)
$$

Where:

  • $x$ = input
  • $\theta$ = parameters
  • $f$ = learned function

Another confusion:

Why do we need different models?

Now I see:

Different models = different function families

12. References

  • Introduction to Statistical Learning (ISLR)
  • Hands-On Machine Learning with Scikit-Learn, Keras & TensorFlow — Aurélien Géron
  • Pattern Recognition and Machine Learning — Bishop
  • Scikit-learn Documentation

Final Note

If this blog is not crystal clear:

  • Derivatives will feel abstract
  • Gradients will feel confusing
  • Neural networks will feel like magic

This is the foundation.