欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python進(jìn)行特征提取的示例代碼

 更新時(shí)間:2020年10月15日 15:00:12   作者:陸勤_數(shù)據(jù)人網(wǎng)  
這篇文章主要介紹了Python進(jìn)行特征提取的示例代碼,幫助大家更好的進(jìn)行數(shù)據(jù)分析,感興趣的朋友可以了解下
#過濾式特征選擇
#根據(jù)方差進(jìn)行選擇,方差越小,代表該屬性識別能力很差,可以剔除
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作為評定算法
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
 
#注意:特征提取對于預(yù)測性能的提升沒有必然的聯(lián)系,接下來進(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)
#切分測試集與驗(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來講多個(gè)學(xué)習(xí)器組成流水線,通常流水線的形式為:將數(shù)據(jù)標(biāo)準(zhǔn)化,
#--》特征提取的學(xué)習(xí)器————》執(zhí)行預(yù)測的學(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))

以上就是Python進(jìn)行特征提取的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python 特征提取的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python計(jì)算n的階乘的方法代碼

    python計(jì)算n的階乘的方法代碼

    在本篇文章里小編給各位整理的是關(guān)于python計(jì)算n的階乘的相關(guān)知識點(diǎn),需要的朋友們參考下。
    2019-10-10
  • python中的for循環(huán)

    python中的for循環(huán)

    Python for循環(huán)可以遍歷任何序列的項(xiàng)目,如一個(gè)列表或者一個(gè)字符串。這篇文章主要介紹了python的for循環(huán),需要的朋友可以參考下
    2018-09-09
  • Python3.7 dataclass使用指南小結(jié)

    Python3.7 dataclass使用指南小結(jié)

    本文將帶你走進(jìn)python3.7的新特性dataclass,通過本文你將學(xué)會dataclass的使用并避免踏入某些陷阱。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • python通過socket實(shí)現(xiàn)多個(gè)連接并實(shí)現(xiàn)ssh功能詳解

    python通過socket實(shí)現(xiàn)多個(gè)連接并實(shí)現(xiàn)ssh功能詳解

    這篇文章主要介紹了python通過socket實(shí)現(xiàn)多個(gè)連接并實(shí)現(xiàn)ssh功能詳解,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • python同時(shí)替換多個(gè)字符串方法示例

    python同時(shí)替換多個(gè)字符串方法示例

    這篇文章主要介紹了python同時(shí)替換多個(gè)字符串方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Flask模板引擎之Jinja2語法介紹

    Flask模板引擎之Jinja2語法介紹

    這篇文章主要介紹了Flask模板引擎之Jinja2語法介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • matplotlib交互式數(shù)據(jù)光標(biāo)mpldatacursor的實(shí)現(xiàn)

    matplotlib交互式數(shù)據(jù)光標(biāo)mpldatacursor的實(shí)現(xiàn)

    這篇文章主要介紹了matplotlib交互式數(shù)據(jù)光標(biāo)mpldatacursor的實(shí)現(xiàn) ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python ctypes庫2_指定參數(shù)類型和返回類型詳解

    python ctypes庫2_指定參數(shù)類型和返回類型詳解

    今天小編就為大家分享一篇python ctypes庫2_指定參數(shù)類型和返回類型詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python的ImageTk.PhotoImage大坑及解決

    python的ImageTk.PhotoImage大坑及解決

    這篇文章主要介紹了python的ImageTk.PhotoImage大坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • keras實(shí)現(xiàn)圖像預(yù)處理并生成一個(gè)generator的案例

    keras實(shí)現(xiàn)圖像預(yù)處理并生成一個(gè)generator的案例

    這篇文章主要介紹了keras實(shí)現(xiàn)圖像預(yù)處理并生成一個(gè)generator的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06

最新評論