利用scikit-learn进行FeatureSelection

编程入门 行业动态 更新时间:2024-10-20 00:43:03

1.单变量特征选择(Univariate feature selection)

>>> from sklearn.datasets import load_iris
>>> from sklearn.feature_selection import SelectKBest
>>> from sklearn.feature_selection import chi2
>>> iris = load_iris()
>>> X, y = iris.data, iris.target
>>> X.shape
(150, 4)
>>> X_new = SelectKBest(chi2, k=2).fit_transform(X, y)
>>> X_new.shape
(150, 2)
除了使用SelectKBest之外,还可以使用SelectPercentile,她是按百分比进行选择的。


2.基于树的特征选择(Tree-based Feature Selection)

>>> from sklearn.ensemble import ExtraTreesClassifier
>>> from sklearn.datasets import load_iris
>>> iris = load_iris()
>>> X, y = iris.data, iris.target
>>> X.shape
(150, 4)
>>> clf = ExtraTreesClassifier()
>>> X_new = clf.fit(X, y).transform(X)
>>> clf.feature_importances_  
array([ 0.04...,  0.05...,  0.4...,  0.4...])
>>> X_new.shape               
(150, 2)

两种方法我都试过,感觉对于人群标签来说,第二种方法要好些。

参考资料:http://scikit-learn/dev/modules/feature_selection.html#feature-selection

                    http://scikit-learn/dev/auto_examples/ensemble/plot_forest_importances.html#example-ensemble-plot-forest-importances-py



更多推荐

利用scikit-learn进行FeatureSelection

本文发布于:2023-06-14 04:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:scikit   learn   FeatureSelection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!