sklearn-基础使用

数据清洗可以用pandas,数据predict的时候就要用到大名鼎鼎的sklearn了,里面包含了很多基础的算法,可以帮助Data Scientist 解决很多问题。
(a)data normalization

from sklearn import preprocessing
# normalize the data attributes
normalized_X = preprocessing.normalize(X) #range from 0 to 1
# standardize the data attributes
standardized_X = preprocessing.scale(X)  #均值为0 方差为1

(b)feature extraction

from sklearn import metrics
from sklearn.ensemble import ExtraTreesClassifier
model = ExtraTreesClassifier()  #基于决策树接近树根
model.fit(X, y)
# display the relative importance of each attribute
print(model.feature_importances_)

rom sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
# create the RFE model and select 3 attributes
rfe = RFE(model, 3)            #暴力破解呀,选出所有大小为3的子集算出误差最小的
rfe = rfe.fit(X, y)
# summarize the selection of the attributes
print(rfe.support_)
print(rfe.ranking_)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
feature_selection模块

Univariate feature selection:单变量的特征选择
单变量特征选择的原理是分别单独的计算每个变量的某个统计指标,根据该指标来判断哪些指标重要。剔除那些不重要的指标。

sklearn.feature_selection模块中主要有以下几个方法:
SelectKBest和SelectPercentile比较相似,前者选择排名排在前n个的变量,后者选择排名排在前n%的变量。而他们通过什么指标来给变量排名呢?这需要二外的指定。
对于regression问题,可以使用f_regression指标。对于classification问题,可以使用chi2或者f_classif变量。
使用的例子:
from sklearn.feature_selection import SelectPercentile, f_classif
selector = SelectPercentile(f_classif, percentile=10)

还有其他的几个方法,似乎是使用其他的统计指标来选择变量:using common univariate statistical tests for each feature: false positive rate SelectFpr, false discovery rate SelectFdr, or family wise error SelectFwe.

文档中说,如果是使用稀疏矩阵,只有chi2指标可用,其他的都必须转变成dense matrix。但是我实际使用中发现f_classif也是可以使用稀疏矩阵的。

Recursive featu
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值