Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_argsort_k_largest_positive():
assert _np_eq(argsort_k_largest_positive(np.array([1.0, 0.0, 2.0]), None),
np.array([2, 0]))
assert _np_eq(argsort_k_largest_positive(np.array([1.0, 0.0, 2.0, 4.0]), 2),
np.array([3, 2]))
def test_argsort_k_largest_empty():
x = np.array([0])
empty = np.array([])
assert _np_eq(x[argsort_k_largest(x, 0)], empty)
assert _np_eq(x[argsort_k_largest_positive(x, None)], empty)
def test_argsort_k_largest_positive():
assert _np_eq(argsort_k_largest_positive(np.array([1.0, 0.0, 2.0]), None),
np.array([2, 0]))
assert _np_eq(argsort_k_largest_positive(np.array([1.0, 0.0, 2.0, 4.0]), 2),
np.array([3, 2]))
def _get_top_abs_features(feature_names, coef, k, x):
indices = argsort_k_largest_positive(np.abs(coef), k)
features = _features(indices, feature_names, coef, x)
pos = [fw for fw in features if fw.weight > 0]
neg = [fw for fw in features if fw.weight < 0]
return pos, neg
def get_feature_importances_filtered(coef, feature_names, flt_indices, top,
coef_std=None):
if flt_indices is not None:
coef = coef[flt_indices]
if coef_std is not None:
coef_std = coef_std[flt_indices]
indices = argsort_k_largest_positive(coef, top)
names, values = feature_names[indices], coef[indices]
std = None if coef_std is None else coef_std[indices]
return FeatureImportances.from_names_values(
names, values, std,
remaining=np.count_nonzero(coef) - len(indices),
)
def _get_top_positive_features(feature_names, coef, k, x):
indices = argsort_k_largest_positive(coef, k)
return _features(indices, feature_names, coef, x)