前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实现的特征提取操作示例

Python实现的特征提取操作示例

作者头像
砸漏
发布2020-10-20 10:13:05
5310
发布2020-10-20 10:13:05
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例讲述了Python实现的特征提取操作。分享给大家供大家参考,具体如下:

代码语言:javascript
复制
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 21 10:57:29 2017
@author: 飘的心
"""
#过滤式特征选择
#根据方差进行选择,方差越小,代表该属性识别能力很差,可以剔除
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_ #展现属性的方差
selector.transform(x)#进行特征选择
selector.get_support(True) #选择结果后,特征之前的索引
selector.inverse_transform(selector.transform(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_
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,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来讲多个学习器组成流水线,通常流水线的形式为:将数据标准化,
#--》特征提取的学习器――――》执行预测的学习器,除了最后一个学习器之后,
#前面的所有学习器必须提供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(cross_validation.train_test_split(x,y,test_size=0.25,
random_state=0,stratify=y))

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:
  • python实现图片处理和特征提取详解
  • Python进行数据提取的方法总结
  • Python中使用NLTK库实现对词干的提取的教程
  • python-opencv在有噪音的情况下提取图像的轮廓实例
  • 详解Python3中字符串中的数字提取方法
  • python实现提取百度搜索结果的方法
  • python提取页面内url列表的方法
  • python 根据正则表达式提取指定的内容实例详解
  • python读取视频流提取视频帧的两种方法
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 您可能感兴趣的文章:
相关产品与服务
图片处理
图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档