Understanding Functions
Lesson Introduction
Hi there! Today, we're talking about functions. By the end, you'll know what functions are in math and how they apply to machine learning. We'll also teach you how to define and use functions in Python. Let's begin!
What is a Function?
A function in math relates an input to an output. It's like a machine: you put something in, and it gives you something back. For instance, if a function adds 2 to any number you give it, putting in 3 gives you 5, and putting in 7 gives you 9.
Formally, if is our input and is our function, our output is , written as .
Functions Examples
Here are some common examples of functions in math:
- Linear Function:
- Quadratic Function:
- Exponential Function:
- Logarithmic Function:
Defining a Function in Python
Let's define a quadratic function in Python. We'll use the example function .
Here, simple_function is the name, and x is the input. The function returns the value of .
Here's the breakdown:
defis the keyword to define a function.simple_function(x)is the name and parameter.return x**2 + 2*x + 1is what the function does.
Quadratic functions are common in machine learning for optimization problems. For example, finding the best-fit line for data points involves solving a quadratic problem.
Evaluating a Function
We can find our function's value at different . For , what does simple_function return? Let's see:
When you call simple_function(3), Python:
- Takes as the input.
- Computes .
- Returns 16, which is printed as
f(3) = 16.
Plotting the Function


