-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEDA_Table.py
More file actions
64 lines (55 loc) · 2.22 KB
/
EDA_Table.py
File metadata and controls
64 lines (55 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Goal: Test bivariate relationships between predictors and targets
import pandas as pd
from scipy.stats import fisher_exact, mannwhitneyu, spearmanr
# Merge data
df = pd.merge(X_num_train_full, df_cat, on='id')
df = pd.merge(df, y_train_full, on='id')
# Define outcomes
outcomes = ['Outcome_1', 'Outcome_2']
# Store results
results = []
# Loop
for outcome in outcomes:
for var in df.columns[1:-2]: # Assume last 2 columns are outcomes
if df[var].dtype.kind in 'iufc': # i: int, u: unsigned int, f: float, c: complex
# Continuous predictor
group0 = df[df[outcome] == 0][var]
group1 = df[df[outcome] == 1][var]
# Check if both groups have enough observations
if len(group0) > 0 and len(group1) > 0:
try:
# Mann-Whitney U
_, p_mw = mannwhitneyu(group0, group1, alternative='two-sided', nan_policy='omit')
# Spearman correlation
corr, p_corr = spearmanr(df[var], df[outcome], nan_policy='omit')
test_used = "Mann-Whitney U + Spearman"
p_value = min(p_mw, p_corr)
except Exception as e:
test_used = "Mann-Whitney U + Spearman (failed)"
p_value = np.nan
else:
test_used = "Mann-Whitney U + Spearman (insufficient data)"
p_value = np.nan
else:
# Categorical predictor
try:
contingency_table = pd.crosstab(df[var], df[outcome])
# Fisher's Exact Test
_, p_value = fisher_exact(contingency_table)
test_used = "Fisher's Exact Test"
except Exception as e:
test_used = "Categorical Test (failed)"
p_value = np.nan
results.append({
'Outcome': outcome,
'Predictor': var,
'Test Used': test_used,
'P-Value': p_value
})
# Results table
results_df = pd.DataFrame(results)
results_df
from datetime import datetime
time_str = datetime.today().strftime('%Y%m%d')
filename = f"{time_str}_FeatureSelection_Update.csv"
results_df.to_csv(filename, index=False)