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
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.

Here, simple_function is the name, and x is the input. The function returns the value of .

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:

When you call simple_function(3), Python:

  1. Takes x=3x = 3 as the input.
  2. Computes .
Plotting the Function

If we evaluate the function at every possible xx, we will get a list of corresponding function values. Each pair xx and f(x)f(x) can be represented as a point on the XYXY plane. If we draw all such pairs, we will get the plot of our function.

Plotting in python is easy with the matplotlib library:

Understanding the Code for Plotting

To help you better understand plotting, here is a breakdown of the code:

  • We import matplotlib.pyplot for plotting and numpy for array handling.
  • We define the function simple_function as before.
  • We generate a list of x-values ranging from -10 to 10 using np.linspace(-10, 10, 400). The 400 specifies the number of points to generate, ensuring a smooth plot.
  • We compute the corresponding y-values by passing our list of x-values to simple_function. Although the function is designed for a single input number, numpy allows it to process the entire list of x-values, returning a list of y-values.
  • We plot these x and y values using plt.plot.

Don't worry about memorizing all this plotting code! We will provide it in the exercises so you can focus on understanding functions rather than learning how to plot them with python.

Evaluating Function with the Plot

Once you have the function's plot, you can see the general rule this function represents. Also, the plot can be used to evaluate the function's value at a specific point. Let's look at this picture:

Here, we can easily evaluate that at x=5x=5 the f(x)f(x) is roughly 3636.

Common Mathematical Functions

Mathematical functions play a crucial role in various fields, including machine learning. Here are some common mathematical functions and how to calculate them in Python.

Trigonometric Functions:

  1. Sine: sin(x)\sin(x). Python: math.sin(x)
  2. Cosine: cos(x)\cos(x). Python: math.cos(x)
  3. Tangent: tan(x)\tan(x). Python: math.tan(x)
Lesson Summary

Great job! Today, we learned what functions are and how they relate inputs to outputs in math and programming. We also learned how to define a function in Python, saw a practical example of evaluating a function, and even plotted a function.

Functions are essential in machine learning for modeling and solving optimization problems. Understanding functions lays the foundation for more complex topics.

Awesome! Now, you'll get hands-on experience defining and evaluating your own functions in Python. This will solidify your understanding of functions, which is crucial for your journey into machine learning and beyond. Let's get started!

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