예제 #1
0
class XGBMultiClassifierExplainerTests(unittest.TestCase):
    def setUp(self):
        X_train, y_train, X_test, y_test = titanic_embarked()
        train_names, test_names = titanic_names()
        _, self.names = titanic_names()

        model = XGBClassifier(n_estimators=5)
        model.fit(X_train, y_train)

        self.explainer = ClassifierExplainer(
            model,
            X_test,
            y_test,
            model_output='raw',
            cats=[{
                'Gender': ['Sex_female', 'Sex_male', 'Sex_nan']
            }, 'Deck', 'Embarked'],
            idxs=test_names,
            labels=['Queenstown', 'Southampton', 'Cherbourg'])

    def test_graphviz_available(self):
        self.assertIsInstance(self.explainer.graphviz_available, bool)

    def test_decision_trees(self):
        dt = self.explainer.decision_trees
        self.assertIsInstance(dt, list)
        self.assertIsInstance(
            dt[0], dtreeviz.models.shadow_decision_tree.ShadowDecTree)

    def test_decisiontree_df(self):
        df = self.explainer.decisiontree_df(tree_idx=0, index=0)
        self.assertIsInstance(df, pd.DataFrame)

        df = self.explainer.decisiontree_df(tree_idx=0, index=self.names[0])
        self.assertIsInstance(df, pd.DataFrame)

        df = self.explainer.decisiontree_df(tree_idx=0,
                                            index=self.names[0],
                                            pos_label=0)
        self.assertIsInstance(df, pd.DataFrame)

    def test_plot_trees(self):
        fig = self.explainer.plot_trees(index=0)
        self.assertIsInstance(fig, go.Figure)

        fig = self.explainer.plot_trees(index=self.names[0])
        self.assertIsInstance(fig, go.Figure)

        fig = self.explainer.plot_trees(index=self.names[0], highlight_tree=0)
        self.assertIsInstance(fig, go.Figure)

        fig = self.explainer.plot_trees(index=self.names[0], pos_label=0)
        self.assertIsInstance(fig, go.Figure)

    def test_calculate_properties(self):
        self.explainer.calculate_properties()
예제 #2
0
class ClassifierBunchTests(unittest.TestCase):
    def setUp(self):
        X_train, y_train, X_test, y_test = titanic_survive()
        train_names, test_names = titanic_names()
        _, self.names = titanic_names()

        model = RandomForestClassifier(n_estimators=5, max_depth=2)
        model.fit(X_train, y_train)

        self.explainer = ClassifierExplainer(
            model,
            X_test,
            y_test,
            roc_auc_score,
            shap='tree',
            cats=['Sex', 'Cabin', 'Embarked'],
            idxs=test_names,
            labels=['Not survived', 'Survived'])

    def test_graphviz_available(self):
        self.assertIsInstance(self.explainer.graphviz_available, bool)

    def test_decision_trees(self):
        dt = self.explainer.decision_trees
        self.assertIsInstance(dt, list)
        self.assertIsInstance(
            dt[0], dtreeviz.models.shadow_decision_tree.ShadowDecTree)

    def test_decisiontree_df(self):
        df = self.explainer.decisiontree_df(tree_idx=0, index=0)
        self.assertIsInstance(df, pd.DataFrame)

        df = self.explainer.decisiontree_df(tree_idx=0, index=self.names[0])
        self.assertIsInstance(df, pd.DataFrame)

    def test_plot_trees(self):
        fig = self.explainer.plot_trees(index=0)
        self.assertIsInstance(fig, go.Figure)

        fig = self.explainer.plot_trees(index=self.names[0])
        self.assertIsInstance(fig, go.Figure)

        fig = self.explainer.plot_trees(index=self.names[0], highlight_tree=0)
        self.assertIsInstance(fig, go.Figure)

    def test_calculate_properties(self):
        self.explainer.calculate_properties()