Visualize 1D Function Optimization-3

Line Plot of Test Function

We could sample the input space randomly, but the benefit of a uniform line or grid of points is that it can be used to generate a smooth plot.

It is smooth because the points in the input space are ordered from smallest to largest. This ordering is important as we expect (hope) that the output of the objective function has a similar smooth relationship between values, e.g. small changes in input result in locally consistent (smooth) changes in the output of the function.

In this case, we can use the samples to generate a line plot of the objective function with the input points (x) on the x-axis of the plot and the objective function output (results) on the y-axis of the plot.

…

create a line plot of input vs result

pyplot.plot(inputs, results)

show the plot

pyplot.show()
Tying this together, the complete example is listed below.

line plot of input vs result for a 1d objective function

from numpy import arange
from matplotlib import pyplot

objective function

def objective(x):
return x**2.0

define range for input

r_min, r_max = -5.0, 5.0

sample input range uniformly at 0.1 increments

inputs = arange(r_min, r_max, 0.1)

compute targets

results = objective(inputs)

create a line plot of input vs result

pyplot.plot(inputs, results)

show the plot

pyplot.show()
Running the example creates a line plot of the objective function.