nodes: 0, module; 1, function_definition; 2, function_name:explain_instance_with_data; 3, parameters; 4, block; 5, identifier:self; 6, identifier:neighborhood_data; 7, identifier:neighborhood_labels; 8, identifier:distances; 9, identifier:label; 10, identifier:num_features; 11, default_parameter; 12, default_parameter; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, if_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, if_statement; 23, return_statement; 24, identifier:feature_selection; 25, string; 26, identifier:model_regressor; 27, None; 28, comment:"""Takes perturbed data, labels and distances, returns explanation.
  
          Args:
              neighborhood_data: perturbed data, 2d array. first element is
                                 assumed to be the original data point.
              neighborhood_labels: corresponding perturbed labels. should have as
                                   many columns as the number of possible labels.
              distances: distances to original data point.
              label: label for which we want an explanation
              num_features: maximum number of features in explanation
              feature_selection: how to select num_features. options are:
                  'forward_selection': iteratively add features to the model.
                      This is costly when num_features is high
                  'highest_weights': selects the features that have the highest
                      product of absolute weight * original data point when
                      learning with all the features
                  'lasso_path': chooses features based on the lasso
                      regularization path
                  'none': uses all features, ignores num_features
                  'auto': uses forward_selection if num_features <= 6, and
                      'highest_weights' otherwise.
              model_regressor: sklearn regressor to use in explanation.
                  Defaults to Ridge regression if None. Must have
                  model_regressor.coef_ and 'sample_weight' as a parameter
                  to model_regressor.fit()
  
          Returns:
              (intercept, exp, score, local_pred):
              intercept is a float.
              exp is a sorted list of tuples, where each tuple (x,y) corresponds
              to the feature id (x) and the local weight (y). The list is sorted
              by decreasing absolute value of y.
              score is the R^2 value of the returned explanation
              local_pred is the prediction of the explanation model on the original instance
          """; 29, assignment; 30, assignment; 31, assignment; 32, comparison_operator:model_regressor is None; 33, block; 34, assignment; 35, call; 36, assignment; 37, assignment; 38, attribute; 39, block; 40, tuple; 41, string_content:auto; 42, identifier:weights; 43, call; 44, identifier:labels_column; 45, subscript; 46, identifier:used_features; 47, call; 48, identifier:model_regressor; 49, None; 50, expression_statement; 51, identifier:easy_model; 52, identifier:model_regressor; 53, attribute; 54, argument_list; 55, identifier:prediction_score; 56, call; 57, identifier:local_pred; 58, call; 59, identifier:self; 60, identifier:verbose; 61, expression_statement; 62, expression_statement; 63, expression_statement; 64, attribute; 65, call; 66, identifier:prediction_score; 67, identifier:local_pred; 68, attribute; 69, argument_list; 70, identifier:neighborhood_labels; 71, slice; 72, identifier:label; 73, attribute; 74, argument_list; 75, assignment; 76, identifier:easy_model; 77, identifier:fit; 78, subscript; 79, identifier:labels_column; 80, keyword_argument; 81, attribute; 82, argument_list; 83, attribute; 84, argument_list; 85, call; 86, call; 87, call; 88, identifier:easy_model; 89, identifier:intercept_; 90, identifier:sorted; 91, argument_list; 92, identifier:self; 93, identifier:kernel_fn; 94, identifier:distances; 95, identifier:self; 96, identifier:feature_selection; 97, identifier:neighborhood_data; 98, identifier:labels_column; 99, identifier:weights; 100, identifier:num_features; 101, identifier:feature_selection; 102, identifier:model_regressor; 103, call; 104, identifier:neighborhood_data; 105, slice; 106, identifier:used_features; 107, identifier:sample_weight; 108, identifier:weights; 109, identifier:easy_model; 110, identifier:score; 111, subscript; 112, identifier:labels_column; 113, keyword_argument; 114, identifier:easy_model; 115, identifier:predict; 116, call; 117, identifier:print; 118, argument_list; 119, identifier:print; 120, argument_list; 121, identifier:print; 122, argument_list; 123, call; 124, keyword_argument; 125, keyword_argument; 126, identifier:Ridge; 127, argument_list; 128, identifier:neighborhood_data; 129, slice; 130, identifier:used_features; 131, identifier:sample_weight; 132, identifier:weights; 133, attribute; 134, argument_list; 135, string; 136, attribute; 137, string; 138, identifier:local_pred; 139, string; 140, subscript; 141, identifier:zip; 142, argument_list; 143, identifier:key; 144, lambda; 145, identifier:reverse; 146, True; 147, keyword_argument; 148, keyword_argument; 149, keyword_argument; 150, subscript; 151, identifier:reshape; 152, integer:1; 153, unary_operator; 154, string_content:Intercept; 155, identifier:easy_model; 156, identifier:intercept_; 157, string_content:Prediction_local; 158, string_content:Right:; 159, identifier:neighborhood_labels; 160, integer:0; 161, identifier:label; 162, identifier:used_features; 163, attribute; 164, lambda_parameters; 165, call; 166, identifier:alpha; 167, integer:1; 168, identifier:fit_intercept; 169, True; 170, identifier:random_state; 171, attribute; 172, identifier:neighborhood_data; 173, integer:0; 174, identifier:used_features; 175, integer:1; 176, identifier:easy_model; 177, identifier:coef_; 178, identifier:x; 179, attribute; 180, argument_list; 181, identifier:self; 182, identifier:random_state; 183, identifier:np; 184, identifier:abs; 185, subscript; 186, identifier:x; 187, integer:1
