Univariate feature scoring for feature selection.
Each function scores every column of X independently against the target and returns one number per feature, so the columns can be ranked and the weakest dropped before a model ever sees them. Because the scoring is univariate, these metrics are fast but blind to interactions: a feature that is useless alone yet informative in combination will score low.
Which one to reach for:
-
chi2-- non-negative / count features against a class label.
-
f_classif-- continuous features against a class label (ANOVA F). -
f_regression-- continuous features against a continuous target. -
correlation-- absolute Pearson correlation; linear relationships only. -
single_rule_score-- accuracy of a one-rule classifier built on the feature
-
relief_f-- nearest-neighbour based; the only one here that is
The statistical tests return a (scores, pvalues) pair; the ranking-style scorers return scores alone.
>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import f_classif
>>> X, y = load_iris()
>>> scores, pvalues = f_classif(X, y)
>>> int(scores.argmax()) # petal length is the most discriminative
2
Functions
Compute chi-squared statistics between each feature and the class.
Parameters
X
y
Returns
chi2_scores
pvalues
Raises
ValueError
X is negative.
>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import chi2
>>> X, y = load_iris()
>>> scores, pvalues = chi2(X, y)
>>> [round(float(v), 1) for v in scores]
[134.4, 78.0, 244.0, 241.7]
>>> int(scores.argmax())
2
Compute ANOVA F-value between each feature and the class.
Parameters
X
y
Returns
f_scores
pvalues
>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import f_classif
>>> X, y = load_iris()
>>> scores, pvalues = f_classif(X, y)
>>> [round(float(v)) for v in scores]
[119, 47, 1179, 959]
Compute F-statistic and p-value for regression on each feature.
Parameters
X
y
Returns
f_scores
pvalues
Notes
f_classif instead. The statistic tests a LINEAR relationship, so a strong non-linear dependence can still score near zero.>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import f_regression
>>> X, y = load_iris()
>>> scores, pvalues = f_regression(X, y.astype(float))
>>> [round(float(v)) for v in scores]
[234, 32, 1342, 1590]
Compute Pearson correlation coefficient between each feature and the target.
Parameters
X
y
Returns
scores
Notes
relief_f and single_rule_score pick up relationships this misses.>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import correlation
>>> X, y = load_iris()
>>> scores = correlation(X, y.astype(float))
>>> [round(float(v), 2) for v in scores]
[0.78, 0.42, 0.95, 0.96]
Evaluate features by the accuracy of a one-rule classifier built on each.
Parameters
X
y
n_bins
Returns
scores
Notes
correlation this captures non-linear structure, provided it is axis-aligned. Scores are not comparable across datasets with different class balance, since the floor is the majority-class rate.>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import single_rule_score
>>> X, y = load_iris()
>>> scores = single_rule_score(X, y)
>>> [round(float(v), 2) for v in scores]
[0.73, 0.59, 0.91, 0.9]
Compute ReliefF scores for each feature.
Parameters
X
y
n_neighbors
n_samples
random_state
Returns
scores
Notes
>>> from tuiml.datasets import load_iris
>>> from tuiml.evaluation.metrics import relief_f
>>> X, y = load_iris()
>>> scores = relief_f(X, y, random_state=0)
>>> len(scores)
4
>>> int(scores.argmax())
3