1
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?

Python + NumPy でランダムデータを生成して可視化する方法

Last updated at Posted at 2025-09-24

Python + NumPy でランダムデータを生成して可視化する方法

学校の研究課題(?)でちょっと必要になったので書いてみました
データのランダム生成に使ってます


目次

  1. はじめに

  2. NumPyでランダムデータを生成する

    • 一様分布
    • 正規分布
    • 整数乱数
    • シード値で再現性を確保
  3. matplotlibでデータを可視化する

    • ヒストグラム
    • 散布図
    • 線グラフ
  4. 応用例

    • 複数分布の比較
    • 3D散布図
    • ゲームやシミュレーションでの活用
  5. まとめ


1. はじめに

データ分析や機械学習、シミュレーションなどで「ランダムデータ」が必要になる場面は多いです。
Pythonの NumPy は数値計算ライブラリとして非常に高速で便利であり、簡単にランダムデータを生成できます。
さらに matplotlib を使えば、生成したデータを直感的に可視化することが可能です。

この記事では、コード例とともに解説します。


2. NumPyでランダムデータを生成する

2-1. 一様分布

import numpy as np

# 0以上1未満の乱数を10個生成
random_floats = np.random.rand(10)
print("乱数 (0~1):", random_floats)

解説

  • np.random.rand(n)[0,1) の範囲で n 個の乱数を生成します
  • 数値の偏りがない均等な分布(=一様分布)

2-2. 正規分布

# 平均0、標準偏差1の正規分布乱数を10個生成
random_normals = np.random.randn(10)
print("正規分布乱数:", random_normals)

解説

  • np.random.randn(n) は平均0、標準偏差1の正規分布に従う乱数を生成
  • 中央付近に値が集まり、両端は少ない分布(ベル型カーブ)

2-3. 整数乱数

# 0から9までの整数を10個ランダム生成
random_integers = np.random.randint(0, 10, 10)
print("整数乱数:", random_integers)

解説

  • np.random.randint(low, high, size)

    • low 以上、high 未満の整数を生成
    • 例: 0~9 の整数乱数10個

2-4. シード値で再現性を確保

np.random.seed(42)  # シード値を固定
print(np.random.rand(5))

解説

  • 同じシード値を設定すると、毎回同じ乱数列が生成されます
  • 実験や検証で結果を再現したいときに便利

3. matplotlibでデータを可視化する

3-1. 柱状グラフ(ヒストグラム)

import matplotlib.pyplot as plt

data = np.random.randn(1000)  # 正規分布乱数1000個
plt.hist(data, bins=30, color='skyblue', edgecolor='black')
plt.title("正規分布のヒストグラム")
plt.xlabel("")
plt.ylabel("出現回数")
plt.show()

ポイント

  • plt.hist() でデータの分布を視覚化
  • bins → データを分割する区間の数
  • ヒストグラムは正規分布や偏りを確認するのに便利

3-2. 散布図

x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y, color='orange')
plt.title("ランダム散布図")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

ポイント

  • plt.scatter() は2次元データの相関や分布を可視化
  • データがどのように広がっているかを直感的に理解可能

3-3. 線グラフ(ランダムウォーク)

y = np.random.randn(100).cumsum()  # 累積和でランダムウォーク
plt.plot(y, color='green')
plt.title("ランダムウォーク")
plt.xlabel("ステップ")
plt.ylabel("")
plt.show()

ポイント

  • ランダムウォークは金融や物理のシミュレーションでよく使われます
  • cumsum() で累積和を計算し、時間変化を表現

4. 応用例

  1. 複数分布の比較

    plt.hist(np.random.randn(1000), bins=30, alpha=0.5, label='正規分布1')
    plt.hist(np.random.randn(1000)+1, bins=30, alpha=0.5, label='正規分布2')
    plt.legend()
    plt.show()
    
  2. 3D散布図

    from mpl_toolkits.mplot3d import Axes3D
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(np.random.rand(50), np.random.rand(50), np.random.rand(50))
    plt.show()
    
  3. ゲームやシミュレーション

    • マップ生成、敵のランダム配置、シミュレーションのランダム要素などに応用可能

5. まとめ

  • NumPyで乱数生成は簡単で高速
  • matplotlibと組み合わせることでデータを直感的に理解可能
  • ヒストグラム、散布図、線グラフを駆使して分析や可視化を行える
  • データ分析、シミュレーション、ゲーム制作など幅広く応用可能

1
0
0

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
1
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?