1. 程式人生 > 程式設計 >Python進行特徵提取的示例程式碼

Python進行特徵提取的示例程式碼

#過濾式特徵選擇
#根據方差進行選擇,方差越小,代表該屬性識別能力很差,可以剔除
from sklearn.feature_selection import VarianceThreshold
x=[[100,1,2,3],[100,4,5,6],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_classif
x=[[1,3,5],[5,1],[3,[1,1]]
y=[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 RFE
from sklearn.svm import LinearSVC #選擇svm作為評定演算法
from sklearn.datasets import load_iris #載入資料集
iris=load_iris()
x=iris.data
y=iris.target
estimator=LinearSVC()
selector=RFE(estimator=estimator,n_features_to_select=2) #選擇2個特徵
selector.fit(x,y)
selector.n_features_  #給出被選出的特徵的數量
selector.support_   #給出了被選擇特徵的mask
selector.ranking_   #特徵排名,被選出特徵的排名為1
 
#注意:特徵提取對於預測效能的提升沒有必然的聯絡,接下來進行比較;
from sklearn.feature_selection import RFE
from sklearn.svm import LinearSVC
from sklearn import cross_validation
from sklearn.datasets import load_iris
 
#載入資料
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)
#切分測試集與驗證集
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,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來講多個學習器組成流水線,通常流水線的形式為:將資料標準化,
#--》特徵提取的學習器————》執行預測的學習器,除了最後一個學習器之後,
#前面的所有學習器必須提供transform方法,該方法用於資料轉化(如歸一化、正則化、
#以及特徵提取
#學習器流水線(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,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,stratify=y))

以上就是Python進行特徵提取的示例程式碼的詳細內容,更多關於Python 特徵提取的資料請關注我們其它相關文章!