[PYTHON] How to draw a 3D graph before optimization

How to draw a graph of the optimization algorithm in DEAP.

How to draw a 3D graph before optimization.

First of all, what kind of 3D graph you want to draw is the evaluation function in the library of genetic algorithms called DEAP. Since I want to consider multi-purpose optimization, it seems easier to understand if I draw a 3D graph, so first I will write a 3D graph.

See this area for the evaluation function of optimization. DEAP 1.3.1 documentation A super-introduction to multipurpose optimization problems that anyone can understand

To draw a three-dimensional graph, first make points in a mesh shape on the two axes X1 and x2. At that point, the value on the z-axis is calculated. Draw a 3D graph of x1, x2, Z values in Axes3D.

1) Make mesh-like dots.

1: Use the numpy ʻarangefunction to create evenly spaced numbers. np.arange (-30,30,1)From a-30 to 30, you can create numerical values with one interval at a time. 2:meshgrid (a, b)` gives a grid point at the intersection of a and b of the one-dimensional array, and puts it in X1 and X2.

#How to make a mesh
import numpy as np
a = np.arange(-30,30,1)
b = np.arange(-30,30,1)
print(a)
#[-30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13
# -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5
#   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23
#  24  25  26  27  28  29]

print(b)
#[-30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13
# -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5
#   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23
#  24  25  26  27  28  29]

X1, x2 = np.meshgrid(a, b)

print(X1)
#[[-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]
# ...
# [-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]]

print(X2)
#[[-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]
# ...
# [-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]
# [-30 -29 -28 ...  27  28  29]]

import matplotlib.pyplot as plt
plt.scatter(X1,X2,s=1)
plt.show()

For confirmation, I drew a two-dimensional scatter plot of X1 and X2, and it became as follows. scatter_Figure 2020-08-30 161126.png

It is a mesh properly. Next, calculate the Z-axis value and make a three-dimensional graph. 3: Z calculated what is called the Ackley function. Formula

{f(x_{1}, x_{2})=20-20\exp \biggl( -0.2\sqrt{\frac{1}{n}\sum_{i=1}^{n}x_{i}^2} \biggr) +e-\exp \biggl(\frac{1}{n}\sum_{i=1}^{n}\cos(2\pi x_{i}) \biggr)
}

For the time being, if you plot with X1 and Z, the graph will be as follows. x1_z_Figure 2020-08-30 162742.png

4: Draw a 3D graph. Use from mpl_toolkits.mplot3d import Axes3D to import a library that draws 3D graphs. ʻAx.plot_wireframe (X1, X2, Z)` makes a three-dimensional graph.

plot_surface: Displayed as a surface. surfaceFigure 2020-08-30 163457.png

plot_wireframe: Displayed in wireframe. wireflameFigure 2020-08-30 163108.png

Z =20 - 20 * np.exp(-0.2 * np.sqrt((X1 ** 2 + X2 ** 2) / 2)) + np.exp(1) - np.exp((np.cos(2 * np.pi * X1) + np.cos(2 * np.pi * X2)) / 2)

plt.scatter(X1,Z,s=1)
plt.show()


from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

#Displayed in wireframe
ax.plot_wireframe(X1, X2, Z)
#Display on the surface
#ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap='hsv')

plt.show()

Recommended Posts

How to draw a 3D graph before optimization
How to draw a graph using Matplotlib
[Python] How to draw a line graph with Matplotlib
[Python] How to convert a 2D list to a 1D list
Draw a flat surface with a matplotlib 3d graph
Draw a "breast curved surface" in a 3D graph (1)
How to draw a bar graph that summarizes multiple series with matplotlib
[Python] How to draw a histogram in Matplotlib
Draw a "breast curved surface" in a 3D graph (2)
[Python] How to create a 2D histogram with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
How to call a function
How to hack a terminal
Draw a graph with networkx
How to transpose a 2D array using only python [Note]
[Visualization] I want to draw a beautiful graph with Plotly
How to make a Japanese-English translation
How to write a Python class
Draw a graph with Julia + PyQtGraph (2)
How to put a symbolic link
Try to draw a Bezier curve
Draw a loose graph with matplotlib
How to make a slack bot
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
Draw a graph with pandas + XlsxWriter
How to delete a Docker container
How to make a crawler --Basic
Draw a graph with PySimple GUI
How to create a config file
How to create a clone from Github
How to split and save a DataFrame
How to build a sphinx translation environment
How to create a git clone folder
Qiita (1) How to write a code name
How to add a package with PyCharm
[Python] How to make a class iterable
(Matplotlib) I want to draw a graph with a size specified in pixels
[Colab] How to copy a huge dataset
[Python] How to invert a character string
How to install a package using a repository
[Ubuntu] How to execute a shell script
How to get a stacktrace in python
How to put a half-width space before letters and numbers in Python.
How to draw a vertical line on a heatmap drawn with Python seaborn
How to create a repository from media
How to make a Backtrader custom indicator
How to choose a Seaborn color palette
How to test on a Django-authenticated page
How to make a Pelican site map
Simply draw a graph by specifying a file
Draw a graph with PyQtGraph Part 1-Drawing
How to run a Maya Python script
How to draw OpenCV images in Pygame