동일한 연구나 실험을 반복했을 때 일관된 결과가 나오는지의 여부.
데이터 분석을 반복하여 p-value를 인위적으로 낮추는 행위
여러 가설 검정을 시도할 때
유의미한 결과만 보고하고, 유의미하지 않은 결과는 보고하지 않는 행위

원하는 결과가 나올 떄까지 자료를 수집하는 것 경계

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
# 데이터 수집 예시
np.random.seed(42)
data = np.random.normal(0, 1, 1000)
sample_sizes = [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
p_values = []
for size in sample_sizes:
sample = np.random.choice(data, size)
_, p_value = stats.ttest_1samp(sample, 0)
p_values.append(p_value)
# p-값 시각화
plt.plot(sample_sizes, p_values, marker='o')
plt.axhline(y=0.05, color='red', linestyle='dashed', linewidth=1)
plt.title('p variation')
plt.xlabel('sample size')
plt.ylabel('p-value')
plt.show()

검증을 위한 데이터는 반드시 미리 따로 분리해 놓아야 함

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import *
# 데이터 생성
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
# 데이터 분할 (탐색용 80%, 검증용 20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 모델 학습
model = LinearRegression()
model.fit(X_train, y_train)
# 탐색용 데이터로 예측
y_train_pred = model.predict(X_train)
# 검증용 데이터로 예측
y_test_pred = model.predict(X_test)
# 탐색용 데이터 평가
train_mse = mean_squared_error(y_train, y_train_pred)
train_r2 = r2_score(y_train, y_train_pred)
print(f"탐색용 데이터 - MSE: {train_mse}, R2: {train_r2}")
# 검증용 데이터 평가
test_mse = mean_squared_error(y_test, y_test_pred)
test_r2 = r2_score(y_test, y_test_pred)
print(f"검증용 데이터 - MSE: {test_mse}, R2: {test_r2}")
결과:
탐색용 데이터 - MSE: 0.8476788564209705, R2: 0.7582381034538057
검증용 데이터 - MSE: 0.6536995137170021, R2: 0.8072059636181392