ValueError: No feature in X meets the variance threshold

Viewed 1259

I'm trying to use VarianceThreshold and I'm getting error:

ValueError: No feature in X meets the variance threshold 0.16000

My code:

    from sklearn.feature_selection import VarianceThreshold
    sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
    sel.fit(X)

X has the following properties:

Shape =  (4898, 11)
Type: =  <class 'pandas.core.frame.DataFrame'>

What is wrong ?

1 Answers

The error message tells you what's wrong: none of the 10 features meets the variance requirement. Therefore, there are no remaining features to use for fitting your data.

Since you neglected to include an actual example, we can't do much in the way of further diagnosis or repair.


Yes, you can get an empty result, but you have to catch an except that appears to be not directly documented. If you feed VarianceThreshold a problem with no solution -- in that no single feature provides the required threshold influence on the labels -- then it throws the ValueError exception you got.

You convert this as you would any other such problem, with an exception handler:

from sklearn.feature_selection import VarianceThreshold

try:
    sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
    sel.fit(X)

except ValueError:
    print("No feature is strong enough to keep")
    sel = []

Does this solve your problem? I did find two examples that were buried within functions; in those instances, the function's exception handler simply used return 0 for any exception.

Related