Python實(shí)現(xiàn)的特征提取操作示例
本文實(shí)例講述了Python實(shí)現(xiàn)的特征提取操作。分享給大家供大家參考,具體如下:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 21 10:57:29 2017
@author: 飄的心
"""
#過(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))
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
pyenv與virtualenv安裝實(shí)現(xiàn)python多版本多項(xiàng)目管理
這篇文章主要介紹了pyenv與virtualenv安裝實(shí)現(xiàn)python多版本多項(xiàng)目管理過(guò)程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
找Python安裝目錄,設(shè)置環(huán)境路徑以及在命令行運(yùn)行python腳本實(shí)例
這篇文章主要介紹了找Python安裝目錄,設(shè)置環(huán)境路徑以及在命令行運(yùn)行python腳本實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
PyCharm使用之配置SSH Interpreter的方法步驟
這篇文章主要介紹了PyCharm使用之配置SSH Interpreter的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī)
這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
python爬取淘寶商品詳情頁(yè)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python爬取淘寶商品詳情頁(yè)數(shù)據(jù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
基于本地知識(shí)的問(wèn)答機(jī)器人langchain-ChatGLM 大語(yǔ)言模型實(shí)現(xiàn)方法詳解
這篇文章主要介紹了基于本地知識(shí)的問(wèn)答機(jī)器人langchain-ChatGLM 大語(yǔ)言模型實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了langchain-ChatGLM的功能、原理、部署方法與操作注意事項(xiàng),需要的朋友可以參考下2023-07-07

