improving plots debug

This commit is contained in:
Alejandro Moreo Fernandez 2024-10-16 17:44:59 +02:00
parent cdf0200430
commit 9aecdad66f
1 changed files with 66 additions and 7 deletions

View File

@ -28,7 +28,7 @@ def plot(xaxis, metrics_measurements, metrics_names, suffix):
fig, ax1 = plt.subplots(figsize=(8, 6)) fig, ax1 = plt.subplots(figsize=(8, 6))
def add_plot(ax, mean_error, std_error, name, color, marker): def add_plot(ax, mean_error, std_error, name, color, marker):
ax.plot(xaxis, mean_error, label=name, marker=marker, color=color) ax.plot(xaxis, mean_error, label=name, marker=marker, color=color, markersize=3)
if std_error is not None: if std_error is not None:
ax.fill_between(xaxis, mean_error - std_error, mean_error + std_error, color=color, alpha=0.2) ax.fill_between(xaxis, mean_error - std_error, mean_error + std_error, color=color, alpha=0.2)
@ -74,6 +74,56 @@ def plot(xaxis, metrics_measurements, metrics_names, suffix):
plt.close() plt.close()
def plot_stack(xaxis, metrics_measurements, metrics_names, suffix):
# Crear la figura y los ejes (4 bloques verticales)
fig, axs = plt.subplots(4, 1, figsize=(8, 12))
x = xaxis
indexes = np.arange(len(metrics_measurements))
axs_idx = 0
# colors = ['b', 'g', 'r', 'c', 'purple']
for m_te, m_tr in zip(indexes[:-1:2], indexes[1::2]):
metric_te, metric_tr = metrics_measurements[m_te], metrics_measurements[m_tr]
metric_te_name, metric_tr_name = metrics_names[m_te], metrics_names[m_tr]
metric_mean_tr = np.mean(metric_tr, axis=0)
metric_std_tr = np.std(metric_tr, axis=0)
metric_mean_te = np.mean(metric_te, axis=0)
metric_std_te = np.std(metric_te, axis=0)
axs[axs_idx].plot(xaxis, metric_mean_tr, label=metric_tr_name, marker='o', color='r', markersize=3)
axs[axs_idx].fill_between(xaxis, metric_mean_tr - metric_std_tr, metric_mean_tr + metric_std_tr, color='r', alpha=0.2)
minx = np.argmin(metric_mean_tr)
axs[axs_idx].axvline(xaxis[minx], color='r', linestyle='--', linewidth=1)
axs[axs_idx].plot(xaxis, metric_mean_te, label=metric_te_name, marker='o', color='b', markersize=3)
axs[axs_idx].fill_between(xaxis, metric_mean_te - metric_std_te, metric_mean_te + metric_std_te, color='b', alpha=0.2)
minx = np.argmin(metric_mean_te)
axs[axs_idx].axvline(xaxis[minx], color='b', linestyle='--', linewidth=1)
# axs[axs_idx].set_title(f'{metric_te_name} and {metric_tr_name}')
axs[axs_idx].legend(loc='lower right')
if axs_idx < len(indexes)//2 -1:
axs[axs_idx].set_xticks([])
axs_idx += 1
# Ajustar el espaciado entre los subplots
plt.tight_layout()
# Mostrar el gráfico
# Mostrar el gráfico
# plt.title(dataset)
# plt.show()
os.makedirs('./plots/likelihood/', exist_ok=True)
plt.savefig(f'./plots/likelihood/{dataset}-fig{suffix}.png')
plt.close()
def generate_data(from_train=False): def generate_data(from_train=False):
data = qp.datasets.fetch_UCIMulticlassDataset(dataset) data = qp.datasets.fetch_UCIMulticlassDataset(dataset)
n_classes = data.n_classes n_classes = data.n_classes
@ -110,7 +160,7 @@ def generate_data(from_train=False):
likelihood_value = [] likelihood_value = []
# for bandwidth in np.linspace(0.01, 0.2, 50): # for bandwidth in np.linspace(0.01, 0.2, 50):
for bandwidth in np.logspace(-5, np.log10(0.2), 50): for bandwidth in np.logspace(-4, np.log10(0.2), 50):
mix_densities = kde.get_mixture_components(tr_posteriors, tr_y, classes, bandwidth) mix_densities = kde.get_mixture_components(tr_posteriors, tr_y, classes, bandwidth)
test_densities = [kde.pdf(kde_i, te_posteriors) for kde_i in mix_densities] test_densities = [kde.pdf(kde_i, te_posteriors) for kde_i in mix_densities]
@ -172,16 +222,24 @@ for i, dataset in enumerate(tqdm(DATASETS, desc='processing datasets', total=len
measurement_names = [] measurement_names = []
if show_ae: if show_ae:
measurements.append(AE_error_te) measurements.append(AE_error_te)
measurement_names.append('AE') measurement_names.append('AE(te)')
measurements.append(AE_error_tr)
measurement_names.append('AE(tr)')
if show_rae: if show_rae:
measurements.append(RAE_error_te) measurements.append(RAE_error_te)
measurement_names.append('RAE') measurement_names.append('RAE(te)')
measurements.append(RAE_error_tr)
measurement_names.append('RAE(tr)')
if show_kld: if show_kld:
measurements.append(KLD_error_te) measurements.append(KLD_error_te)
measurement_names.append('KLD') measurement_names.append('KLD(te)')
measurements.append(KLD_error_tr)
measurement_names.append('KLD(tr)')
if show_mse: if show_mse:
measurements.append(MSE_error_te) measurements.append(MSE_error_te)
measurement_names.append('MSE') measurement_names.append('MSE(te)')
measurements.append(MSE_error_tr)
measurement_names.append('MSE(tr)')
measurements.append(normalize_metric(LIKE_value_te)) measurements.append(normalize_metric(LIKE_value_te))
measurements.append(normalize_metric(LIKE_value_tr)) measurements.append(normalize_metric(LIKE_value_tr))
measurement_names.append('NLL(te)') measurement_names.append('NLL(te)')
@ -200,7 +258,8 @@ for i, dataset in enumerate(tqdm(DATASETS, desc='processing datasets', total=len
# measurements.append(normalize_metric(LIKE_value_tr)) # measurements.append(normalize_metric(LIKE_value_tr))
# measurement_names.append('NLL(te)') # measurement_names.append('NLL(te)')
# measurement_names.append('NLL(tr)') # measurement_names.append('NLL(tr)')
plot(xaxis, measurements, measurement_names, suffix='AVEtr') # plot(xaxis, measurements, measurement_names, suffix='AVEtr')
plot_stack(xaxis, measurements, measurement_names, suffix='AVEtr')