Beispiel #1
0
    def __init__(self,
                 explainer: SmartExplainer,
                 project_info_file: str,
                 x_train: Optional[pd.DataFrame] = None,
                 y_train: Optional[pd.DataFrame] = None,
                 y_test: Optional[pd.DataFrame] = None,
                 config: Optional[dict] = None):
        self.explainer = explainer
        self.metadata = load_yml(path=project_info_file)
        self.x_train_init = x_train
        if x_train is not None:
            self.x_train_pre = inverse_transform(x_train,
                                                 self.explainer.preprocessing)
            if self.explainer.postprocessing:
                self.x_train_pre = apply_postprocessing(
                    self.x_train_pre, self.explainer.postprocessing)
        else:
            self.x_train_pre = None
        self.x_pred = self.explainer.x_pred
        self.config = config if config is not None else dict()
        self.col_names = list(self.explainer.columns_dict.values())
        self.df_train_test = self._create_train_test_df(test=self.x_pred,
                                                        train=self.x_train_pre)
        self.y_pred = self.explainer.model.predict(self.explainer.x_init)
        self.y_test, target_name_test = self._get_values_and_name(
            y_test, 'target')
        self.y_train, target_name_train = self._get_values_and_name(
            y_train, 'target')
        self.target_name = target_name_train or target_name_test

        if 'title_story' in self.config.keys():
            self.title_story = config['title_story']
        elif self.explainer.title_story != '':
            self.title_story = self.explainer.title_story
        else:
            self.title_story = 'Shapash report'
        self.title_description = self.config[
            'title_description'] if 'title_description' in self.config.keys(
            ) else ''

        print_css_style()
        print_javascript_misc()

        if 'metrics' in self.config.keys():
            if not isinstance(self.config['metrics'], list) or not isinstance(
                    self.config['metrics'][0], dict):
                raise ValueError(
                    "The metrics parameter expects a list of dict.")
            for metric in self.config['metrics']:
                for key in metric:
                    if key not in ['path', 'name', 'use_proba_values']:
                        raise ValueError(
                            f"Unknown key : {key}. Key should be in ['path', 'name', 'use_proba_values']"
                        )
                    if key == 'use_proba_values' and not isinstance(
                            metric['use_proba_values'], bool):
                        raise ValueError(
                            '"use_proba_values" metric key expects a boolean value.'
                        )
Beispiel #2
0
    def apply_postprocessing(self, postprocessing=None):
        """
        Modifies x_pred Dataframe according to postprocessing modifications, if exists.

        Parameters
        ----------
        postprocessing: Dict
            Dictionnary of postprocessing modifications to apply in x_pred.

        Returns
        -------
        pandas.Dataframe
            Returns x_pred if postprocessing is empty, modified dataframe otherwise.
        """
        if postprocessing:
            return apply_postprocessing(self.x_pred, postprocessing)
        else:
            return self.x_pred