jcmachicao
commited on
Commit
•
9f312ed
1
Parent(s):
f06e522
Upload prediction_analyzer.py
Browse files- prediction_analyzer.py +33 -0
prediction_analyzer.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
class PredictionAnalyzer:
|
6 |
+
def __init__(self, model, df_ready):
|
7 |
+
self.model = model
|
8 |
+
self.df_ready = df_ready
|
9 |
+
|
10 |
+
def predictions_loop(self):
|
11 |
+
pred_total = []
|
12 |
+
for filter_cat in list(self.df_ready.columns):
|
13 |
+
df_sample = self.df_ready[self.df_ready[filter_cat]==1]
|
14 |
+
lb_sample = len(df_sample)/len(self.df_ready)
|
15 |
+
X = df_sample.values
|
16 |
+
X_test_tensor = torch.tensor(X, dtype=torch.float32)
|
17 |
+
inputs = X_test_tensor
|
18 |
+
outputs = self.model(inputs)
|
19 |
+
outputs_show = outputs.detach().numpy().flatten()
|
20 |
+
outputs_show[outputs_show > 0.55] = 1.0
|
21 |
+
outputs_show[outputs_show < 0.45] = 0.0
|
22 |
+
filtered_arr = outputs_show[(outputs_show == 0.0) | (outputs_show == 1.0)]
|
23 |
+
unique, counts = np.unique(filtered_arr, return_counts=True)
|
24 |
+
if len(counts) > 1:
|
25 |
+
sum_cat = counts[0]+counts[1]
|
26 |
+
pred_total.append([filter_cat, round(counts[0]/sum_cat*100, 2), round(counts[1]/sum_cat*100, 2), round(lb_sample*100, 2)])
|
27 |
+
else:
|
28 |
+
pred_total.append([filter_cat, 0, 0, round(lb_sample*100, 2)])
|
29 |
+
|
30 |
+
pred_df_tot = pd.DataFrame(pred_total, columns=['Categoria', 'Pred==0', 'Pred==1', 'linea_base'])
|
31 |
+
pred_df_tot_sort = pred_df_tot.sort_values(by='Pred==1', ascending=False, ignore_index=True)
|
32 |
+
|
33 |
+
return pred_df_tot_sort
|