My dataset has columns like 'city' and 'gender'. SelectKBest crashes on them.
Why this happens
All scikit-learn score functions require numeric input. String values like "Delhi" or "male" must be encoded as numbers first.
Answer 1: One-hot encode, then use chi2
One-hot encoding turns each category into its own 0/1 column. These columns are non-negative and count-like, which is exactly what chi2 is designed for.
from sklearn.preprocessing import OneHotEncoder
pipe = Pipeline([
("ohe", OneHotEncoder(handle_unknown="ignore")),
("select", SelectKBest(chi2, k=10)),
])
pipe.fit(X_cat, y)
print(pipe.named_steps["select"].get_feature_names_out(
pipe.named_steps["ohe"].get_feature_names_out()))
Keep in mind that this selects individual category columns (e.g. city_Delhi), not whole original features. You may keep some categories of a column and drop others. That's often fine, but it's worth knowing.
