国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現的特征提取操作示例

2020-02-15 23:53:00
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現的特征提取操作。分享給大家供大家參考,具體如下:

# -*- coding: utf-8 -*-"""Created on Mon Aug 21 10:57:29 2017@author: 飄的心"""#過濾式特征選擇#根據方差進行選擇,方差越小,代表該屬性識別能力很差,可以剔除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_ #展現屬性的方差selector.transform(x)#進行特征選擇selector.get_support(True) #選擇結果后,特征之前的索引selector.inverse_transform(selector.transform(x)) #將特征選擇后的結果還原成原始數據                         #被剔除掉的數據,顯示為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,則              #返回的是一個布爾值組成的數組,該數組只是那些特征被選擇selector.transform(x)#包裹時特征選擇from sklearn.feature_selection import RFEfrom sklearn.svm import LinearSVC #選擇svm作為評定算法from sklearn.datasets import load_iris #加載數據集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_  #給出被選出的特征的數量selector.support_   #給出了被選擇特征的maskselector.ranking_   #特征排名,被選出特征的排名為1#注意:特征提取對于預測性能的提升沒有必然的聯系,接下來進行比較;from sklearn.feature_selection import RFEfrom sklearn.svm import LinearSVCfrom sklearn import cross_validationfrom sklearn.datasets import load_iris#加載數據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.947368421053import 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來講多個學習器組成流水線,通常流水線的形式為:將數據標準化,#--》特征提取的學習器————》執行預測的學習器,除了最后一個學習器之后,#前面的所有學習器必須提供transform方法,該方法用于數據轉化(如歸一化、正則化、#以及特征提取#學習器流水線(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))            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太和县| 闽清县| 汾西县| 武宣县| 威宁| 星子县| 潜江市| 静安区| 铁岭县| 柳州市| 蒙自县| 遂昌县| 庄河市| 元朗区| 英超| 松阳县| 大同市| 石屏县| 三亚市| 阜阳市| 米易县| 资兴市| 庆安县| 乌兰浩特市| 吉木萨尔县| 海南省| 内江市| 库伦旗| 龙江县| 调兵山市| 隆德县| 西城区| 墨竹工卡县| 沁源县| 丹凤县| 洪江市| 高清| 钦州市| 五家渠市| 新兴县| 汶川县|