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 xx is our input and ff is our function, our output is f(x)f(x), written as f:xf(x)f: x \mapsto f(x).

Functions Examples

Here are some common examples of functions in math:

  1. Linear Function: f(x)=2x+3f(x) = 2x + 3
  2. Quadratic Function: f(x)=x24x+4f(x) = x^2 - 4x + 4
  3. Exponential Function: f(x)=exf(x) = e^x
  4. Logarithmic Function: f(x)=log(x)f(x) = \log(x)

Defining a Function in Python

Let's define a quadratic function in Python. We'll use the example function f(x)=x2+2x+1f(x) = x^2 + 2x + 1.

Python
# Defining a simple function
def simple_function(x):
    return x**2 + 2*x + 1

Here, simple_function is the name, and x is the input. The function returns the value of x2+2x+1x^2 + 2x + 1.

Here's the breakdown:

  • def is the keyword to define a function.
  • simple_function(x) is the name and parameter.
  • return x**2 + 2*x + 1 is 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 xx. For x=3x = 3, what does simple_function return? Let's see:

Python
def simple_function(x):
    return x**2 + 2*x + 1

# Evaluate function at x=3
result = simple_function(3)
print("f(3) =", result)  # f(3) = 16

When you call simple_function(3), Python:

  1. Takes x=3x = 3 as the input.
  2. Computes 32+2×3+13^2 + 2 \times 3 + 1.
  3. Returns 16, which is printed as f(3) = 16.

Plotting the Function

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal