def test_does_quack_expected_values_mixin_negative(self): NoExpectedValues = type('InvalidExpectedValues', (BaseValid, FeatureImportanceValid, GlobalValid), {}) assert not ExpectedValuesMixin._does_quack(NoExpectedValues()) class ExpectedValuesNone(object): @property def expected_values(self): return None ExpectedValuesNoneMixin = type('InvalidExpectedValues', (ExpectedValuesNone,), {}) assert not ExpectedValuesMixin._does_quack(ExpectedValuesNoneMixin())
def create_global(self, local_importance_values, evaluation_examples=None, expected_values=None, include_local=True, batch_size=Defaults.DEFAULT_BATCH_SIZE): """Create a global explanation from the list of local feature importance values. :param local_importance_values: The feature importance values. :type local_importance_values: numpy.array or scipy.sparse.csr_matrix or list[scipy.sparse.csr_matrix] :param evaluation_examples: A matrix of feature vector examples (# examples x # features) on which to explain the model's output. :type evaluation_examples: numpy.array or pandas.DataFrame or scipy.sparse.csr_matrix :param expected_values: The expected values of the model. :type expected_values: numpy.array :param include_local: Include the local explanations in the returned global explanation. If include_local is False, will stream the local explanations to aggregate to global. :type include_local: bool :param batch_size: If include_local is False, specifies the batch size for aggregating local explanations to global. :type batch_size: int """ local_explanation = self.create_local(local_importance_values, evaluation_examples, expected_values) kwargs = {ExplainParams.METHOD: self.method} kwargs[ExplainParams.FEATURES] = self.features if ExpectedValuesMixin._does_quack(local_explanation): kwargs[ExplainParams. EXPECTED_VALUES] = local_explanation.expected_values if include_local: kwargs[ExplainParams.LOCAL_EXPLANATION] = local_explanation # Aggregate local explanation to global return _aggregate_global_from_local_explanation(**kwargs) else: if ExplainParams.CLASSIFICATION in kwargs: if kwargs[ExplainParams.CLASSIFICATION]: model_task = ModelTask.Classification else: model_task = ModelTask.Regression else: model_task = ModelTask.Unknown kwargs = _aggregate_streamed_local_explanations( self, evaluation_examples, model_task, self.features, batch_size, **kwargs) return _create_global_explanation(**kwargs)
def test_does_quack_expected_values_mixin(self): ValidExpectedValues = type('ValidExpectedValues', (ExpectedValuesValid,), {}) assert ExpectedValuesMixin._does_quack(ValidExpectedValues())