Пример #1
0
 def test_apply_postprocessing_2(self):
     """
     Unit test apply_postprocessing 2
     """
     xpl = SmartExplainer()
     xpl.x_pred = pd.DataFrame([[1, 2], [3, 4]],
                               columns=['Col1', 'Col2'],
                               index=['Id1', 'Id2'])
     xpl.features_dict = {'Col1': 'Column1', 'Col2': 'Column2'}
     xpl.columns_dict = {0: 'Col1', 1: 'Col2'}
     xpl.inv_features_dict = {'Column1': 'Col1', 'Column2': 'Col2'}
     postprocessing = {
         'Col1': {
             'type': 'suffix',
             'rule': ' t'
         },
         'Col2': {
             'type': 'prefix',
             'rule': 'test'
         }
     }
     expected_output = pd.DataFrame(data=[['1 t', 'test2'],
                                          ['3 t', 'test4']],
                                    columns=['Col1', 'Col2'],
                                    index=['Id1', 'Id2'])
     output = xpl.apply_postprocessing(postprocessing)
     assert np.array_equal(output, expected_output)
Пример #2
0
 def test_check_postprocessing_1(self):
     """
     Unit test check_postprocessing
     """
     xpl = SmartExplainer()
     xpl.x_pred = pd.DataFrame(
         [[1, 2],
          [3, 4]],
         columns=['Col1', 'Col2'],
         index=['Id1', 'Id2']
     )
     xpl.features_dict = {'Col1': 'Column1', 'Col2': 'Column2'}
     xpl.columns_dict = {0: 'Col1', 1: 'Col2'}
     xpl.inv_features_dict = {'Column1': 'Col1', 'Column2': 'Col2'}
     postprocessing1 = {0: {'Error': 'suffix', 'rule': ' t'}}
     postprocessing2 = {0: {'type': 'Error', 'rule': ' t'}}
     postprocessing3 = {0: {'type': 'suffix', 'Error': ' t'}}
     postprocessing4 = {0: {'type': 'suffix', 'rule': ' '}}
     postprocessing5 = {0: {'type': 'case', 'rule': 'lower'}}
     postprocessing6 = {0: {'type': 'case', 'rule': 'Error'}}
     with self.assertRaises(ValueError):
         xpl.check_postprocessing(postprocessing1)
         xpl.check_postprocessing(postprocessing2)
         xpl.check_postprocessing(postprocessing3)
         xpl.check_postprocessing(postprocessing4)
         xpl.check_postprocessing(postprocessing5)
         xpl.check_postprocessing(postprocessing6)
Пример #3
0
    def test_modify_postprocessing_1(self):
        """
        Unit test modify postprocessing 1
        """
        xpl = SmartExplainer()
        xpl.x_pred = pd.DataFrame([[1, 2], [3, 4]],
                                  columns=['Col1', 'Col2'],
                                  index=['Id1', 'Id2'])
        xpl.features_dict = {'Col1': 'Column1', 'Col2': 'Column2'}
        xpl.columns_dict = {0: 'Col1', 1: 'Col2'}
        xpl.inv_features_dict = {'Column1': 'Col1', 'Column2': 'Col2'}
        postprocessing = {
            0: {
                'type': 'suffix',
                'rule': ' t'
            },
            'Column2': {
                'type': 'prefix',
                'rule': 'test'
            }
        }

        expected_output = {
            'Col1': {
                'type': 'suffix',
                'rule': ' t'
            },
            'Col2': {
                'type': 'prefix',
                'rule': 'test'
            }
        }
        output = xpl.modify_postprocessing(postprocessing)
        assert output == expected_output
Пример #4
0
 def test_add_3(self):
     """
     Unit test add 3
     """
     xpl = SmartExplainer()
     xpl.columns_dict = {0: 'Age', 1: 'Education', 2: 'Sex'}
     xpl.add(features_dict={'Age': 'Age (Years Old)'})
     assert xpl.features_dict['Age'] == 'Age (Years Old)'
     assert xpl.features_dict['Education'] == 'Education'
Пример #5
0
 def test_check_features_dict_1(self):
     """
     Unit test check features dict 1
     """
     xpl = SmartExplainer(features_dict={'Age': 'Age (Years Old)'})
     xpl.columns_dict = {0: 'Age', 1: 'Education', 2: 'Sex'}
     xpl.check_features_dict()
     assert xpl.features_dict['Age'] == 'Age (Years Old)'
     assert xpl.features_dict['Education'] == 'Education'
