python特征提取代码_Python进⾏特征提取的⽰例代码Python进⾏特征提取的⽰例代码,特征,⽅差,数据,的是,流⽔线
Python进⾏特征提取的⽰例代码
易采站长站,站长之家为您整理了Python进⾏特征提取的⽰例代码的相关内容。
#过滤式特征选择
#根据⽅差进⾏选择,⽅差越⼩,代表该属性识别能⼒很差,可以剔除
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_ #展现属性的⽅差
<_support(True) #选择结果后,特征之前的索引
selector.inverse_ansform(x)) #将特征选择后的结果还原成原始数据
#被剔除掉的数据,显⽰为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个特征,指标使⽤的是⽅差分析F值
selector.fit(x,y)
selector.scores_ #每⼀个特征的得分
selector.pvalues_
<_support(True) #如果为true,则返回被选出的特征下标,如果选择False,则
#返回的是⼀个布尔值组成的数组,该数组只是那些特征被选择
#包裹时特征选择
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()
python新手代码示例selector=RFE(estimator=estimator,n_features_to_select=2)
X_t=selector.fit_transform(X,y)
#切分测试集与验证集
x_train,x_test,y_train,y_test=ain_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=ain_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_
#嵌⼊式特征选择
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.threshold_
<_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,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(ain_test_split(x,y,test_size=0.25,
random_state=0,stratify=y))
以上就是Python进⾏特征提取的⽰例代码的详细内容,更多关于Python 特征提取的资料请关注易采站长站其它相关⽂章!以上就是关于对Python进⾏特征提取的⽰例代码的详细介绍。欢迎⼤家对Python进⾏特征提取的⽰例代码内容提出宝贵意见

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。