def _weights(label_id): scores = feature_weights[:, label_id] _x = x if flt_indices is not None: scores = scores[flt_indices] _x = mask(_x, flt_indices) return get_top_features(feature_names, scores, top, _x)
def get_score_feature_weights(_label_id): _score, _feature_weights = scores_weights[_label_id] _x = x if flt_indices is not None: _x = mask(_x, flt_indices) _feature_weights = mask(_feature_weights, flt_indices) return _score, get_top_features(feature_names, _feature_weights, top, _x)
def _weights(label_id): coef = get_coef(clf, label_id) _x = x scores = _multiply(_x, coef) if flt_indices is not None: scores = scores[flt_indices] _x = mask(_x, flt_indices) return get_top_features(feature_names, scores, top, _x)
def get_score_feature_weights(_label_id): _weights = _target_feature_weights( weight_dicts[_label_id], num_features=len(feature_names), bias_idx=feature_names.bias_idx, ) _score = _get_score(weight_dicts[_label_id]) _x = x if flt_indices is not None: _x = mask(_x, flt_indices) _weights = mask(_weights, flt_indices) return _score, get_top_features(flt_feature_names, _weights, top, _x)
def explain_predictions(self, docs, top=30): if not isinstance(self.clf, XGBClassifier): raise NotImplementedError booster = self.clf.booster() xgb_feature_names = {f: i for i, f in enumerate(booster.feature_names)} feature_names = get_feature_names(self.clf, self.vec, num_features=len(xgb_feature_names)) feature_names.bias_name = '<BIAS>' X = self.vec.transform(docs) X = X.tocsc() dmatrix = DMatrix(X, missing=self.clf.missing) leaf_ids = booster.predict(dmatrix, pred_leaf=True) tree_dumps = booster.get_dump(with_stats=True) docs_weights = [] for i, _leaf_ids in enumerate(leaf_ids): all_weights = _target_feature_weights( _leaf_ids, tree_dumps, feature_names=feature_names, xgb_feature_names=xgb_feature_names)[1] weights = np.zeros_like(all_weights) idx = X[i].nonzero()[1] bias_idx = feature_names.bias_idx weights[idx] = all_weights[idx] weights[bias_idx] = all_weights[bias_idx] docs_weights.append(weights) weights = np.mean(docs_weights, axis=0) feature_weights = get_top_features(feature_names=np.array( [_prettify_feature(f) for f in feature_names]), coef=weights, top=top) return Explanation( estimator=type(self.clf).__name__, targets=[TargetExplanation('y', feature_weights=feature_weights)], )
def _features(label_id): return get_top_features(state_feature_names, state_coef[label_id], top)
def _features(target_id): coef = get_coef(reg, target_id, scale=coef_scale) if flt_indices is not None: coef = coef[flt_indices] return get_top_features(feature_names, coef, top)
def _features(label_id): coef = get_coef(clf, label_id, scale=coef_scale) if flt_indices is not None: coef = coef[flt_indices] return get_top_features(feature_names, coef, top)
def _weights(label_id): coef = get_coef(reg, label_id) scores = _multiply(x, coef) return get_top_features(feature_names, scores, top)