Pandas サンプリングとランダムデータ
Pandas は豊富なランダムサンプリング機能を提供しており、データセットから要件に応じてランダムにサンプルを選択でき、ランダムデータの生成もサポートしています。
ランダムサンプリング
sample メソッド
例
import pandas as pd
import numpy as np
# 大きなデータセットを作成
df = pd.DataFrame({
"ID": range(1, 1001),
"値": np.random.randn(1000)
})
# 5行をランダムに抽出
print("ランダムに5行抽出:")
print(df.sample(5))
print()
# データの30%を抽出
print("ランダムに30%抽出:")
print(df.sample(frac=0.3))
print()
# 復元抽出(重複あり)
print("復元抽出で5行:")
print(df.sample(5, replace=True))
import numpy as np
# 大きなデータセットを作成
df = pd.DataFrame({
"ID": range(1, 1001),
"値": np.random.randn(1000)
})
# 5行をランダムに抽出
print("ランダムに5行抽出:")
print(df.sample(5))
print()
# データの30%を抽出
print("ランダムに30%抽出:")
print(df.sample(frac=0.3))
print()
# 復元抽出(重複あり)
print("復元抽出で5行:")
print(df.sample(5, replace=True))
乱数シードの設定
例
import pandas as pd
import numpy as np
# 乱数シードを設定し、結果を再現可能にする
np.random.seed(42)
df = pd.DataFrame({
"ID": range(1, 11),
"値": np.random.randn(10)
})
print("乱数シード42を使用:")
print(df.sample(3))
# 同じシードで再度実行すると、同じ結果が得られる
print("\n同じシードで再度実行:")
np.random.seed(42)
print(df.sample(3))
import numpy as np
# 乱数シードを設定し、結果を再現可能にする
np.random.seed(42)
df = pd.DataFrame({
"ID": range(1, 11),
"値": np.random.randn(10)
})
print("乱数シード42を使用:")
print(df.sample(3))
# 同じシードで再度実行すると、同じ結果が得られる
print("\n同じシードで再度実行:")
np.random.seed(42)
print(df.sample(3))
ランダムデータの生成
例
import pandas as pd
import numpy as np
# ランダムな整数を生成
print("ランダムな整数 [0, 10):")
print(pd.Series(np.random.randint(0, 10, 5)))
print()
# ランダムな浮動小数点数を生成
print("ランダムな浮動小数点数 [0, 1):")
print(pd.Series(np.random.random(5)))
print()
# 正規分布のランダム数を生成
print("正規分布 N(0, 1):")
print(pd.Series(np.random.randn(5)))
print()
# 平均と標準偏差を指定
print("正規分布 N(10, 2):")
print(pd.Series(np.random.normal(10, 2, 5)))
print()
# ランダムに選択
choices = ["A", "B", "C", "D"]
print("ランダム選択:")
print(pd.Series(np.random.choice(choices, 10)))
import numpy as np
# ランダムな整数を生成
print("ランダムな整数 [0, 10):")
print(pd.Series(np.random.randint(0, 10, 5)))
print()
# ランダムな浮動小数点数を生成
print("ランダムな浮動小数点数 [0, 1):")
print(pd.Series(np.random.random(5)))
print()
# 正規分布のランダム数を生成
print("正規分布 N(0, 1):")
print(pd.Series(np.random.randn(5)))
print()
# 平均と標準偏差を指定
print("正規分布 N(10, 2):")
print(pd.Series(np.random.normal(10, 2, 5)))
print()
# ランダムに選択
choices = ["A", "B", "C", "D"]
print("ランダム選択:")
print(pd.Series(np.random.choice(choices, 10)))
トレーニング/テストセットの分割
例
import pandas as pd
import numpy as np
# 模擬データセット
df = pd.DataFrame({
"特徴1": np.random.randn(100),
"特徴2": np.random.randn(100),
"ターゲット": np.random.choice([0, 1], 100)
})
# トレーニングセットとテストセットに分割 (80% / 20%)
train = df.sample(frac=0.8, random_state=42)
test = df.drop(train.index)
print(f"トレーニングセット: {len(train)} 行")
print(f"テストセット: {len(test)} 行")
print()
# 層化抽出(目的変数の比率を維持)
print("層化抽出:")
from sklearn.model_selection import train_test_split
# scikit-learn のインストールが必要: pip install scikit-learn
# train, test = train_test_split(df, test_size=0.2, stratify=df["ターゲット"])
import numpy as np
# 模擬データセット
df = pd.DataFrame({
"特徴1": np.random.randn(100),
"特徴2": np.random.randn(100),
"ターゲット": np.random.choice([0, 1], 100)
})
# トレーニングセットとテストセットに分割 (80% / 20%)
train = df.sample(frac=0.8, random_state=42)
test = df.drop(train.index)
print(f"トレーニングセット: {len(train)} 行")
print(f"テストセット: {len(test)} 行")
print()
# 層化抽出(目的変数の比率を維持)
print("層化抽出:")
from sklearn.model_selection import train_test_split
# scikit-learn のインストールが必要: pip install scikit-learn
# train, test = train_test_split(df, test_size=0.2, stratify=df["ターゲット"])