What’s a function?

A function is like a machine that takes input and generates output. Most machines have different settings that regulate the output (typically, a button pad is there to modify the settings). These settings are called the parameters of the function.

Example of a function

For example, in fa,b(x1,x2)=ax1+bx2f_{a,b}(x_1,x_2)=ax_1+bx_2, ff is a function, x1x_1 and x2x_2 are two inputs, aa and bb are two parameters, and ax1+bx2ax_1+bx_2 is the function’s output.

Functions in Python

Remember: This course uses Python. If you’re not fluent in Python, you may find this course very helpful.

Here’s how we can experiment with the function in the example above in Python:

Press + to interact
def f(x1,x2):
a , b = 2 , -4
output = a*x1 + b*x2
return output
x1 , x2 = 5 , 7
c = f(x1, x2)
print(c)

Formal definition

Formally, a function is a relation from a set of inputs to a set of possible outputs, where each input is related to exactly one output.

This means that if an object, xx, is in the set of inputs (called the domain), then a function, ff, maps the object, xx, to precisely one object, f(x)f(x), in the set of possible outputs (called the codomain).

Generality of a function’s input and output

The input, xx, and output, yy, of a function can be any object, such as an array, matrix, tensor, and so on. A matrix is a two-dimensional array, whereas a tensor is an array of several (typically more than two) dimensions. Don’t worry about this too much. We’ll learn about matrices, vectors, and tensors in detail in subsequent lessons.

Example: Array input, scalar output

The following function adds the elements of the input array and returns the sum. f([x1,x2,...,xn])=x1+x2+...+xnf([x_1,x_2, ... ,x_n])=x_1+x_2+...+x_n

Press + to interact
import numpy as np
def f(x):
return np.sum(x)
print(f(np.array([-1, 3, 4.6, 0, 9])))

Example: Array input, array output

The following function sorts the input array in ascending order: f([x1,x2,...,xn])=[xi1,xi2,...,xin]f([x_1,x_2, ... ,x_n])=[x_{i1},x_{i2},...,x_{in}] where xijxik;k>jx_{ij} \le x_{ik}; k>j

Press + to interact
import numpy as np
def f(x):
return np.sort(x)
print(f(np.array([3, 4, 6, 1, 2, -1, 0, 9])))

Visualization of a function

Visualizing a function can be beneficial. In data analysis, visualization can give a better insight into the relationship between input and output.

Visualizing polynomials

Let’s visualize some simple polynomial functions: f(x)=x2f(x)=x^2 and f(x)=x3f(x)=x^3.

Press + to interact
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0., 5., 0.2)
plt.plot(x, x**2, 'bs', x, x**3, 'g^')
plt.xlabel('x');plt.ylabel('f(x)')
plt.legend(['x^2', 'x^3'], loc='upper left')

Visualizing planes in 3D

A plane in 3D can be viewed as a function, fa,b,c(x1,x2)=ax1+bx2+cf_{a,b,c}(x_1,x_2)=ax_1+bx_2+c. If cc is 00, then the plane passes through the origin and forms a subspace (more on this later).

Note: We can also interact with the 3D plot using the following app. Launch the app and then press the “Run” button inside the “Jupyter” notebook. Use your mouse to rotate and zoom the plot. Feel free to edit the code and experiment with the parameters and code.

Please login to launch live app!