Python進(jìn)行特征提取
#過(guò)濾式特征選擇
#根據(jù)方差進(jìn)行選擇,方差越小,代表該屬性識(shí)別能力很差,可以剔除
from?sklearn.feature_selection?import?VarianceThreshold
x=[[100,1,2,3],
???[100,4,5,6],
???[100,7,8,9],
???[101,11,12,13]]
selector=VarianceThreshold(1)??#方差閾值值,
selector.fit(x)
selector.variances_??#展現(xiàn)屬性的方差
selector.transform(x)#進(jìn)行特征選擇
selector.get_support(True)??#選擇結(jié)果后,特征之前的索引
selector.inverse_transform(selector.transform(x))?#將特征選擇后的結(jié)果還原成原始數(shù)據(jù)
??????????????????????????????????????????????????#被剔除掉的數(shù)據(jù),顯示為0
??????????????????????????????????????????????????
#單變量特征選擇
from?sklearn.feature_selection?import?SelectKBest,f_classif
x=[[1,2,3,4,5],
???[5,4,3,2,1],
???[3,3,3,3,3],
???[1,1,1,1,1]]
y=[0,1,0,1]
selector=SelectKBest(score_func=f_classif,k=3)#選擇3個(gè)特征,指標(biāo)使用的是方差分析F值
selector.fit(x,y)
selector.scores_?#每一個(gè)特征的得分
selector.pvalues_
selector.get_support(True)?#如果為true,則返回被選出的特征下標(biāo),如果選擇False,則
????????????????????????????#返回的是一個(gè)布爾值組成的數(shù)組,該數(shù)組只是那些特征被選擇
selector.transform(x)
?
?
#包裹時(shí)特征選擇
from?sklearn.feature_selection?import?RFE
from?sklearn.svm?import?LinearSVC??#選擇svm作為評(píng)定算法
from?sklearn.datasets?import?load_iris?#加載數(shù)據(jù)集
iris=load_iris()
x=iris.data
y=iris.target
estimator=LinearSVC()
selector=RFE(estimator=estimator,n_features_to_select=2)?#選擇2個(gè)特征
selector.fit(x,y)
selector.n_features_???#給出被選出的特征的數(shù)量
selector.support_??????#給出了被選擇特征的mask
selector.ranking_??????#特征排名,被選出特征的排名為1
?
#注意:特征提取對(duì)于預(yù)測(cè)性能的提升沒(méi)有必然的聯(lián)系,接下來(lái)進(jìn)行比較;
from?sklearn.feature_selection?import?RFE
from?sklearn.svm?import?LinearSVC
from?sklearn?import?cross_validation
from?sklearn.datasets?import?load_iris
?
#加載數(shù)據(jù)
iris=load_iris()
X=iris.data
y=iris.target
#特征提取
estimator=LinearSVC()
selector=RFE(estimator=estimator,n_features_to_select=2)
X_t=selector.fit_transform(X,y)
#切分測(cè)試集與驗(yàn)證集
x_train,x_test,y_train,y_test=cross_validation.train_test_split(X,y,
????????????????????????????????????test_size=0.25,random_state=0,stratify=y)
x_train_t,x_test_t,y_train_t,y_test_t=cross_validation.train_test_split(X_t,y,
????????????????????????????????????test_size=0.25,random_state=0,stratify=y)
?
?
clf=LinearSVC()
clf_t=LinearSVC()
clf.fit(x_train,y_train)
clf_t.fit(x_train_t,y_train_t)
print('origin?dataset?test?score:',clf.score(x_test,y_test))
#origin?dataset?test?score:?0.973684210526
print('selected?Dataset:test?score:',clf_t.score(x_test_t,y_test_t))
#selected?Dataset:test?score:?0.947368421053
?
import?numpy?as?np
from?sklearn.feature_selection?import?RFECV
from?sklearn.svm?import?LinearSVC
from?sklearn.datasets?import?load_iris
iris=load_iris()
x=iris.data
y=iris.target
estimator=LinearSVC()
selector=RFECV(estimator=estimator,cv=3)
selector.fit(x,y)
selector.n_features_
selector.support_
selector.ranking_
selector.grid_scores_
#嵌入式特征選擇
import?numpy?as?np
from?sklearn.feature_selection?import?SelectFromModel
from?sklearn.svm?import?LinearSVC
from?sklearn.datasets?import?load_digits
digits=load_digits()
x=digits.data
y=digits.target
estimator=LinearSVC(penalty='l1',dual=False)
selector=SelectFromModel(estimator=estimator,threshold='mean')
selector.fit(x,y)
selector.transform(x)
selector.threshold_
selector.get_support(indices=True)
?
#scikitlearn提供了Pipeline來(lái)講多個(gè)學(xué)習(xí)器組成流水線,通常流水線的形式為:將數(shù)據(jù)標(biāo)準(zhǔn)化,
#--》特征提取的學(xué)習(xí)器————》執(zhí)行預(yù)測(cè)的學(xué)習(xí)器,除了最后一個(gè)學(xué)習(xí)器之后,
#前面的所有學(xué)習(xí)器必須提供transform方法,該方法用于數(shù)據(jù)轉(zhuǎn)化(如歸一化、正則化、
#以及特征提取
#學(xué)習(xí)器流水線(pipeline)
from?sklearn.svm?import?LinearSVC
from?sklearn.datasets?import?load_digits
from?sklearn?import?cross_validation
from?sklearn.linear_model?import?LogisticRegression
from?sklearn.pipeline?import?Pipeline
def?test_Pipeline(data):
????x_train,x_test,y_train,y_test=data
????steps=[('linear_svm',LinearSVC(C=1,penalty='l1',dual=False)),
???????????('logisticregression',LogisticRegression(C=1))]
????pipeline=Pipeline(steps)
????pipeline.fit(x_train,y_train)
????print('named?steps',pipeline.named_steps)
????print('pipeline?score',pipeline.score(x_test,y_test))
????
if?__name__=='__main__':
????data=load_digits()
????x=data.data
????y=data.target
????test_Pipeline(cross_validation.train_test_split(x,y,test_size=0.25,
????????????????????????????????????random_state=0,stratify=y))
公眾號(hào)推薦:數(shù)據(jù)思踐
數(shù)據(jù)思踐公眾號(hào)記錄和分享數(shù)據(jù)人思考和踐行的內(nèi)容與故事。
《數(shù)據(jù)科學(xué)與人工智能》公眾號(hào)推薦朋友們學(xué)習(xí)和使用Python語(yǔ)言,需要加入Python語(yǔ)言群的,請(qǐng)掃碼加我個(gè)人微信,備注【姓名-Python群】,我誠(chéng)邀你入群,大家學(xué)習(xí)和分享。
? ? 關(guān)于Python語(yǔ)言,有任何問(wèn)題或者想法,請(qǐng)留言或者加群討論。
評(píng)論
圖片
表情