Пример #6
0
 def test_to_pandas_1(self):
     """
     Unit test to pandas 1
     """
     xpl = SmartExplainer()
     xpl.state = SmartState()
     data = {}
     data['contrib_sorted'] = pd.DataFrame(
         [[0.32230754, 0.1550689, 0.10183475, 0.05471339],
          [-0.58547512, -0.37050409, -0.07249285, 0.00171975],
          [-0.48666675, 0.25507156, -0.16968889, 0.0757443]],
         columns=[
             'contribution_0', 'contribution_1', 'contribution_2',
             'contribution_3'
         ],
         index=[0, 1, 2])
     data['var_dict'] = pd.DataFrame(
         [[1, 0, 2, 3], [1, 0, 3, 2], [1, 0, 2, 3]],
         columns=['feature_0', 'feature_1', 'feature_2', 'feature_3'],
         index=[0, 1, 2])
     data['x_sorted'] = pd.DataFrame(
         [[1., 3., 22., 1.], [2., 1., 2., 38.], [2., 3., 26., 1.]],
         columns=['feature_0', 'feature_1', 'feature_2', 'feature_3'],
         index=[0, 1, 2])
     xpl.data = data
     xpl.columns_dict = {0: 'Pclass', 1: 'Sex', 2: 'Age', 3: 'Embarked'}
     xpl.features_dict = {
         'Pclass': 'Pclass',
         'Sex': 'Sex',
         'Age': 'Age',
         'Embarked': 'Embarked'
     }
     xpl.x = pd.DataFrame(
         [[3., 1., 22., 1.], [1., 2., 38., 2.], [3., 2., 26., 1.]],
         columns=['Pclass', 'Sex', 'Age', 'Embarked'],
         index=[0, 1, 2])
     xpl.x_pred = xpl.x
     xpl.contributions = data['contrib_sorted']
     xpl.y_pred = pd.DataFrame([1, 2, 3], columns=['pred'], index=[0, 1, 2])
     model = lambda: None
     model.predict = types.MethodType(self.predict, model)
     xpl.model = model
     xpl._case, xpl._classes = xpl.check_model()
     xpl.state = xpl.choose_state(xpl.contributions)
     output = xpl.to_pandas(max_contrib=2)
     expected = pd.DataFrame(
         [[1, 'Sex', 1.0, 0.32230754, 'Pclass', 3.0, 0.1550689],
          [2, 'Sex', 2.0, -0.58547512, 'Pclass', 1.0, -0.37050409],
          [3, 'Sex', 2.0, -0.48666675, 'Pclass', 3.0, 0.25507156]],
         columns=[
             'pred', 'feature_1', 'value_1', 'contribution_1', 'feature_2',
             'value_2', 'contribution_2'
         ],
         index=[0, 1, 2],
         dtype=object)
     expected['pred'] = expected['pred'].astype(int)
     assert not pd.testing.assert_frame_equal(expected, output)
Пример #7
0
 def test_check_features_name_4(self):
     """
     Unit test check features name 4
     """
     xpl = SmartExplainer()
     xpl.columns_dict = None
     xpl.features_dict = None
     feature_list = [1, 2, 4]
     output = xpl.check_features_name(feature_list)
     expected_output = feature_list
     np.testing.assert_array_equal(output, expected_output)
Пример #8
0
 def test_check_features_name_3(self):
     """
     Unit test check features name 3
     """
     xpl = SmartExplainer()
     xpl.columns_dict = {0: 'tech_0', 1: 'tech_1', 2: 'tech_2'}
     xpl.inv_columns_dict = {v: k for k, v in xpl.columns_dict.items()}
     feature_list = ['tech_2']
     output = xpl.check_features_name(feature_list)
     expected_output = [2]
     np.testing.assert_array_equal(output, expected_output)
Пример #9
0
 def test_apply_postprocessing_1(self):
     """
     Unit test apply_postprocessing 1
     """
     xpl = SmartExplainer()
     xpl.x_pred = pd.DataFrame([[1, 2], [3, 4]],
                               columns=['Col1', 'Col2'],
                               index=['Id1', 'Id2'])
     xpl.features_dict = {'Col1': 'Column1', 'Col2': 'Column2'}
     xpl.columns_dict = {0: 'Col1', 1: 'Col2'}
     xpl.inv_features_dict = {'Column1': 'Col1', 'Column2': 'Col2'}
     assert np.array_equal(xpl.x_pred, xpl.apply_postprocessing())
