data1.csv

data2.csv

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

df = pd.read_csv('data1.csv', index_col = 0)
ts = pd.read_csv('data2.csv', index_col = 0)
# 기술통계표
desc_numeric = df[["height_cm", "weight_kg", "spend_10k"]].describe()   # 수치형-연속형
desc_categorical = df[["gender", "pay_method"]].apply(lambda x: x.value_counts())   # 수치형-이산형
# 도수분포표 (height_cm, 5cm bin) & 히스토그램
import numpy as np
from scipy.stats import gaussian_kde

# 히스토그램 그리기 (count 기준)
hist = sns.histplot(data=df, x="height_cm", bins=15, color="#4f85c3", kde=False)

# KDE 곡선 계산 (bw_method, cut 조정 가능)
data = df["height_cm"].dropna()
kde = gaussian_kde(data, bw_method=0.3)

# KDE 평가할 x축 값 범위 지정 (히스토그램 범위 내)
x_min, x_max = data.min(), data.max()
x_eval = np.linspace(x_min, x_max, 200)

# KDE y값 계산
kde_y = kde(x_eval)

# 히스토그램 바 너비
bins = hist.patches
bin_width = bins[0].get_width()

# KDE y축 스케일 조정: 밀도 × (샘플수 × bin 너비)a
kde_y_scaled = kde_y * len(data) * bin_width

# KDE 곡선 플롯
plt.plot(x_eval, kde_y_scaled, color='black', linewidth=2, linestyle='--')

plt.xlabel("Height (cm)")
plt.show()

image.png

✅ 히스토그램

🔸 plt.hist()

plt.figure()
plt.hist(df["height_cm"], bins=9, edgecolor="black", color="#4f85c3")
plt.title("Histogram of Height (cm)")
plt.xlabel("Height (cm)")
plt.ylabel("Count")
plt.show()

image.png

🔎 주요 키워드