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!
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 .
Here are some common examples of functions in math:
- Linear Function:
- Quadratic Function:
- Exponential Function:
- Logarithmic Function:
Let's define a quadratic function in Python. We'll use the example function .
Python1# Defining a simple function 2def simple_function(x): 3 return x**2 + 2*x + 1
Here, simple_function
is the name, and x
is the input. The function returns the value of .
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.
We can find our function's value at different . For , what does simple_function
return? Let's see:
Python1def simple_function(x): 2 return x**2 + 2*x + 1 3 4# Evaluate function at x=3 5result = simple_function(3) 6print("f(3) =", result) # f(3) = 16
When you call simple_function(3)
, Python:
- Takes as the input.
- Computes .
- Returns 16, which is printed as
f(3) = 16
.
If we evaluate the function at every possible , we will get a list of corresponding function values. Each pair and can be represented as a point on the plane. If we draw all such pairs, we will get the plot of our function.
Plotting in python is easy with the matplotlib
library:
Python1import matplotlib.pyplot as plt 2import numpy as np 3 4# Define the function again for plotting 5def simple_function(x): 6 return x**2 + 2*x + 1 7 8# Generate x values 9x = np.linspace(-10, 10, 400) 10# Generate y values using the function 11y = simple_function(x) 12 13# Plot the function 14plt.plot(x, y, label='f(x) = x^2 + 2x + 1') 15plt.xlabel('x') 16plt.ylabel('f(x)') 17plt.title('Plot of the Function f(x) = x^2 + 2x + 1') 18plt.legend() 19plt.grid(True) 20plt.show()
This code will produce a plot of the quadratic function . The curve will help you better understand how the function behaves. Let's take a look at the result:
Here are some insights we can get by simply looking at this picture:
- The function is zero at around .
- Before this, the function decreases.
- After this, the function increases.
- The farther we from the , the faster this function grows.
To help you better understand plotting, here is a breakdown of the code:
- We import
matplotlib.pyplot
for plotting andnumpy
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)
. The400
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.
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 the is roughly .
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:
- Sine: . Python:
math.sin(x)
- Cosine: . Python:
math.cos(x)
- Tangent: . Python:
math.tan(x)
Logarithms:
- Natural Logarithm: (base ). Python:
math.log(x)
- Logarithm Base 10: . Python:
math.log10(x)
Other:
- Square Root: . Python:
math.sqrt(x)
- Exponential: . Python:
math.exp(x)
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!