Pythonによる統計データの解析方法

ゼミで扱う予定です。

import random
import statistics
import numpy as np
from scipy import stats

# 1から100までの乱数を1000個生成
random_numbers = [random.randint(1, 100) for _ in range(1000)]

# 基本統計量の計算
mean = statistics.mean(random_numbers)
median = statistics.median(random_numbers)
variance = statistics.variance(random_numbers)
std_dev = statistics.stdev(random_numbers)
minimum = min(random_numbers)
maximum = max(random_numbers)

# 四分位数の計算
q1 = np.percentile(random_numbers, 25)
q3 = np.percentile(random_numbers, 75)
iqr = q3 - q1

# 歪度と尖度の計算
skewness = float(stats.skew(random_numbers))
kurtosis = float(stats.kurtosis(random_numbers))

print(f"生成された乱数: {random_numbers}")
print(f"データ数: {len(random_numbers)}")
print(f"\n基本統計量:")
print(f"平均値: {mean:.2f}")
print(f"中央値: {median:.2f}")
print(f"分散: {variance:.2f}")
print(f"標準偏差: {std_dev:.2f}")
print(f"最小値: {minimum}")
print(f"最大値: {maximum}")
print(f"\n四分位数:")
print(f"第1四分位数 (Q1): {q1:.2f}")
print(f"第3四分位数 (Q3): {q3:.2f}")
print(f"四分位範囲 (IQR): {iqr:.2f}")
print(f"\n分布の形状:")
print(f"歪度: {skewness:.2f}")
print(f"尖度: {kurtosis:.2f}")

1 Comment

Comments are closed