Trying to figure out the shape of information entropy in 3D.
2D case is clear as seen in https://en.wikipedia.org/wiki/Entropy_(information_theory)
'''
======================
3D surface (color map)
======================
Demonstrates plotting a 3D surface colored with the coolwarm color map.
The surface is made opaque by using antialiased=False.
Also demonstrates using the LinearLocator and custom formatting for the
z axis tick labels.
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
%matplotlib notebook
fig = plt.figure()
ax = fig.gca(projection='3d')
Xs = np.arange(0.01, 1, 0.01)
Ys = np.arange(0.01, 1, 0.01)
Xs, Ys = np.meshgrid(Xs, Ys)
Z = - (Xs * np.log(Xs) + Ys * np.log(Ys) + (1 - Xs - Ys) * np.log(1 - Xs - Ys))
ax.contour(Xs, Ys, Z)
# ax.plot_surface(Xs, Ys, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
/projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:10: RuntimeWarning: divide by zero encountered in log /projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:10: RuntimeWarning: invalid value encountered in log /projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:10: RuntimeWarning: invalid value encountered in multiply
<matplotlib.contour.QuadContourSet at 0x7f29692f2160>
res = []
p1s = []
p2s = []
zs = []
for p1 in np.arange(0.01, 1, 0.01):
for p2 in np.arange(0.01, 1 - p1, 0.01):
p3 = 1 - p1 - p2
z = - np.sum(p1 * np.log(p1) + p2 * np.log(p2) + p3 * np.log(p3))
p1s.append(p1)
p2s.append(p2)
zs.append(z)
p1s = np.array(p1s)
p2s = np.array(p2s)
zs = np.array(zs)
# Y = np.arange(0, X, 0.01)
# X, Y = np.meshgrid(X, Y)
# Z = np.sum(X * np.log(X) + Y * np.log(Y) + (1 - X - Y) * np.log(1 - X - Y))
# # Plot the surface.
# surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
# linewidth=0, antialiased=False)
# # Customize the z axis.
# ax.set_zlim(-1.01, 1.01)
# ax.zaxis.set_major_locator(LinearLocator(10))
# ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# # Add a color bar which maps values to colors.
# fig.colorbar(surf, shrink=0.5, aspect=5)
# plt.show()
/projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:30: RuntimeWarning: divide by zero encountered in log /projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:30: RuntimeWarning: invalid value encountered in double_scalars
p1s.shape
(4856,)
p2s.shape
(4856,)
zs.shape
(4856,)
X, Y = np.meshgrid(p1s, p2s)
X.shape
(4856, 4856)
/projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:6: RuntimeWarning: divide by zero encountered in log /projects/btl2/zxue/miniconda3/envs/ml/lib/python3.5/site-packages/ipykernel/__main__.py:6: RuntimeWarning: invalid value encountered in multiply
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7fc448ccbf28>
np.meshgrid([1, 2], [3, 4])
[array([[1, 2], [1, 2]]), array([[3, 3], [4, 4]])]