-
Notifications
You must be signed in to change notification settings - Fork 0
/
anova.py
146 lines (130 loc) · 6.22 KB
/
anova.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.2
# kernelspec:
# display_name: TFM
# language: python
# name: tfm
# ---
# %%
import statsmodels.api as sm
from statsmodels.formula.api import ols
import pandas as pd
numeric_level_list = True
df = pd.read_csv('all_data_2024-07-17/20240823-232405/all.csv').query('recall<0.99')
df['global_level_list'] = df['global_level_list'].apply(eval)
df['local_level_list'] = df['local_level_list'].apply(eval)
if numeric_level_list:
# df['global_level_list'] = df['global_level_list'].apply(lambda row: sum([2**i for i in row]))
# df['local_level_list'] = df['local_level_list'].apply(lambda row: sum([2**i for i in row]))
# df['global_level_list'] = df['global_level_list'].apply(lambda row: len(row))
# df['local_level_list'] = df['local_level_list'].apply(lambda row: len(row))
# Numero de puntos por cada uno
df['global_level_list'] = df['global_level_list'].apply(lambda row: sum([2**(max(row)-i) for i in row]))
df['local_level_list'] = df['local_level_list'].apply(lambda row: sum([2**(max(row)-i) for i in row]))
# df['global_level_list'] = df['global_level_list'].apply(lambda row: int(str(row).replace(",", "").replace(" ", "").replace("()", "0").replace("(", "").replace(")", "")))
# df['local_level_list'] = df['local_level_list'].apply(lambda row: int(str(row).replace(",", "").replace(" ", "").replace("()", "0").replace("(", "").replace(")", "")))
df_red = df.loc[:, (df != df.iloc[0]).any()][['use_wavelet', 'global_level_list', 'local_level_list', 'l1',
'l2', 'dropout', 'frac', 'F1', 'F1_val', ]]
df[[
'use_wavelet',
'binary_classification', 'global_level_list',
'local_level_list', 'l1', 'l2', 'dropout',
'frac', 'precision', 'recall', 'F1',
'Fβ', 'precision_val', 'recall_val', 'F1_val', 'Fβ_val',
]] .sort_values(by="F1_val", ascending=False).style.background_gradient(cmap='coolwarm_r').to_excel("all.xlsx", engine="openpyxl", index=False)
if numeric_level_list:
lm = ols('F1_val ~ use_wavelet + global_level_list + local_level_list + l1 + l2 + dropout + frac',
data=df_red).fit()
else:
lm = ols('F1_val ~ use_wavelet + C(global_level_list) + C(local_level_list) + l1 + l2 + dropout + frac',
data=df_red).fit()
table = sm.stats.anova_lm(lm, typ=2, robust="hc3") # Type 2 ANOVA DataFrame
print(lm.summary())
print(table.to_latex())
table
# %%
df.keys()
# %%
import matplotlib.pyplot as plt
num = df_red.assign(use_wavelet=df_red.use_wavelet.astype(int))
pd.plotting.scatter_matrix(num, alpha=0.2, figsize=(32, 18), hist_kwds={"bins": 50})
None
for key in num.keys():
if key == 'F1_val':
continue
plt.figure()
pd.plotting.scatter_matrix(num[['F1_val', key]])
# ax = num.plot.scatter(x=key, y='F1_val')
num
# %%
from IPython.display import display
display(df.query("F1_val < 0.8").describe())
display(df.query("F1_val >= 0.8").describe())
display(num.query("F1_val < 0.8").describe())
display(num.query("F1_val >= 0.8").describe())
# df.query("F1_val < 0.8").to_csv("menor.csv", index=False)
# df.query("F1_val >= 0.8").to_csv("mayor.csv", index=False)
# %%
lm.params.sort_values(ascending=False)
# pd.get_dummies(df_red, columns=["global_level_list", "local_level_list"])
# df_red.explode('global_level_list').explode('local_level_list')
df_red
from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer(sparse_output=True)
mlb = MultiLabelBinarizer()
df_red_oh = df_red.copy()
df_red_oh = df_red_oh.join(pd.DataFrame(mlb.fit_transform(df_red_oh.pop('global_level_list')),
columns=[f"global_{x}" for x in mlb.classes_],
index=df_red_oh.index))
df_red_oh = df_red_oh.join(pd.DataFrame(mlb.fit_transform(df_red_oh.pop('local_level_list')),
columns=[f"local_{x}" for x in mlb.classes_],
index=df_red_oh.index))
df_red_oh
categorical = False
if categorical:
lm = ols(f'F1_val ~ {"+".join([f"C({k})" for k in df_red_oh.keys() if k.startswith("global")])} + \
{"+".join([f"C({k})" for k in df_red_oh.keys() if k.startswith("local")])}',
data=df_red_oh).fit()
else:
lm = ols(f'F1_val ~ {"+".join([f"{k}" for k in df_red_oh.keys() if k.startswith("global")])} + \
{"+".join([f"{k}" for k in df_red_oh.keys() if k.startswith("local")])}',
data=df_red_oh).fit()
table = sm.stats.anova_lm(lm, typ=2, robust="hc3") # Type 2 ANOVA DataFrame
print(lm.summary())
table
# %%
df = pd.read_csv('all_data_2024-07-17/20240819-190659/all.csv')
print("\n".join([f"{k} = {v if k != 'wavelet_family' and not pd.isna(v) else repr(v) if k =='wavelet_family' else 'None'}" for k, v in df.sort_values(by="F1_val", ascending=False).iloc[0, 0:22].items()]))
# %%
import statsmodels.api as sm
from statsmodels.formula.api import ols
import pandas as pd
numeric_level_list = True
df = pd.read_csv('all_data_2024-07-17/20240819-190659/all.csv')
df['global_level_list'] = df['global_level_list'].apply(eval)
df['local_level_list'] = df['local_level_list'].apply(eval)
if numeric_level_list:
df['global_level_list'] = df['global_level_list'].apply(lambda row: sum([2**i for i in row]))
df['local_level_list'] = df['local_level_list'].apply(lambda row: sum([2**i for i in row]))
df_red = df.loc[:, (df != df.iloc[0]).any()][['use_wavelet', 'global_level_list', 'local_level_list', 'l1',
'l2', 'dropout', 'frac', 'F1', 'F1_val', ]]
if numeric_level_list:
lm = ols('F1_val ~ global_level_list + local_level_list + l1 + l2 + dropout + frac',
data=df_red).fit()
else:
lm = ols('F1_val ~ C(use_wavelet) + C(global_level_list) + C(local_level_list) + l1 + l2 + dropout + frac',
data=df_red).fit()
table = sm.stats.anova_lm(lm, typ=2, robust="hc3") # Type 2 ANOVA DataFrame
table
# %%
from scipy.stats import f_oneway
df = pd.read_csv('all_data_2024-07-17/20240823-232405/all.csv')
f_oneway(df.query("use_wavelet").F1_val.dropna().to_numpy(), df.query("~use_wavelet").F1_val.dropna().to_numpy())
df.query("use_wavelet").F1_val.dropna().describe(), df.query("~use_wavelet").F1_val.dropna().describe()