I'm getting this error with SelectKBest(chi2, k=5). I applied StandardScaler before feature selection, so some values are negative.
Why this happens
The chi-squared test treats each feature value as a frequency or count, such as how many times a word appears in a document. A negative count has no meaning, so scikit-learn checks for negative values and refuses to run. StandardScaler centers each feature at zero, so about half your values become negative, and you hit this error.
Answer 1: Scale to [0, 1] with MinMaxScaler instead of StandardScaler
MinMaxScaler maps every feature into the range [0, 1], so no value is negative and chi2 will run.
from sklearn.preprocessing import MinMaxScaler
from sklearn.feature_selection import chi2
from sklearn.pipeline import Pipeline
pipe = Pipeline([
("scale", MinMaxScaler()),
("select", SelectKBest(chi2, k=5)),
])
X_new = pipe.fit_transform(X, y)
A word of caution: this silences the error, but chi2 on scaled continuous data is not a statistically meaningful test. The ranking it gives may still be useful in practice. If your features are truly continuous, Answer 2 is usually the better choice.
