您现在的位置是:热点 >>正文
机器学习5种特征选择的方法!
热点8428人已围观
简介...
我们知道模型的性能会随着使用特征数量的增加而增加。但是,当超过峰值时,模型性能将会下降。这就是为什么我们只需要选择能够有效预测的特征的原因。
特征选择类似于降维技术,其目的是减少特征的数量,但是从根本上说,它们是不同的。区别在于要素选择会选择要保留或从数据集中删除的要素,而降维会创建数据的投影,从而产生全新的输入要素。
特征选择有很多方法,在本文中我将介绍 Scikit-Learn 中 5 个方法,因为它们是最简单但却非常有用的,让我们开始吧。
1、方差阈值特征选择
具有较高方差的特征表示该特征内的值变化大,较低的方差意味着要素内的值相似,而零方差意味着您具有相同值的要机器素。
方差选择法,先要计算各个特征的方差,然后根据阈值,选择方差大于阈值的特征,使用方法我们举例说明:
import pandas as pd import seaborn as sns mpg = sns.load_dataset('mpg').select_dtypes('number') mpg.head()
对于此示例,我仅出于简化目的使用数字特征。在使用方差阈值特征选择之前,我们需要对所有这些数字特征进行转换,因为方差受数字刻度的影响。
f学习rom sklearn.preprocessing import StandardScaler scaler = StandardScaler() mpg = pd.DataFrame(scaler.fit_transform(mpg), columns = mpg.columns) mpg.head()
所有特征都在同一比例上,让我们尝试仅使用方差阈值方法选择我们想要的特征。假设我的方差限制为一个方差。
from 5种sklearn.feature_selection import VarianceThreshold selector = VarianceThreshold(1) selector.fit(mpg) mpg.columns[selector.get_support()]
方差阈值是一种无监督学习的特征选择方法。如果我们希望出于监督学习的目的而选择功能怎么办?那就是我们接下来要讨论的。
2、SelectKBest特征特征
单变量特征选择是一种基于单变量统计检验的方法,例如:chi特征2,Pearson等等。
SelectKBest 的前提是将未经验证的统计测试与基于 X 和 y 之间的统计结果选择 K 数的特征相结合。
mpg = sns.load_dataset('mpg') mpg = mpg.select_dtypes('number').dropna() #Divide the features into Independent and Dependent Variable X = mpg.选择drop('mpg' , axis =1) y = mpg['mpg']
由于单变量特征选择方法旨在进行监督学习,因此我们将特征分为独立变量和因变量。接下来,我们将使用SelectKBest,假设我只想要最重要的两个特征。
from sklearn.feature_selection import SelectKBest, mutual_info_regression #Select top 2 featur的方es based on mutual info regression selector = SelectKBest(mutual_info_regression, k =2) selector.fit(X, y) X.columns[selector.get_support()] 3、递归特征消除(RFE)
递归特征消除或RFE是一种特征选择方法,利用机器学习模型通过在递归训练后消除最不重要的特征来选择特征。
根据Scikit-L法!earn,RFE是一种通过递归考虑越来越少的特征集来选择特征的方法。
首先对估计器进行初始特征集训练,然后通过coef_attribute或feature_importances_attribute获得每个特征的重要性。 然后从当前特征中删除最不重要的特征。在修剪后的数据集上递归地重复该过程,直到最终达到所需的要选择的特征数量。
在此示例中,我想使用泰坦尼克号数据集进行分类问题,在那里我想预测谁将生存下来。
#Load the dataset and only selecting the nu机器merical features for example purposes titanic = sns.load_dataset('titanic')[['survived', 'pclass', 'age', 'parch', 'sibsp', 'fare']].dropna() X = titanic.drop('survived', axis = 1) y =学习 titanic['survived']
我想看看哪些特征最能帮助我预测谁可以幸免于泰坦尼克号事件。让我们使用LogisticRegression模型获得最佳特征。
from sklearn.feature_selection import RFE from sklearn.linear_model import LogisticRegression # #Selecting the Best important 5种features according to Logistic Regression rfe_selector = RFE(estimator=LogisticRegression(),n_features_to_select = 2, step = 1) rfe_selector.fit(X, y) X.columns[rfe_selector.get_support()]
默认情况下,为RFE选择的特征数特征是全部特征的中位数,步长是1.当然,你可以根据自己的经验进行更改。
4、SelectFromModel 特征选择
Scikit-Learn 的 SelectFromModel 用于选择特征的机器学习模型估计,它基于重要性属性阈值。默认情况下,阈值是平均值。
让我们使用一个数据集示例来更好地理解这一概念。我将使用之前的数据。
from sklearn.feature_selection import SelectFromModel sfm_selector = SelectFromModel(estimator=Lo选择gisticRegression()) sfm_selector.fit(X, y) X.columns[sfm_selector.get_support()]
与RFE一样,你可以使用任何机器学习模型来选择功能,只要可以调用它来估计特征重要性即可。你可以使用随机森林模或XGBoost进行尝试。
5、顺序特征选择(SFS)
顺序特征选择是一种贪婪算法,用于根据交叉验证得分和估计量来向前或向后查找最佳特征,它是 Scikit-Learn 版本0.24中的新增功能。方法如下:
SFS-Forward 通过从零个特征开始进行功能选择,并的方找到了一个针对单个特征训练机器学习模型时可以最大化交叉验证得分的特征。 一旦选择了第一个功能,便会通过向所选功能添加新功能来重复该过程。当我们发现达到所需数量的功能时,该过程将停止。
让我们举一个例子说明。
from sklearn.feature_selection import SequentialFeatureSelector sfs_selector = SequentialFeatureSelector(estimator=LogisticRegression(), n_法!features_to_select = 3, cv =10, direction ='backward') sfs_selector.fit(X, y) X.columns[sfs_selector.get_support()] 结论
特征选择是机器学习模型中的一个重要方面,对于模型无用的特征,不仅影响模型的训练速度,同时也会影响模型的效果。
Tags:
相关文章
Adobe王炸更新:Firefly智能体公测,Photoshop竟能“自如编排”?
热点Adobe公司于 4 月 28 日正式宣布,其备受期待的Firefly AI Assistant(Firefly AI助手)正式进入公测阶段,并开始向全球符合条件的订阅用户开放。首批体验名单涵盖了Cr...
【热点】
阅读更多谈谈基于深度学习的目标检测网络为什么会误检,以及如何优化目标检测的误检问题
热点对于以人脸检测为代表的目标检测深度学习网络来说,误检是一件非常恼人的事情。把狗检测为猫尚可接受,毕竟有些狗的确长得像猫,但是把墙壁、灯泡、拳头、衣服检测成人脸就不能忍了,明明一点都不像。稍稍思考下,我...
【热点】
阅读更多亚马逊四季度净销售额1492亿美元 云计算业务营收仍在增加
热点2月3日消息,据国外媒体报道,当地时间周四,苹果、高通、亚马逊、谷歌母公司Alphabet等多家公司发布了最新一个季度或财季的财报,同其他科技巨头一样,电商及云计算巨头亚马逊去年四季度的利润,同比也大...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.nss.nhs.scot/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,XWD
- https://www.seagrant.wisc.edu/?page=63&s=谷歌maps群发程序|软件【tg:seogoogle987】,QIN
- https://www.temu.com/search_result.html?search_key=谷歌maps群发程序|软件【tg:seogoogle987】ie5
- https://www.riohondo.edu/?action=QUERY&s=谷歌maps群发程序|软件【tg:seogoogle987】,aNG
- https://www.lee.com/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,QtT
- https://wwwcp.umes.edu/search/谷歌maps群发程序|软件【tg:seogoogle987】,zYD
- https://www.wattpad.com/search/谷歌maps群发程序|软件【tg:seogoogle987】qpb
- https://www.nonsolocap.it/cap?action=SEARCH&k=谷歌maps群发程序|软件【tg:seogoogle987】,jhM
- https://www.monroecounty.gov/search/?q=谷歌maps群发程序|软件【tg:seogoogle987】,RGh
- https://www.jusoen.com/addreng.asp?p1=谷歌maps群发程序|软件【tg:seogoogle987】egl
- https://www.luxurysociety.com/en/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,7hu
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌maps群发程序|软件【tg:seogoogle987】,fuF
- https://www.msrenewal.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,978
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=谷歌maps群发程序|软件【tg:seogoogle987】,7C1
- https://www.springfieldlibrary.org/?s=谷歌maps群发程序|软件【tg:seogoogle987】,mYY
- https://www.smarty.net/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,fKy
- https://www.soccerbible.com/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,jf6
- https://www.ucycle.com/search/=谷歌maps群发程序|软件【tg:seogoogle987】,jws
- https://www.parriscues.com/catalogsearch/result/?q=谷歌maps群发程序|软件【tg:seogoogle987】,BGg
- https://www.tcv.org.uk/search/谷歌maps群发程序|软件【tg:seogoogle987】,XVj
- https://www.mexperience.com/?T=1686132112324&s=谷歌maps群发程序|软件【tg:seogoogle987】,lQR
- https://www.tmtpost.com/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,oKW
- https://www.youm7.com/Home/Search?allwords=谷歌maps群发程序|软件【tg:seogoogle987】,DA1
- https://xn--lgernevedlystskoven-lxb.dk/search/谷歌maps群发程序|软件【tg:seogoogle987】,8q0
- https://www.parl.ca/legisinfo/fr/projets-de-loi?parlsession=all&view=list&keywords=谷歌maps群发程序|软件【tg:seogoogle987】,bgz
- https://www.u-bourgogne.fr/?cat=117&s=谷歌maps群发程序|软件【tg:seogoogle987】,KKB
- https://www2.ual.es/ProyectoLates/?s=谷歌maps群发程序|软件【tg:seogoogle987】,gZ0
- https://search.danawa.com/dsearch.php?query=谷歌maps群发程序|软件【tg:seogoogle987】qxJ
- https://nicolazzi.it/en/?s=谷歌maps群发程序|软件【tg:seogoogle987】,vWz
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=谷歌maps群发程序|软件【tg:seogoogle987】,Ubr
- https://www.randjleather.co.uk/index.php?route=product/search&tag=谷歌maps群发程序|软件【tg:seogoogle987】,d29
- https://www.smics.com/tc/site/search?token=谷歌maps群发程序|软件【tg:seogoogle987】,iZp
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌maps群发程序|软件【tg:seogoogle987】,pbM
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌maps群发程序|软件【tg:seogoogle987】w6I
- https://www.reginatangoshoes.com/en/catalogsearch/result/index/?T=1686132113407&q=谷歌maps群发程序|软件【tg:seogoogle987】,j6A
- https://www.qidulp.com/search?query=谷歌maps群发程序|软件【tg:seogoogle987】,kg1
- https://www.wuv.edu/?s=谷歌maps群发程序|软件【tg:seogoogle987】,9UW
- https://blvdseafood.com/?s=谷歌maps群发程序|软件【tg:seogoogle987】x3S
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌maps群发程序|软件【tg:seogoogle987】8qt
- https://www.tullygroup.us/?ID=hbkq8jFjrQlVRrwzEmvoUJJAY4e1Lfo5&s=谷歌maps群发程序|软件【tg:seogoogle987】,X7D
- https://www.shopping.com/search.html?keyword=谷歌maps群发程序|软件【tg:seogoogle987】,9ig
- https://hsmoa.com/search?query=谷歌maps群发程序|软件【tg:seogoogle987】CD6
- https://www.solerpalau.com/en-en/catalogsearch/result/?q=谷歌maps群发程序|软件【tg:seogoogle987】,rPC
- https://www.livehindustan.com/tags/谷歌maps群发程序|软件【tg:seogoogle987】,iin
- https://www.navymwr.org/search/?query=谷歌maps群发程序|软件【tg:seogoogle987】,ywd
- https://www.universal-music.co.jp/search/?q=谷歌maps群发程序|软件【tg:seogoogle987】,3kQ
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌maps群发程序|软件【tg:seogoogle987】,obP
- https://www.thealexandrian.net/?s=谷歌maps群发程序|软件【tg:seogoogle987】,T1v
- https://www.flickr.com/photos/tags/谷歌maps群发程序|软件【tg:seogoogle987】s8a
- https://www.openspaceinstitute.org/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,MSb