from sklearn.feature_selection import SelectKBest
时间: 2023-09-03 16:09:16 浏览: 121
`SelectKBest` is a class in the `sklearn.feature_selection` module that implements univariate feature selection. It selects the best features based on univariate statistical tests like chi-squared test, ANOVA F-test, mutual information score, etc. The `k` parameter specifies the number of features to select. The selected features are ranked by their scores and returned as a numpy array or a sparse matrix.
Here's an example of using `SelectKBest` to select the top 10 features using chi-squared test:
```python
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
# load iris dataset
iris = load_iris()
X, y = iris.data, iris.target
# apply SelectKBest with chi-squared test to select top 10 features
selector = SelectKBest(chi2, k=10)
X_new = selector.fit_transform(X, y)
# get the indices of the selected features
selected_features_indices = selector.get_support(indices=True)
# print the selected feature names
selected_feature_names = [iris.feature_names[i] for i in selected_features_indices]
print(selected_feature_names)
```
This will output the names of the top 10 features selected by `SelectKBest` using chi-squared test.
阅读全文