Пример #10
0
 def test_to_pandas_2(self):
     """
     Unit test to_pandas :
     test to_pandas method in classification case with
     predict_proba output and column_dict attribute
     """
     xpl = SmartExplainer()
     contrib = pd.DataFrame(
         [[0.32230754, 0.1550689, 0.10183475, 0.05471339],
          [-0.58547512, -0.37050409, -0.07249285, 0.00171975],
          [-0.48666675, 0.25507156, -0.16968889, 0.0757443]],
         index=[0, 1, 2])
     model = lambda: None
     model._classes = np.array([1, 3])
     model.predict = types.MethodType(self.predict, model)
     model.predict_proba = types.MethodType(self.predict_proba, model)
     x = pd.DataFrame(
         [[3., 1., 22., 1.], [1., 2., 38., 2.], [3., 2., 26., 1.]],
         index=[0, 1, 2])
     pred = pd.DataFrame([3, 1, 1], columns=['pred'], index=[0, 1, 2])
     xpl.compile(contributions=contrib, x=x, model=model, y_pred=pred)
     xpl.columns_dict = {0: 'Pclass', 1: 'Sex', 2: 'Age', 3: 'Embarked'}
     xpl.features_dict = {
         'Pclass': 'Pclass',
         'Sex': 'Sex',
         'Age': 'Age',
         'Embarked': 'Embarked'
     }
     output = xpl.to_pandas(max_contrib=3, positive=True, proba=True)
     expected = pd.DataFrame([[
         3, 0.8, 'Pclass', 3.0, 0.32230754, 'Sex', 1.0, 0.1550689, 'Age',
         22.0, 0.10183475
     ],
                              [
                                  1, 0.3, 'Pclass', 1.0, 0.58547512, 'Sex',
                                  2.0, 0.37050409, 'Age', 38.0, 0.07249285
                              ],
                              [
                                  1, 0.4, 'Pclass', 3.0, 0.48666675, 'Age',
                                  26.0, 0.16968889, np.nan, np.nan, np.nan
                              ]],
                             columns=[
                                 'pred', 'proba', 'feature_1', 'value_1',
                                 'contribution_1', 'feature_2', 'value_2',
                                 'contribution_2', 'feature_3', 'value_3',
                                 'contribution_3'
                             ],
                             index=[0, 1, 2],
                             dtype=object)
     expected['pred'] = expected['pred'].astype(int)
     expected['proba'] = expected['proba'].astype(float)
     assert not pd.testing.assert_frame_equal(expected, output)
Пример #11
0
 def test_check_features_name_1(self):
     """
     Unit test check features name 1
     """
     xpl = SmartExplainer()
     xpl.features_dict = {'tech_0': 'domain_0', 'tech_1': 'domain_1', 'tech_2': 'domain_2'}
     xpl.inv_features_dict = {v: k for k, v in xpl.features_dict.items()}
     xpl.columns_dict = {0: 'tech_0', 1: 'tech_1', 2: 'tech_2'}
     xpl.inv_columns_dict = {v: k for k, v in xpl.columns_dict.items()}
     feature_list_1 = ['domain_0', 'tech_1']
     feature_list_2 = ['domain_0', 0]
     self.assertRaises(ValueError, xpl.check_features_name, feature_list_1)
     self.assertRaises(ValueError, xpl.check_features_name, feature_list_2)
Пример #12
0
 def test_modify_postprocessing_2(self):
     """
     Unit test modify postprocessing 2
     """
     xpl = SmartExplainer()
     xpl.x_pred = pd.DataFrame([[1, 2], [3, 4]],
                               columns=['Col1', 'Col2'],
                               index=['Id1', 'Id2'])
     xpl.features_dict = {'Col1': 'Column1', 'Col2': 'Column2'}
     xpl.columns_dict = {0: 'Col1', 1: 'Col2'}
     xpl.inv_features_dict = {'Column1': 'Col1', 'Column2': 'Col2'}
     postprocessing = {'Error': {'type': 'suffix', 'rule': ' t'}}
     with self.assertRaises(ValueError):
         xpl.modify_postprocessing(postprocessing)