This commit is contained in:
Alejandro Moreo Fernandez 2026-01-13 14:36:20 +01:00
parent ce51766944
commit 934b09fa66
1 changed files with 27 additions and 18 deletions

View File

@ -1,4 +1,5 @@
from collections import defaultdict
import pandas as pd
import model_selection
import quapy as qp
@ -33,15 +34,17 @@ def methods():
kdey_hyper_clr = {'bandwidth': [0.05, 0.1, 0.5, 1., 2., 5.]}
yield 'BayesianACC', ACC(LR()), acc_hyper, lambda hyper: BayesianCC(LR(), mcmc_seed=0, prior='uniform')
# yield f'BaKDE-Ait', KDEyCLR(LR()), kdey_hyper_clr, lambda hyper: BayesianKDEy(kernel='aitchison', mcmc_seed=0, engine='numpyro', temperature=None, prior='uniform', **hyper)
yield f'BaKDE-Ait', KDEyCLR(LR()), kdey_hyper_clr, lambda hyper: BayesianKDEy(kernel='aitchison', mcmc_seed=0, engine='numpyro', temperature=None, prior='uniform', **hyper)
def run_test(test, alpha_test, alpha_train, concentration, prior_type, bay_quant, train_prev, results):
def run_test(test, alpha_test, alpha_train, concentration, prior_type, bay_quant, train_prev, dataset_name, method_name, results):
test_generator = DirichletProtocol(test, alpha=alpha_test, repeats=100, random_state=0)
for i, (sample_X, true_prev) in tqdm(enumerate(test_generator()), total=test_generator.total(),
desc=f'{method_name} informative alpha with {concentration=}'):
desc=f'{method_name} {prior_type} alpha with {concentration=}'):
estim_prev, region = bay_quant.predict_conf(sample_X)
results['dataset'].append(dataset_name)
results['method_name'].append(method_name)
results['prior-type'].append(prior_type)
results['train-prev'].append(train_prev)
results['concentration'].append(concentration)
@ -58,7 +61,15 @@ def run_test(test, alpha_test, alpha_train, concentration, prior_type, bay_quant
results['samples'].append(region.samples)
def experiment(dataset: Dataset, point_quantifier: AggregativeQuantifier, grid: dict, bay_constructor, hyper_choice_path: Path):
def experiment(dataset: Dataset,
dataset_name: str,
point_quantifier: AggregativeQuantifier,
grid: dict,
bay_constructor,
method_name:str,
hyper_choice_path: Path):
with qp.util.temp_seed(0):
training, test = dataset.train_test
@ -86,20 +97,14 @@ def experiment(dataset: Dataset, point_quantifier: AggregativeQuantifier, grid:
# informative prior
alpha_test_informative = alpha_train
prior_type = 'informative'
run_test(test, alpha_test_informative, alpha_train, concentration, prior_type, bay_quant, train_prev, results)
run_test(test, alpha_test_informative, alpha_train, concentration, prior_type, bay_quant, train_prev, dataset_name, method_name, results)
# informative prior
alpha_test_wrong = antagonistic_prevalence(train_prev, strength=1) * concentration
prior_type = 'wrong'
run_test(test, alpha_test_wrong, alpha_train, concentration, prior_type, bay_quant, train_prev, results)
run_test(test, alpha_test_wrong, alpha_train, concentration, prior_type, bay_quant, train_prev, dataset_name, method_name, results)
report = {
'optim_hyper': best_hyperparams,
'train-prev': train_prev,
'results': {k: np.asarray(v) for k, v in results.items()}
}
return report
return results
if __name__ == '__main__':
@ -107,6 +112,7 @@ if __name__ == '__main__':
selected = select_imbalanced_datasets()
print(f'selected datasets={selected}')
qp.environ['SAMPLE_SIZE'] = multiclass['sample_size']
reports = []
for data_name in selected:
data = multiclass['fetch_fn'](data_name)
for method_name, surrogate_quant, hyper_params, bay_constructor in methods():
@ -114,8 +120,11 @@ if __name__ == '__main__':
hyper_path = experiment_path(result_dir/'hyperparams', data_name, surrogate_quant.__class__.__name__)
print(f'Launching {method_name} in dataset {data_name}')
experiment(dataset=data,
point_quantifier=surrogate_quant,
grid=hyper_params,
bay_constructor=bay_constructor,
hyper_choice_path=hyper_path)
report = qp.util.pickled_resource(
result_path, experiment, data, data_name, surrogate_quant, hyper_params, bay_constructor, method_name, hyper_path
)
reports.append(report)
# df = pd.DataFrame(results)