From 2c902e15e3d45d3b21ae0f86d59e6017755430e7 Mon Sep 17 00:00:00 2001 From: MIURA Yasuyuki Date: Wed, 15 Nov 2023 03:35:34 +0900 Subject: [PATCH 1/4] add step03/plot_surface.py --- step03/plot_surface.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 step03/plot_surface.py diff --git a/step03/plot_surface.py b/step03/plot_surface.py new file mode 100644 index 0000000..e78ea2f --- /dev/null +++ b/step03/plot_surface.py @@ -0,0 +1,21 @@ +import numpy as np +import matplotlib.pyplot as plt + + +X = np.array([[-2, -1, 0, 1, 2], + [-2, -1, 0, 1, 2], + [-2, -1, 0, 1, 2], + [-2, -1, 0, 1, 2], + [-2, -1, 0, 1, 2]]) +Y = np.array([[-2, -2, -2, -2, -2], + [-1, -1, -1, -1, -1], + [0, 0, 0, 0, 0], + [1, 1, 1, 1, 1], + [2, 2, 2, 2, 2]]) +Z = X ** 2 + Y ** 2 + +ax = plt.axes(projection='3d') # projection='3d'により3d用グラフを指定 +ax.plot_surface(X, Y, Z, cmap='jet') # cmap='jet'によりグラフの色を指定 +ax.set_xlabel('x') +ax.set_ylabel('y') +plt.show() From 87e1661cdcfb1db6d0c70e5558295522d8a987f1 Mon Sep 17 00:00:00 2001 From: MIURA Yasuyuki Date: Wed, 15 Nov 2023 03:47:41 +0900 Subject: [PATCH 2/4] update step03/plot_surface.py --- step03/plot_surface.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/step03/plot_surface.py b/step03/plot_surface.py index e78ea2f..a5219e5 100644 --- a/step03/plot_surface.py +++ b/step03/plot_surface.py @@ -2,6 +2,7 @@ import matplotlib.pyplot as plt +# 5x5 ==================== X = np.array([[-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2], @@ -19,3 +20,16 @@ ax.set_xlabel('x') ax.set_ylabel('y') plt.show() + +# np.meshgrid() ==================== +x = np.arange(-2, 2, 0.1) +y = np.arange(-2, 2, 0.1) + +X, Y = np.meshgrid(x, y) +Z = X ** 2 + Y ** 2 + +ax = plt.axes(projection='3d') +ax.plot_surface(X, Y, Z, cmap='jet') +ax.set_xlabel('x') +ax.set_ylabel('y') +plt.show() From 92c7ade9f813c88785feb2073db7e5d0065d6d68 Mon Sep 17 00:00:00 2001 From: MIURA Yasuyuki Date: Wed, 15 Nov 2023 03:48:03 +0900 Subject: [PATCH 3/4] add step03/plot_contour.py --- step03/plot_contour.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 step03/plot_contour.py diff --git a/step03/plot_contour.py b/step03/plot_contour.py new file mode 100644 index 0000000..7a897de --- /dev/null +++ b/step03/plot_contour.py @@ -0,0 +1,14 @@ +import numpy as np +import matplotlib.pyplot as plt + +x = np.arange(-2, 2, 0.1) +y = np.arange(-2, 2, 0.1) + +X, Y = np.meshgrid(x, y) +Z = X ** 2 + Y ** 2 + +ax = plt.axes() +ax.contour(X, Y, Z) +ax.set_xlabel('x') +ax.set_ylabel('y') +plt.show() From f1483b29716ffdd37ba4dae26cb94900c6c50f05 Mon Sep 17 00:00:00 2001 From: MIURA Yasuyuki Date: Wed, 15 Nov 2023 03:48:15 +0900 Subject: [PATCH 4/4] add step03/plot_norm.py --- step03/plot_norm.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 step03/plot_norm.py diff --git a/step03/plot_norm.py b/step03/plot_norm.py new file mode 100644 index 0000000..779f38d --- /dev/null +++ b/step03/plot_norm.py @@ -0,0 +1,35 @@ +import numpy as np +import matplotlib.pyplot as plt + +def multivariate_normal(x, mu, cov): + det = np.linalg.det(cov) + inv = np.linalg.inv(cov) + D = len(x) + z = 1 / np.sqrt((2 * np.pi) ** D * det) + y = z * np.exp((x - mu).T @ inv @ (x - mu) / -2.0) + return y + +mu = np.array([0.5, -0.2]) +cov = np.array([[2.0, 0.3], + [0.3, 0.5]]) + +x = y = np.arange(-5, 5, 0.1) +X, Y = np.meshgrid(x, y) +Z = np.zeros_like(X) + +for i in range(X.shape[0]): + for j in range(X.shape[1]): + x = np.array([X[i, j], Y[i, j]]) + Z[i, j] = multivariate_normal(x, mu, cov) + +fig = plt.figure() +ax1 = fig.add_subplot(1, 2, 1, projection='3d') +ax1.set_xlabel('x') +ax1.set_ylabel('y') +ax1.plot_surface(X, Y, Z, cmap='jet') + +ax2 = fig.add_subplot(1, 2, 2) +ax2.set_xlabel('x') +ax2.set_ylabel('y') +ax2.contour(X, Y, Z) +plt.show()