nirdizati_light.recommender_model.classifier_and_regressor.classifier_and_regressor

 1import logging
 2from enum import Enum
 3from operator import itemgetter
 4
 5from pandas import DataFrame
 6
 7from nirdizati_light.hyperparameter_optimisation.common import HyperoptTarget, retrieve_best_model
 8from nirdizati_light.predictive_model.predictive_model import PredictiveModel
 9
10logger = logging.getLogger(__name__)
11
12
13def drop_columns(df: DataFrame) -> DataFrame:
14    df = df.drop(['trace_id', 'label'], 1)
15    return df
16
17
18class RecommenderModelInstantiation(Enum):
19    CLASSIFIER = 'classifier'
20    REGRESSOR = 'regressor'
21
22
23class ClassifierAndRegressor:
24
25    def __init__(self, model_type, CLASSIFIER_CONF, classifier_train_df, classifier_validate_df, REGRESSOR_CONF, regressor_train_df, regressor_validate_df):
26        self.model_type = model_type
27        self.full_classifier_train_df = classifier_train_df
28        self.full_classifier_validate_df = classifier_validate_df
29
30        self.full_regressor_train_df = regressor_train_df
31        self.full_regressor_validate_df = regressor_validate_df
32        self.model = dict()
33        self.model[RecommenderModelInstantiation.CLASSIFIER.value] = PredictiveModel(
34            CLASSIFIER_CONF,
35            CLASSIFIER_CONF['predictive_model'],
36            self.full_classifier_train_df,
37            self.full_classifier_validate_df
38        )
39        self.model[RecommenderModelInstantiation.REGRESSOR.value] = PredictiveModel(
40            REGRESSOR_CONF,
41            REGRESSOR_CONF['predictive_model'],
42            self.full_regressor_train_df,
43            self.full_regressor_validate_df
44        )
45
46    def fit(self, max_evaluations, target=dict({
47            RecommenderModelInstantiation.CLASSIFIER.value: HyperoptTarget.AUC.value,
48            RecommenderModelInstantiation.REGRESSOR.value: HyperoptTarget.MAE.value
49        })):
50        # ottimizza classificatore
51        print('Optimizing the classifier')
52        self.model[RecommenderModelInstantiation.CLASSIFIER.value].model, \
53        self.model[RecommenderModelInstantiation.CLASSIFIER.value].config = retrieve_best_model(
54            self.model[RecommenderModelInstantiation.CLASSIFIER.value],
55            self.model[RecommenderModelInstantiation.CLASSIFIER.value].model_type,
56            max_evaluations,
57            target[RecommenderModelInstantiation.CLASSIFIER.value]
58        )
59        # ottimizza regressore
60        print('Optimizing the regressor')
61        self.model[RecommenderModelInstantiation.REGRESSOR.value].model, \
62        self.model[RecommenderModelInstantiation.REGRESSOR.value].config = retrieve_best_model(
63            self.model[RecommenderModelInstantiation.REGRESSOR.value],
64            self.model[RecommenderModelInstantiation.REGRESSOR.value].model_type,
65            max_evaluations,
66            target[RecommenderModelInstantiation.REGRESSOR.value]
67        )
68
69    def recommend(self, df, top_n):
70        recommendations = []
71        for trace, likely_next in zip(df.T,
72                               self.model[RecommenderModelInstantiation.CLASSIFIER.value].model.predict_proba(
73                                       drop_columns(df))):
74            likely_next_activities = likely_next.argsort()[-top_n:][::-1]
75            ranked_next_activities = [
76                (
77                    next_activity,
78                    self.model[RecommenderModelInstantiation.REGRESSOR.value].model.predict(
79                        [list(drop_columns(df).T[trace].values.reshape(1, -1)[0]) + [next_activity]])
80                )
81                for next_activity in likely_next_activities
82            ]
83
84            recommendations += [ min(ranked_next_activities, key=itemgetter(1))[0] ]
85        return recommendations
def drop_columns(df: pandas.core.frame.DataFrame) -> pandas.core.frame.DataFrame:
14def drop_columns(df: DataFrame) -> DataFrame:
15    df = df.drop(['trace_id', 'label'], 1)
16    return df
class RecommenderModelInstantiation(enum.Enum):
19class RecommenderModelInstantiation(Enum):
20    CLASSIFIER = 'classifier'
21    REGRESSOR = 'regressor'

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access::
>>> Color.RED
<Color.RED: 1>
  • value lookup:
>>> Color(1)
<Color.RED: 1>
  • name lookup:
>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

