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í)器組成流水線(xiàn),通常流水線(xiàn)的形式為:將數(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í)器流水線(xiàn)(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))
以上就是Python進(jìn)行特征提取的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python 特征提取的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PyQt5按下按鍵選擇文件夾并顯示的實(shí)現(xiàn)
這篇文章主要介紹了PyQt5按下按鍵選擇文件夾并顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
使用python將mdb數(shù)據(jù)庫(kù)文件導(dǎo)入postgresql數(shù)據(jù)庫(kù)示例
mdb格式文件可以通過(guò)mdbtools工具將內(nèi)中包含的每張表導(dǎo)出到csv格式文件。由于access數(shù)據(jù)庫(kù)和postgresQL數(shù)據(jù)庫(kù)格式上會(huì)存在不通性,所以使用python的文件處理,將所得csv文件修改成正確、能識(shí)別的格式2014-02-02
Python實(shí)現(xiàn)連接兩個(gè)無(wú)規(guī)則列表后刪除重復(fù)元素并升序排序的方法
這篇文章主要介紹了Python實(shí)現(xiàn)連接兩個(gè)無(wú)規(guī)則列表后刪除重復(fù)元素并升序排序的方法,涉及Python針對(duì)列表的合并、遍歷、判斷、追加、排序等操作技巧,需要的朋友可以參考下2018-02-02
python -v 報(bào)錯(cuò)問(wèn)題的解決方法
在本篇文章里小編給大家整理了關(guān)于python -v 報(bào)錯(cuò)問(wèn)題的解決方法及相關(guān)知識(shí)點(diǎn),有興趣的朋友們可以學(xué)習(xí)下。2020-09-09
python3操作注冊(cè)表的方法(Url protocol)
使用python操作注冊(cè)表的方法最近學(xué)習(xí)了一下,現(xiàn)在做一下筆記,由于對(duì)Python語(yǔ)言的使用還不是很熟練,所以寫(xiě)不出高大上的結(jié)構(gòu)2020-02-02
對(duì)python中字典keys,values,items的使用詳解
今天小編就為大家分享一篇對(duì)python中字典keys,values,items的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
python實(shí)現(xiàn)猜數(shù)游戲(保存游戲記錄)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)猜數(shù)游戲,保存游戲記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06

