跟着Leo机器学习:sklearn之Feature selection
一个很有趣的个人博客,不信你来撩 fangzengye.com
sklearn框架

函数导图

1.13. Feature selection
1.13.1. Removing features with low variance
from sklearn.feature_selection import VarianceThreshold
X = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 1, 0], [0, 1, 1]]
sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
sel.fit_transform(X)
1.13.2. Univariate feature selection
from sklearn.datasets import load_iris from sklearn.feature_selection import SelectKBest from sklearn.feature_selection import chi2 X, y = load_iris(return_X_y=True) X.shape
X_new = SelectKBest(chi2, k=2).fit_transform(X, y)
X_new.shape
1.13.4. Feature selection using SelectFromModel
1.13.4.1. L1-based feature selection
from sklearn.svm import LinearSVC from sklearn.datasets import load_iris from sklearn.feature_selection import SelectFromModel X, y = load_iris(return_X_y=True) X.shape
lsvc = LinearSVC(C=0.01, penalty="l1", dual=False).fit(X, y)
model = SelectFromModel(lsvc, prefit=True)
X_new = model.transform(X)
X_new.shape
1.13.4.2. Tree-based feature selection
from sklearn.ensemble import ExtraTreesClassifier from sklearn.datasets import load_iris from sklearn.feature_selection import SelectFromModel X, y = load_iris(return_X_y=True) X.shapeclf = ExtraTreesClassifier(n_estimators=50)
clf = clf.fit(X, y)
clf.feature_importances_
model = SelectFromModel(clf, prefit=True)
X_new = model.transform(X)
X_new.shape
1.13.5. Feature selection as part of a pipeline
clf = Pipeline([
('feature_selection', SelectFromModel(LinearSVC(penalty="l1"))),
('classification', RandomForestClassifier())
])
clf.fit(X, y)
源地址
https://scikit-learn.org/stable/modules/feature_selection.html
我的个人博客fangzengye.com, 欢迎来撩哦!
原文博主: 热衷开源的宝藏Boy
版权声明: 自由转载-非商用-禁止演绎-保持署名| CC BY-NC-ND 3.0
浙公网安备 33010602011771号