CLASSIFIER = <RecommenderModelInstantiation.CLASSIFIER: 'classifier'>
REGRESSOR = <RecommenderModelInstantiation.REGRESSOR: 'regressor'>
Inherited Members
enum.Enum
name
value
class ClassifierAndRegressor:
24class ClassifierAndRegressor:
25
26    def __init__(self, model_type, CLASSIFIER_CONF, classifier_train_df, classifier_validate_df, REGRESSOR_CONF, regressor_train_df, regressor_validate_df):
27        self.model_type = model_type
28        self.full_classifier_train_df = classifier_train_df
29        self.full_classifier_validate_df = classifier_validate_df
30
31        self.full_regressor_train_df = regressor_train_df
32        self.full_regressor_validate_df = regressor_validate_df
33        self.model = dict()
34        self.model[RecommenderModelInstantiation.CLASSIFIER.value] = PredictiveModel(
35            CLASSIFIER_CONF,
36            CLASSIFIER_CONF['predictive_model'],
37            self.full_classifier_train_df,
38            self.full_classifier_validate_df
39        )
40        self.model[RecommenderModelInstantiation.REGRESSOR.value] = PredictiveModel(
41            REGRESSOR_CONF,
42            REGRESSOR_CONF['predictive_model'],
43            self.full_regressor_train_df,
44            self.full_regressor_validate_df
45        )
46
47    def fit(self, max_evaluations, target=dict({
48            RecommenderModelInstantiation.CLASSIFIER.value: HyperoptTarget.AUC.value,
49            RecommenderModelInstantiation.REGRESSOR.value: HyperoptTarget.MAE.value
50        })):
51        # ottimizza classificatore
52        print('Optimizing the classifier')
53        self.model[RecommenderModelInstantiation.CLASSIFIER.value].model, \
54        self.model[RecommenderModelInstantiation.CLASSIFIER.value].config = retrieve_best_model(
55            self.model[RecommenderModelInstantiation.CLASSIFIER.value],
56            self.model[RecommenderModelInstantiation.CLASSIFIER.value].model_type,
57            max_evaluations,
58            target[RecommenderModelInstantiation.CLASSIFIER.value]
59        )
60        # ottimizza regressore
61        print('Optimizing the regressor')
62        self.model[RecommenderModelInstantiation.REGRESSOR.value].model, \
63        self.model[RecommenderModelInstantiation.REGRESSOR.value].config = retrieve_best_model(
64            self.model[RecommenderModelInstantiation.REGRESSOR.value],
65            self.model[RecommenderModelInstantiation.REGRESSOR.value].model_type,
66            max_evaluations,
67            target[RecommenderModelInstantiation.REGRESSOR.value]
68        )
69
70    def recommend(self, df, top_n):
71        recommendations = []
72        for trace, likely_next in zip(df.T,
73                               self.model[RecommenderModelInstantiation.CLASSIFIER.value].model.predict_proba(
74                                       drop_columns(df))):
75            likely_next_activities = likely_next.argsort()[-top_n:][::-1]
76            ranked_next_activities = [
77                (
78                    next_activity,
79                    self.model[RecommenderModelInstantiation.REGRESSOR.value].model.predict(
80                        [list(drop_columns(df).T[trace].values.reshape(1, -1)[0]) + [next_activity]])
81                )
82                for next_activity in likely_next_activities
83            ]
84
85            recommendations += [ min(ranked_next_activities, key=itemgetter(1))[0] ]
86        return recommendations
ClassifierAndRegressor( model_type, CLASSIFIER_CONF, classifier_train_df, classifier_validate_df, REGRESSOR_CONF, regressor_train_df, regressor_validate_df)
26    def __init__(self, model_type, CLASSIFIER_CONF, classifier_train_df, classifier_validate_df, REGRESSOR_CONF, regressor_train_df, regressor_validate_df):
27        self.model_type = model_type
28        self.full_classifier_train_df = classifier_train_df
29        self.full_classifier_validate_df = classifier_validate_df
30
31        self.full_regressor_train_df = regressor_train_df
32        self.full_regressor_validate_df = regressor_validate_df
33        self.model = dict()
34        self.model[RecommenderModelInstantiation.CLASSIFIER.value] = PredictiveModel(
35            CLASSIFIER_CONF,
36            CLASSIFIER_CONF['predictive_model'],
37            self.full_classifier_train_df,
38            self.full_classifier_validate_df
39        )
40        self.model[RecommenderModelInstantiation.REGRESSOR.value] = PredictiveModel(
41            REGRESSOR_CONF,
42            REGRESSOR_CONF['predictive_model'],
43            self.full_regressor_train_df,
44            self.full_regressor_validate_df
45        )
model_type
full_classifier_train_df
full_classifier_validate_df
full_regressor_train_df
full_regressor_validate_df
model
def fit( self, max_evaluations, target={'classifier': 'auc', 'regressor': 'mae'}):
47    def fit(self, max_evaluations, target=dict({
48            RecommenderModelInstantiation.CLASSIFIER.value: HyperoptTarget.AUC.value,
49            RecommenderModelInstantiation.REGRESSOR.value: HyperoptTarget.MAE.value
50        })):
51        # ottimizza classificatore
52        print('Optimizing the classifier')
53        self.model[RecommenderModelInstantiation.CLASSIFIER.value].model, \
54        self.model[RecommenderModelInstantiation.CLASSIFIER.value].config = retrieve_best_model(
55            self.model[RecommenderModelInstantiation.CLASSIFIER.value],
56            self.model[RecommenderModelInstantiation.CLASSIFIER.value].model_type,
57            max_evaluations,
58            target[RecommenderModelInstantiation.CLASSIFIER.value]
59        )
60        # ottimizza regressore
61        print('Optimizing the regressor')
62        self.model[RecommenderModelInstantiation.REGRESSOR.value].model, \
63        self.model[RecommenderModelInstantiation.REGRESSOR.value].config = retrieve_best_model(
64            self.model[RecommenderModelInstantiation.REGRESSOR.value],
65            self.model[RecommenderModelInstantiation.REGRESSOR.value].model_type,
66            max_evaluations,
67            target[RecommenderModelInstantiation.REGRESSOR.value]
68        )
def recommend(self, df, top_n):
70    def recommend(self, df, top_n):
71        recommendations = []
72        for trace, likely_next in zip(df.T,
73                               self.model[RecommenderModelInstantiation.CLASSIFIER.value].model.predict_proba(
74                                       drop_columns(df))):
75            likely_next_activities = likely_next.argsort()[-top_n:][::-1]
76            ranked_next_activities = [
77                (
78                    next_activity,
79                    self.model[RecommenderModelInstantiation.REGRESSOR.value].model.predict(
80                        [list(drop_columns(df).T[trace].values.reshape(1, -1)[0]) + [next_activity]])
81                )
82                for next_activity in likely_next_activities
83            ]
84
85            recommendations += [ min(ranked_next_activities, key=itemgetter(1))[0] ]
86        return recommendations