# -*- coding: utf-8 -*-
"""
Created on Thu Oct 27 10:53:56 2016
@author: sirius
feature chioce
"""
"""
---------------------特征处理之--过滤型-----------------------------------
评估单个特征和结果值之间的相关程度,排序留下TOP相关的特征部分
"""
"""
用到的方法:
Pearson相关系数、互信息、距离相关度
缺点:
没有考虑到特征之间的关联作用,可能把有用的关联特征误踢掉,工业界用得较少
"""
import numpy as np
from scipy.stats import pearsonr
np.random.seed(0)
size=300
x_data=np.random.normal(0,1,size) #正态分布,0期望,1方差
print "Lower noise",pearsonr(x_data,x_data+np.random.normal(0,1,size))
print "Higher noise",pearsonr(x_data,x_data+np.random.normal(0,10,size))
"""
>>>
Lower noise (0.71824836862138408, 7.3240173129983507e-49) #第一个值为pearson相关度
Higher noise (0.057964292079338155, 0.31700993885324752)
"""
"""
例子,调用sklearn中iris数据集,判断单个特征和结果值的相关程度
"""
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import SelectPercentile
from sklearn.feature_selection import chi2
iris=load_iris()
x,y=iris.data,iris.target
print x.shape
"""
>>>
(150L, 4L)
"""
x_new=SelectKBest(chi2,k=2).fit_transform(x,y)#k=2表示把相关度最高的两个返回
x_new2=SelectPercentile(chi2,percentile=75).fit_transform(x,y)
#percentile=75是取按相关度从高到低排列的特征的前75%部分
print x_new.shape
print x_new2.shape
"""
>>>
(150L, 2L)
(150L, 3L)
"""
"""
-----------------------------------特征处理之包裹型-------------------------
把特征选择看作一个特征子集搜索问题,筛选各种特征子集,用模型评估结果,典型的包裹型算法为
“递归特征删除算法” recursive feature elimination algorithm
例如,在逻辑回归中:首先用全量的特征跑一个模型(用AUC来评估,包括准确率和召回率),
得到线性模型的系数 (theta1*x1+theta2*x2+theta3*x3+...) ,把theta从大到小排列,删掉5-10%的弱特征,
再观测模型的AUC变化,如果没有明显的变化,则继续删除,逐步进行,AUC出现大的下滑停止
"""
from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression
from sklearn import datasets
boston=datasets.load_boston() #波士顿的房价,用线性回归作预测
x=boston["data"] #506L,13L
y=boston["target"] #506l
names=boston["feature_names"]
lr=LinearRegression()
# rank all feature, continue the elimination until the last one
rfe=RFE(lr,n_features_to_select=1) #参数是指最后至少保留几个特征
rfe.fit(x,y)
print "Features sorted by their rank:"
print sorted(zip(map(lambda x: round(x,4),rfe.ranking_),names)) #从大到小排序
"""
>>>
Features sorted by their rank:
[(1.0, 'NOX'), (2.0, 'RM'), (3.0, 'CHAS'), (4.0, 'PTRATIO'), (5.0, 'DIS'),
(6.0, 'LSTAT'), (7.0, 'RAD'), (8.0, 'CRIM'), (9.0, 'INDUS'), (10.0, 'ZN'),
(11.0, 'TAX'), (12.0, 'B'), (13.0, 'AGE')]
"""
"""
-----------------------------------特征处理之嵌入型-------------------------
根据模型来分析特征的重要性(有别于包裹型,是从生产的模型权重等)
最常见的方式为用正则化方式来做特征选择,举个例子:
最早在电商用LR做CTR预估(点击率预估),在3-5亿维的系数特征上用L1(具有截断作用,没有相关性则为0,
L2具有缩放效应,theta会很小)正则化的LR模型,剩余2-3千万的feature,意味着其他的feature重要度不够
"""
from sklearn.svm import LinearSVC
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectFromModel
iris=load_iris()
x,y=iris.data,iris.target
print x.shape #(150L, 4L)
lsvc=LinearSVC(C=0.01,penalty="L1",dual=False).fit(x,y) #SVC=support vector classification
model=SelectFromModel(lsvc,prefit=True)
x_new=model.transform(x)
print x_new.shape
"""
>>> print x_new.shape
(150L, 2L)
"""
机器学习(八)使用sklearn库进行数据分析_——特征处理之过滤、包裹、嵌入型
最新推荐文章于 2023-12-27 14:38:33 发布