Python進行特征提取的示例代碼
#過濾式特征選擇#根據(jù)方差進行選擇,方差越小,代表該屬性識別能力很差,可以剔除from sklearn.feature_selection import VarianceThresholdx=[[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)#進行特征選擇selector.get_support(True) #選擇結(jié)果后,特征之前的索引selector.inverse_transform(selector.transform(x)) #將特征選擇后的結(jié)果還原成原始數(shù)據(jù) #被剔除掉的數(shù)據(jù),顯示為0 #單變量特征選擇from sklearn.feature_selection import SelectKBest,f_classifx=[[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個特征,指標使用的是方差分析F值selector.fit(x,y)selector.scores_ #每一個特征的得分selector.pvalues_selector.get_support(True) #如果為true,則返回被選出的特征下標,如果選擇False,則 #返回的是一個布爾值組成的數(shù)組,該數(shù)組只是那些特征被選擇selector.transform(x) #包裹時特征選擇from sklearn.feature_selection import RFEfrom sklearn.svm import LinearSVC #選擇svm作為評定算法from sklearn.datasets import load_iris #加載數(shù)據(jù)集iris=load_iris()x=iris.datay=iris.targetestimator=LinearSVC()selector=RFE(estimator=estimator,n_features_to_select=2) #選擇2個特征selector.fit(x,y)selector.n_features_ #給出被選出的特征的數(shù)量selector.support_ #給出了被選擇特征的maskselector.ranking_ #特征排名,被選出特征的排名為1 #注意:特征提取對于預測性能的提升沒有必然的聯(lián)系,接下來進行比較;from sklearn.feature_selection import RFEfrom sklearn.svm import LinearSVCfrom sklearn import cross_validationfrom sklearn.datasets import load_iris #加載數(shù)據(jù)iris=load_iris()X=iris.datay=iris.target#特征提取estimator=LinearSVC()selector=RFE(estimator=estimator,n_features_to_select=2)X_t=selector.fit_transform(X,y)#切分測試集與驗證集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.973684210526print(’selected Dataset:test score:’,clf_t.score(x_test_t,y_test_t))#selected Dataset:test score: 0.947368421053 import numpy as npfrom sklearn.feature_selection import RFECVfrom sklearn.svm import LinearSVCfrom sklearn.datasets import load_irisiris=load_iris()x=iris.datay=iris.targetestimator=LinearSVC()selector=RFECV(estimator=estimator,cv=3)selector.fit(x,y)selector.n_features_selector.support_selector.ranking_selector.grid_scores_#嵌入式特征選擇import numpy as npfrom sklearn.feature_selection import SelectFromModelfrom sklearn.svm import LinearSVCfrom sklearn.datasets import load_digitsdigits=load_digits()x=digits.datay=digits.targetestimator=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來講多個學習器組成流水線,通常流水線的形式為:將數(shù)據(jù)標準化,#--》特征提取的學習器————》執(zhí)行預測的學習器,除了最后一個學習器之后,#前面的所有學習器必須提供transform方法,該方法用于數(shù)據(jù)轉(zhuǎn)化(如歸一化、正則化、#以及特征提取#學習器流水線(pipeline)from sklearn.svm import LinearSVCfrom sklearn.datasets import load_digitsfrom sklearn import cross_validationfrom sklearn.linear_model import LogisticRegressionfrom sklearn.pipeline import Pipelinedef 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))
以上就是Python進行特征提取的示例代碼的詳細內(nèi)容,更多關(guān)于Python 特征提取的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Struts2獲取參數(shù)的三種方法總結(jié)2. JSP中Servlet的Request與Response的用法與區(qū)別3. IntelliJ IDEA刪除類的方法步驟4. js select支持手動輸入功能實現(xiàn)代碼5. Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進程6. vue cli4下環(huán)境變量和模式示例詳解7. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式8. Django視圖類型總結(jié)9. IntelliJ IDEA導入jar包的方法10. Xml簡介_動力節(jié)點Java學院整理
