-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path01.feat_mi.py
152 lines (145 loc) · 4.72 KB
/
01.feat_mi.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#%% imports and definitions
import itertools as itt
import os
import numpy as np
import pandas as pd
import plotly.express as px
from plotly.express.colors import qualitative
from tqdm.auto import tqdm
from routine.clustering import Kmeans_cluster
from routine.data_generation import generate_data
from routine.lnc import MI
from routine.plotting import imshow
FIG_PATH = "./figs/feat_mi"
PARAM_DATA = {
"num_users": 1000,
"num_campaigns": 100,
"samples_per_campaign": 100,
"num_cohort": 10,
"even_cohort": True,
"response_sig_a": 10,
"cross_weight": None,
"magnify_hf": 1,
}
PARAM_FONT_SZ = {"font_size": 16, "title_font_size": 24, "legend_title_font_size": 24}
PARAM_VAR = np.around(np.linspace(0.05, 0.6, 12), 2)
PARAM_NREPEAT = 30
OUT_PATH = "./intermediate/feat_mi"
os.makedirs(FIG_PATH, exist_ok=True)
os.makedirs(OUT_PATH, exist_ok=True)
#%% generate data with uniform variance
mi_df = []
for cvar in tqdm(PARAM_VAR):
for irpt in range(PARAM_NREPEAT):
obs_df, user_df, camp_df = generate_data(cohort_variances=cvar, **PARAM_DATA)
for featA, featB in itt.combinations_with_replacement(
["cohort", "user_f0", "user_f1", "user_fh"], 2
):
mi = MI.mi_LNC(user_df[[featA, featB]].values.T)
mi_df.append(
pd.Series(
{
"featA": featA,
"featB": featB,
"mi": mi,
"cohort_var": cvar,
"irpt": irpt,
}
)
.to_frame()
.T
)
mi_df = pd.concat(mi_df, ignore_index=True)
mi_df.to_csv(os.path.join(OUT_PATH, "mi_df_uniform.csv"), index=False)
#%% plot mi matrix
mi_df = pd.read_csv(os.path.join(OUT_PATH, "mi_df_uniform.csv"))
mi_df = mi_df.groupby(["featA", "featB", "cohort_var"])["mi"].mean().reset_index()
fig = imshow(
mi_df,
facet_row=None,
facet_col="cohort_var",
subplot_args={"col_wrap": 6, "horizontal_spacing": 0.05},
x="featA",
y="featB",
z="mi",
coloraxis="coloraxis",
text="mi",
texttemplate="%{text:.1f}",
)
fig.update_layout(
{"coloraxis": {"cmin": 0, "cmax": 3, "colorscale": "viridis"}}, **PARAM_FONT_SZ
)
fig.write_html(os.path.join(FIG_PATH, "mi_matrix.html"))
#%% plot fh mi bar
mi_df = pd.read_csv(os.path.join(OUT_PATH, "mi_df_uniform.csv"))
mi_df = (
mi_df[(mi_df["featB"] == "user_fh") & (mi_df["featA"] != "user_fh")]
.groupby(["featA", "featB", "cohort_var"])["mi"]
.mean()
.reset_index()
)
fig = px.bar(mi_df, x="cohort_var", y="mi", color="featA")
fig.update_layout(
legend_title="Variable",
xaxis_title="Cohort Variance",
yaxis_title="Mutual Information<br>Shared with Hidden Feature",
**PARAM_FONT_SZ
)
fig.write_html(os.path.join(FIG_PATH, "mi_fh_bar.html"))
#%% generate data with clustering
mi_df = []
for cvar in tqdm(PARAM_VAR):
for irpt in range(PARAM_NREPEAT):
obs_df, user_df, camp_df = generate_data(
cohort_variances=np.array([cvar, cvar, 0.2]), **PARAM_DATA
)
user_df = user_df.rename(columns={"cohort": "cohort_real"})
user_df["cohort_cluster"] = Kmeans_cluster(
user_df[["user_f0", "user_f1"]], PARAM_DATA["num_cohort"]
)
for featA, featB in itt.combinations_with_replacement(
["cohort_real", "cohort_cluster", "user_f0", "user_f1", "user_fh"], 2
):
mi = MI.mi_LNC(user_df[[featA, featB]].values.T)
mi_df.append(
pd.Series(
{
"featA": featA,
"featB": featB,
"mi": mi,
"cohort_var": cvar,
"irpt": irpt,
}
)
.to_frame()
.T
)
mi_df = pd.concat(mi_df, ignore_index=True)
mi_df.to_csv(os.path.join(OUT_PATH, "mi_df_cluster.csv"), index=False)
#%% plot fh mi bar
mi_df = pd.read_csv(os.path.join(OUT_PATH, "mi_df_cluster.csv"))
mi_df = (
mi_df[(mi_df["featB"] == "user_fh") & (mi_df["featA"] != "user_fh")]
.groupby(["featA", "featB", "cohort_var"])["mi"]
.mean()
.reset_index()
)
fig = px.bar(
mi_df,
x="cohort_var",
y="mi",
color="featA",
color_discrete_map={
"user_f0": qualitative.Plotly[2],
"cohort_real": qualitative.Plotly[1],
"cohort_cluster": qualitative.Plotly[3],
"user_f1": qualitative.Plotly[0],
},
)
fig.update_layout(
legend_title="Variable",
xaxis_title="Visible Feature Variance",
yaxis_title="Mutual Information<br>Shared with Hidden Feature",
**PARAM_FONT_SZ
)
fig.write_html(os.path.join(FIG_PATH, "mi_fh_bar_cluster.html"))