virgo-modules 0.5.1__py3-none-any.whl → 0.6.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of virgo-modules might be problematic. Click here for more details.
- virgo_modules/src/edge_utils/conformal_utils.py +67 -32
- virgo_modules/src/edge_utils/shap_utils.py +23 -52
- {virgo_modules-0.5.1.dist-info → virgo_modules-0.6.1.dist-info}/METADATA +1 -1
- {virgo_modules-0.5.1.dist-info → virgo_modules-0.6.1.dist-info}/RECORD +7 -7
- {virgo_modules-0.5.1.dist-info → virgo_modules-0.6.1.dist-info}/LICENSE +0 -0
- {virgo_modules-0.5.1.dist-info → virgo_modules-0.6.1.dist-info}/WHEEL +0 -0
- {virgo_modules-0.5.1.dist-info → virgo_modules-0.6.1.dist-info}/top_level.txt +0 -0
|
@@ -1,44 +1,79 @@
|
|
|
1
1
|
from plotly.subplots import make_subplots
|
|
2
2
|
import plotly.graph_objects as go
|
|
3
|
-
from mapie.classification import MapieClassifier
|
|
4
3
|
from sklearn.pipeline import Pipeline
|
|
5
4
|
import mlflow
|
|
5
|
+
import pandas as pd
|
|
6
6
|
import numpy as np
|
|
7
|
+
from sklearn.base import BaseEstimator, ClassifierMixin
|
|
8
|
+
from mapie.classification import SplitConformalClassifier
|
|
7
9
|
|
|
10
|
+
class ConformalStack(mlflow.pyfunc.PythonModel):
|
|
11
|
+
def __init__(self, model,targets, alphas):
|
|
12
|
+
self.model = model
|
|
13
|
+
self.targets = targets
|
|
14
|
+
self.alphas = alphas
|
|
15
|
+
def fit(self, data):
|
|
16
|
+
self.classifiers = dict()
|
|
17
|
+
for i,target in enumerate(self.targets):
|
|
18
|
+
st = SingleStack(self.model["model"],i)
|
|
19
|
+
st.fit()
|
|
20
|
+
seg_model = Pipeline([
|
|
21
|
+
('pipe',self.model['pipe_transform']),
|
|
22
|
+
('modelbase',st)
|
|
23
|
+
])
|
|
24
|
+
mapie_class = SplitConformalClassifier(seg_model, prefit=True, random_state=123, conformity_score="lac", confidence_level=1-np.array(self.alphas))
|
|
25
|
+
mapie_class.conformalize(data, data[self.targets[i]].values)
|
|
26
|
+
self.classifiers[target] = mapie_class
|
|
27
|
+
def predict_conformal(self, data, ):
|
|
28
|
+
for target in self.targets:
|
|
29
|
+
prefix = target+"_conf"
|
|
30
|
+
_, y_pis = self.classifiers[target].predict_set(data)
|
|
31
|
+
for i,alpha in enumerate(self.alphas):
|
|
32
|
+
data[f'{prefix}-{alpha}'] = y_pis[:,1,i]
|
|
33
|
+
data[f'{prefix}-{alpha}'] = np.where(data[f'{prefix}-{alpha}'] == True,alpha,0)
|
|
34
|
+
return data
|
|
35
|
+
|
|
8
36
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
mapie_class.fit(data, data[targets[i]].values)
|
|
18
|
-
classfiers.append(mapie_class)
|
|
19
|
-
return classfiers
|
|
37
|
+
class SingleStack(ClassifierMixin, BaseEstimator):
|
|
38
|
+
def __init__(self, model, estimator_index):
|
|
39
|
+
self.model = model
|
|
40
|
+
self.estimator_index = estimator_index
|
|
41
|
+
|
|
42
|
+
def fit(self):
|
|
43
|
+
self._is_fitted = True
|
|
44
|
+
self.classes_ = [0,1]
|
|
20
45
|
|
|
21
|
-
def
|
|
22
|
-
|
|
23
|
-
for i,
|
|
24
|
-
|
|
25
|
-
|
|
46
|
+
def predict_proba(self, X):
|
|
47
|
+
metas_pred = dict()
|
|
48
|
+
for i,cont in enumerate(self.model.estimators, start=1):
|
|
49
|
+
_,estimator = cont
|
|
50
|
+
meta_pred = estimator.predict_proba(X)
|
|
51
|
+
metas_pred[f"meta{i}0"] = meta_pred[0][:,1]
|
|
52
|
+
metas_pred[f"meta{i}1"] = meta_pred[1][:,1]
|
|
53
|
+
self.meta_preds_df__ = pd.DataFrame(metas_pred)
|
|
26
54
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
55
|
+
prediction_vector = list()
|
|
56
|
+
for i,cont in enumerate(self.model.meta_estimators, start=0):
|
|
57
|
+
_,estimator = cont
|
|
58
|
+
metacols = [f"meta{j}{i}" for j in range(1,len(self.model.estimators)+1)]
|
|
59
|
+
preds = estimator.predict_proba(self.meta_preds_df__[metacols].values)
|
|
60
|
+
prediction_vector.append(preds)
|
|
61
|
+
return prediction_vector[self.estimator_index]
|
|
62
|
+
|
|
63
|
+
def predict(self, X):
|
|
64
|
+
prediction_vector = list()
|
|
65
|
+
_ = self.predict_proba(X)
|
|
66
|
+
for i,cont in enumerate(self.model.meta_estimators, start=0):
|
|
67
|
+
_,estimator = cont
|
|
68
|
+
metacols = [f"meta{j}{i}" for j in range(1,len(self.model.estimators)+1)]
|
|
69
|
+
preds = estimator.predict(self.meta_preds_df__[metacols].values)
|
|
70
|
+
prediction_vector.append(preds)
|
|
71
|
+
|
|
72
|
+
p = np.array(tuple(prediction_vector))
|
|
73
|
+
return p.reshape((p.shape[1],p.shape[0]))[:,self.estimator_index]
|
|
74
|
+
|
|
75
|
+
def __sklearn_is_fitted__(self):
|
|
76
|
+
return hasattr(self, "_is_fitted") and self._is_fitted
|
|
42
77
|
|
|
43
78
|
def edge_conformal_lines(data, alphas,threshold = 0.6, plot = False, look_back = 750, offset = 0.08):
|
|
44
79
|
### corect labels ####
|
|
@@ -5,49 +5,30 @@ import numpy as np
|
|
|
5
5
|
from plotly.subplots import make_subplots
|
|
6
6
|
import plotly.graph_objects as go
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
explainers.append(model)
|
|
29
|
-
return explainers
|
|
30
|
-
|
|
31
|
-
def get_shapvalues(explainers, data):
|
|
32
|
-
shap_values = {}
|
|
33
|
-
for i,explainer in enumerate(explainers):
|
|
34
|
-
shap_value_i = explainer(data)
|
|
35
|
-
shap_values[i] = shap_value_i
|
|
36
|
-
return shap_values
|
|
37
|
-
|
|
38
|
-
def get_explainerclusters(model, data, targets):
|
|
39
|
-
clustermodels = list()
|
|
40
|
-
for i, _ in enumerate(model['model'].estimators_):
|
|
41
|
-
transf_data = model['pipe_transform'].transform(data)
|
|
42
|
-
Y = data[targets[i]]
|
|
43
|
-
cluster_model = shap.utils.hclust(transf_data, Y)
|
|
44
|
-
clustermodels.append(cluster_model)
|
|
45
|
-
return clustermodels
|
|
46
|
-
|
|
47
|
-
def mean_shap(data, explainers, pipe_transform, dict_shap_values):
|
|
8
|
+
class StackInterpretor(mlflow.pyfunc.PythonModel):
|
|
9
|
+
def __init__(self, model, targets):
|
|
10
|
+
self.base_estimators = model.estimators_
|
|
11
|
+
self.targets = targets
|
|
12
|
+
def fit_interpretor(self, data):
|
|
13
|
+
interpretors = {}
|
|
14
|
+
for label, predictor in zip(self.targets,self.base_estimators):
|
|
15
|
+
explainer = shap.Explainer(predictor, data)
|
|
16
|
+
interpretors[label] = explainer
|
|
17
|
+
self.interpretors = interpretors
|
|
18
|
+
def get_shap_values(self, data):
|
|
19
|
+
shap_values = dict()
|
|
20
|
+
for label, interpretor in self.interpretors.items():
|
|
21
|
+
shap_value = interpretor(data)
|
|
22
|
+
shap_values[label] = shap_value
|
|
23
|
+
return shap_values
|
|
24
|
+
def register_map(self, mapping):
|
|
25
|
+
self.mapping = mapping
|
|
26
|
+
|
|
27
|
+
def mean_shap(data, explainers, pipe_transform):
|
|
48
28
|
t_data = pipe_transform.transform(data)
|
|
49
29
|
input_features = t_data.columns
|
|
50
|
-
shap_results =
|
|
30
|
+
shap_results = explainers.get_shap_values(t_data)
|
|
31
|
+
dict_shap_values = explainers.mapping
|
|
51
32
|
arrays_ = list()
|
|
52
33
|
for k,_ in shap_results.items():
|
|
53
34
|
arrays_.append(shap_results.get(k).values)
|
|
@@ -70,14 +51,4 @@ def edge_shap_lines(data, plot = False, look_back = 750):
|
|
|
70
51
|
fig.update_layout(title_text="sirius - feature power",width=1200,height = 500)
|
|
71
52
|
if plot:
|
|
72
53
|
fig.show()
|
|
73
|
-
return fig
|
|
74
|
-
|
|
75
|
-
def log_top_shap(runid, top_shap):
|
|
76
|
-
with mlflow.start_run(run_id=runid) as run:
|
|
77
|
-
mlflow.log_dict(top_shap,f"explainer/top_shap.json")
|
|
78
|
-
print('artifact was logged')
|
|
79
|
-
|
|
80
|
-
def load_top_shap(runid):
|
|
81
|
-
folder = f"explainer/top_shap.json"
|
|
82
|
-
top_shap = mlflow.artifacts.load_dict(f"runs:/{runid}/{folder}")
|
|
83
|
-
return top_shap
|
|
54
|
+
return fig
|
|
@@ -8,12 +8,12 @@ virgo_modules/src/re_utils.py,sha256=DBY_VBB1wKm5D7znutpF_66CTLZhJfx54h8Ws0YzdN4
|
|
|
8
8
|
virgo_modules/src/ticketer_source.py,sha256=jxP-OOeoyN2JxRQg-mX6t6WNJXiIrhWKDywDxpYANxU,101977
|
|
9
9
|
virgo_modules/src/transformer_utils.py,sha256=ysCUp3cB3_7Jr9OHDqhg2_6Vu0k1YVjfqbvQNbxpbhI,8990
|
|
10
10
|
virgo_modules/src/edge_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
-
virgo_modules/src/edge_utils/conformal_utils.py,sha256=
|
|
11
|
+
virgo_modules/src/edge_utils/conformal_utils.py,sha256=cKm4KSM261Eu1FJn4oowKYiKIesW81VbqITIvopGSVk,5410
|
|
12
12
|
virgo_modules/src/edge_utils/edge_utils.py,sha256=7nYPLDNyKqeKIuOOwQi4wsBibzs9gP1HgYMISXJX1Y8,19522
|
|
13
|
-
virgo_modules/src/edge_utils/shap_utils.py,sha256=
|
|
13
|
+
virgo_modules/src/edge_utils/shap_utils.py,sha256=FgcHkfddvdFSeUqEubYa2ExRGVAWSthqK4b-eKagEmo,2333
|
|
14
14
|
virgo_modules/src/edge_utils/stack_model.py,sha256=QqE91uLo2KauGEj91AVNANB1xE7J4Fa49YOX7k5mFng,4257
|
|
15
|
-
virgo_modules-0.
|
|
16
|
-
virgo_modules-0.
|
|
17
|
-
virgo_modules-0.
|
|
18
|
-
virgo_modules-0.
|
|
19
|
-
virgo_modules-0.
|
|
15
|
+
virgo_modules-0.6.1.dist-info/LICENSE,sha256=pNgFyCYgmimaw0o6V20JupZLROycAnOA_HDDh1tX2V4,1097
|
|
16
|
+
virgo_modules-0.6.1.dist-info/METADATA,sha256=9EtSQrm2xy6-S4wGgWwWbL5V7yz-8BV6TlK3G18LyoM,876
|
|
17
|
+
virgo_modules-0.6.1.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
|
18
|
+
virgo_modules-0.6.1.dist-info/top_level.txt,sha256=ZjI-qEkDtT-8mFwGAWnXfqPOKEGlIhWRW1es1VyXc60,14
|
|
19
|
+
virgo_modules-0.6.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|