" 대푯값 "
어떤 대푯값을 보느냐에 따라 해석이 달라진다
사례
평균 = 30만원, 중앙값 = 5만원, 최빈값 = 9,900원일 때, 각 대푯값 활용 방안
왜곡을 없애려면 평균 + 중앙값 + 최빈값 세트로 봐야 함
보고서에는 평균, 중앙값, 최빈값 모두 명시해야 함
df['height_cm'].mean() # 평균
df['height_cm'].median() # 중앙값
df['height_cm'].mode() # 최빈값
# 데이터 로드 (CSV 파일 경로 수정 필요)
df = pd.read_excel("Online Retail.xlsx") # UCI 데이터는 xlsx 형식
# 결측치/이상치 제거
df = df.dropna(subset=["CustomerID"])
df = df[df["Quantity"] > 0]
df["Sales"] = df["Quantity"] * df["UnitPrice"]
# 고객별 결제 금액 집계
customer_sales = df.groupby("CustomerID")["Sales"].sum()
# 대표값 계산
mean_val = customer_sales.mean()
median_val = customer_sales.median()
mode_val = customer_sales.mode().iloc[0]
std_val = customer_sales.std()
print(f"평균: {mean_val:.2f}")
print(f"중앙값: {median_val:.2f}")
print(f"최빈값: {mode_val:.2f}")
print(f"표준편차: {std_val:.2f}")
# 히스토그램 (로그 스케일)
fig = plt.figure(figsize=(8,6), facecolor='k')
ax= fig.add_subplot()
ax.patch.set_facecolor('k')
plt.hist(customer_sales, bins=50, color="#7FFFD4")
plt.xscale("log")
plt.axvline(mean_val, color="#8A2BE2", linestyle="--", label=f"Mean={mean_val:.0f}")
plt.axvline(median_val, color="#FFD700", linestyle=":", label=f"Median={median_val:.0f}")
plt.axvline(mode_val, color="#FF4500", linestyle="-.", label=f"Mode={mode_val:.0f}")
plt.legend()
ax.set_title('Online Retail - Customer Purchase Distribution', color='w')
ax.set_xlabel('Customer Total Sales (log scale)', color='w')
ax.set_ylabel('Count', color='w')
ax.tick_params(colors='w')
ax.spines['bottom'].set_color('w')
ax.spines['left'].set_color('w')
plt.show()

" 데이터의 흩어짐 "
분산을 제곱으로 계산하는 이유
분산의 양의 제곱근
안정성과 위험을 보여주는 언어
평균은 분포의 위치를, 표준편차는 분포의 퍼짐을 결정함
데이터 값에 2씩 더하면?
데이터 값에 2씩 곱하면?

df['height_cm'].var() # 분산
df['height_cm'].std() # 표준편차
# 데이터 만든 부분
x = np.array([2, 4, 6, 8, 10], dtype=float)
plus_a = x + 10
times_k = x * 2
std_base = float(np.std(x, ddof=1))
std_plus_a = float(np.std(plus_a, ddof=1))
std_times_k = float(np.std(times_k, ddof=1))
df_transform = pd.DataFrame({
"dataset": ["base", "+a (a=10)", "×k (k=2)"],
"std": [std_base, std_plus_a, std_times_k],
"mean": [float(np.mean(x)), float(np.mean(plus_a)), float(np.mean(times_k))]
})
# Bar chart
fig = plt.figure(figsize=(8,6), facecolor='k')
ax= fig.add_subplot()
ax.patch.set_facecolor('k')
plt.bar(df_transform["dataset"], df_transform["std"], color = ['#7FFFD4', '#87CEFA', '#00BFFF'])
ax.set_title('Effect of transformations on standard deviation', color='w')
ax.set_ylabel('Standard deviation', color='w')
ax.tick_params(colors='w')
ax.spines['bottom'].set_color('w')
ax.spines['left'].set_color('w')
savefig("transform_std")
plt.show()

히스토그램 비교

같은 평균이어도 표준편차가 다르면 안정성 / 해석 / 의사결정이 완전히 달라짐
얍 1등