File size: 1,488 Bytes
9f312ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
import pandas as pd

class PredictionAnalyzer:
    def __init__(self, model, df_ready):
        self.model = model
        self.df_ready = df_ready

    def predictions_loop(self):
        pred_total = []
        for filter_cat in list(self.df_ready.columns):
            df_sample = self.df_ready[self.df_ready[filter_cat]==1]
            lb_sample = len(df_sample)/len(self.df_ready)
            X = df_sample.values
            X_test_tensor = torch.tensor(X, dtype=torch.float32)
            inputs = X_test_tensor
            outputs = self.model(inputs)
            outputs_show = outputs.detach().numpy().flatten()
            outputs_show[outputs_show > 0.55] = 1.0
            outputs_show[outputs_show < 0.45] = 0.0
            filtered_arr = outputs_show[(outputs_show == 0.0) | (outputs_show == 1.0)]
            unique, counts = np.unique(filtered_arr, return_counts=True)
            if len(counts) > 1:
                sum_cat = counts[0]+counts[1]
                pred_total.append([filter_cat, round(counts[0]/sum_cat*100, 2), round(counts[1]/sum_cat*100, 2), round(lb_sample*100, 2)])
            else:
                pred_total.append([filter_cat, 0, 0, round(lb_sample*100, 2)])
        
        pred_df_tot = pd.DataFrame(pred_total, columns=['Categoria', 'Pred==0', 'Pred==1', 'linea_base'])
        pred_df_tot_sort = pred_df_tot.sort_values(by='Pred==1', ascending=False, ignore_index=True)
        
        return pred_df_tot_sort