code: def explain_instance_with_data(self,
                                     neighborhood_data,
                                     neighborhood_labels,
                                     distances,
                                     label,
                                     num_features,
                                     feature_selection='auto',
                                     model_regressor=None):
          """Takes perturbed data, labels and distances, returns explanation.
  
          Args:
              neighborhood_data: perturbed data, 2d array. first element is
                                 assumed to be the original data point.
              neighborhood_labels: corresponding perturbed labels. should have as
                                   many columns as the number of possible labels.
              distances: distances to original data point.
              label: label for which we want an explanation
              num_features: maximum number of features in explanation
              feature_selection: how to select num_features. options are:
                  'forward_selection': iteratively add features to the model.
                      This is costly when num_features is high
                  'highest_weights': selects the features that have the highest
                      product of absolute weight * original data point when
                      learning with all the features
                  'lasso_path': chooses features based on the lasso
                      regularization path
                  'none': uses all features, ignores num_features
                  'auto': uses forward_selection if num_features <= 6, and
                      'highest_weights' otherwise.
              model_regressor: sklearn regressor to use in explanation.
                  Defaults to Ridge regression if None. Must have
                  model_regressor.coef_ and 'sample_weight' as a parameter
                  to model_regressor.fit()
  
          Returns:
              (intercept, exp, score, local_pred):
              intercept is a float.
              exp is a sorted list of tuples, where each tuple (x,y) corresponds
              to the feature id (x) and the local weight (y). The list is sorted
              by decreasing absolute value of y.
              score is the R^2 value of the returned explanation
              local_pred is the prediction of the explanation model on the original instance
          """
  
          weights = self.kernel_fn(distances)
          labels_column = neighborhood_labels[:, label]
          used_features = self.feature_selection(neighborhood_data,
                                                 labels_column,
                                                 weights,
                                                 num_features,
                                                 feature_selection)
  
          if model_regressor is None:
              model_regressor = Ridge(alpha=1, fit_intercept=True,
                                      random_state=self.random_state)
          easy_model = model_regressor
          easy_model.fit(neighborhood_data[:, used_features],
                         labels_column, sample_weight=weights)
          prediction_score = easy_model.score(
              neighborhood_data[:, used_features],
              labels_column, sample_weight=weights)
  
          local_pred = easy_model.predict(neighborhood_data[0, used_features].reshape(1, -1))
  
          if self.verbose:
              print('Intercept', easy_model.intercept_)
              print('Prediction_local', local_pred,)
              print('Right:', neighborhood_labels[0, label])
          return (easy_model.intercept_,
                  sorted(zip(used_features, easy_model.coef_),
                         key=lambda x: np.abs(x[1]), reverse=True),
                  prediction_score, local_pred)