0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonとChatGPTでガウス分布を描いてみる

Last updated at Posted at 2023-05-05

確率分布の図を説明スライド用に用意したいと思って「ガウス分布 パワポ」などで検索した。複雑な方法でガウス分布を描いている人がたくさんいたが、google slidesで作れずexcelを開くのも面倒なのでChatGPTに聞いてみたら5分程度でいい感じの図が作成できたのでメモ。

とりあえずchatGPTを開いて

「make a code to visualize 2 gaussian curves that intersect」

と入力してみるとこんなコードが返ってきた。

import numpy as np
import matplotlib.pyplot as plt

# Define the parameters for the two Gaussians
mu1 = 2.5
sigma1 = 1.0
mu2 = 3.5
sigma2 = 0.8

# Create the x-axis values
x = np.linspace(-1, 8, 1000)

# Calculate the y-axis values for the two Gaussians
y1 = 1/(sigma1 * np.sqrt(2 * np.pi)) * np.exp( - (x - mu1)**2 / (2 * sigma1**2) )
y2 = 1/(sigma2 * np.sqrt(2 * np.pi)) * np.exp( - (x - mu2)**2 / (2 * sigma2**2) )

# Find the intersection point of the two curves
idx = np.argwhere(np.diff(np.sign(y1 - y2))).flatten()
intersection_x = x[idx]
intersection_y = y1[idx]

# Plot the two Gaussians and their intersection point
plt.plot(x, y1, label='Gaussian 1')
plt.plot(x, y2, label='Gaussian 2')
plt.plot(intersection_x, intersection_y, 'ro', markersize=10, label='Intersection')
plt.legend()
plt.show()

google colabで描画してみると中々いい感じと思えるプロットがでてきた。

gauss2_v1.png

ただ、ガウス分布の下の部分を塗りつぶしたいところ。通常だったらgoogleで検索するがここでもchatGPTに聞いてみる。

「can you fill under the curve of both curves and make the intersection darker」

するとこんなコードが返ってきた。

import numpy as np
import matplotlib.pyplot as plt

# Define the parameters for the two Gaussians
mu1 = 2.5
sigma1 = 1.0
mu2 = 3.5
sigma2 = 0.8

# Create the x-axis values
x = np.linspace(-1, 8, 1000)

# Calculate the y-axis values for the two Gaussians
y1 = 1/(sigma1 * np.sqrt(2 * np.pi)) * np.exp( - (x - mu1)**2 / (2 * sigma1**2) )
y2 = 1/(sigma2 * np.sqrt(2 * np.pi)) * np.exp( - (x - mu2)**2 / (2 * sigma2**2) )

# Find the intersection point of the two curves
idx = np.argwhere(np.diff(np.sign(y1 - y2))).flatten()
intersection_x = x[idx]
intersection_y = y1[idx]

# Plot the two Gaussians and their intersection point
plt.plot(x, y1, label='Gaussian 1', color='blue')
plt.plot(x, y2, label='Gaussian 2', color='green')

# Fill the area under the curves
plt.fill_between(x, 0, y1, alpha=0.2, color='blue')
plt.fill_between(x, 0, y2, alpha=0.2, color='green')

# Plot the intersection point with a darker color
plt.plot(intersection_x, intersection_y, 'ro', markersize=10, label='Intersection', color='red')

plt.legend()
plt.show()

実行してみると

gauss2_v2.png

ちゃんとできてる!

あとはintersectionが邪魔だったり、legendは必要ないので消してあげて、ガウス分布を整えてみる。

import numpy as np
import matplotlib.pyplot as plt

# Define the parameters for the two Gaussians
mu1 = 2.5
sigma1 = 1.0
mu2 = 4.
sigma2 = 1.0

# Create the x-axis values
x = np.linspace(-1, 8, 1000)

# Calculate the y-axis values for the two Gaussians
y1 = 1/(sigma1 * np.sqrt(2 * np.pi)) * np.exp( - (x - mu1)**2 / (2 * sigma1**2) )
y2 = 1/(sigma2 * np.sqrt(2 * np.pi)) * np.exp( - (x - mu2)**2 / (2 * sigma2**2) )

# Find the intersection point of the two curves
idx = np.argwhere(np.diff(np.sign(y1 - y2))).flatten()
intersection_x = x[idx]
intersection_y = y1[idx]

# Plot the two Gaussians and their intersection point
plt.plot(x, y1, label='Gaussian 1', color='blue')
plt.plot(x, y2, label='Gaussian 2', color='blue')

# Fill the area under the curves
plt.fill_between(x, 0, y1, alpha=0.2, color='blue')
plt.fill_between(x, 0, y2, alpha=0.2, color='blue')

# Plot the intersection point with a darker color
# plt.plot(intersection_x, intersection_y, 'ro', markersize=10, label='Intersection', color='red')

# plt.legend()
plt.show()

gauss2_v3.png

ほしいと思っていた図が完成しました!

ちょっとしたコードの作成にchatGPTはちょうどいいなと思いました。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?