from sklearn.svm import LinearSVC
from sklearn.datasets import load_iris
from sklearn.feature_selection import RFE,RFECV
from sklearn.model_selection import train_test_split #数据预处理包裹式特征选取RFE模型
def test_RFE():
iris=load_iris()
X=iris.data
y=iris.target
estimator=LinearSVC()
selector=RFE(estimator=estimator,n_features_to_select=2)
selector.fit(X,y)
print("N_features %s"%selector.n_features_)
print("Support is %s"%selector.support_)
print("Ranking %s"%selector.ranking_) #调用test_RFE()
test_RFE()

#数据预处理包裹式特征选取RFECV模型
def test_RFECV():
iris=load_iris()
X=iris.data
y=iris.target
estimator=LinearSVC()
selector=RFECV(estimator=estimator,cv=3)
selector.fit(X,y)
print("N_features %s"%selector.n_features_)
print("Support is %s"%selector.support_)
print("Ranking %s"%selector.ranking_)
print("Grid Scores %s"%selector.grid_scores_) #调用test_RFECV()
test_RFECV()

def test_compare_with_no_feature_selection():
'''
比较经过特征选择和未经特征选择的数据集,对 LinearSVC 的预测性能的区别
'''
### 加载数据
iris=load_iris()
X,y=iris.data,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=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=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("Original DataSet: test score=%s"%(clf.score(X_test,y_test)))
print("Selected DataSet: test score=%s"%(clf_t.score(X_test_t,y_test_t))) #调用test_compare_with_no_feature_selection()
test_compare_with_no_feature_selection()

吴裕雄 python 机器学习——数据预处理包裹式特征选取模型的更多相关文章

  1. 吴裕雄 python 机器学习——数据预处理过滤式特征选取SelectPercentile模型

    from sklearn.feature_selection import SelectPercentile,f_classif #数据预处理过滤式特征选取SelectPercentile模型 def ...

  2. 吴裕雄 python 机器学习——数据预处理过滤式特征选取VarianceThreshold模型

    from sklearn.feature_selection import VarianceThreshold #数据预处理过滤式特征选取VarianceThreshold模型 def test_Va ...

  3. 吴裕雄 python 机器学习——数据预处理二元化OneHotEncoder模型

    from sklearn.preprocessing import OneHotEncoder #数据预处理二元化OneHotEncoder模型 def test_OneHotEncoder(): X ...

  4. 吴裕雄 python 机器学习——数据预处理二元化Binarizer模型

    from sklearn.preprocessing import Binarizer #数据预处理二元化Binarizer模型 def test_Binarizer(): X=[[1,2,3,4,5 ...

  5. 吴裕雄 python 机器学习——数据预处理字典学习模型

    from sklearn.decomposition import DictionaryLearning #数据预处理字典学习DictionaryLearning模型 def test_Diction ...

  6. 吴裕雄 python 机器学习——数据预处理嵌入式特征选择

    import numpy as np import matplotlib.pyplot as plt from sklearn.svm import LinearSVC from sklearn.li ...

  7. 吴裕雄 python 机器学习——数据预处理正则化Normalizer模型

    from sklearn.preprocessing import Normalizer #数据预处理正则化Normalizer模型 def test_Normalizer(): X=[[1,2,3, ...

  8. 吴裕雄 python 机器学习——数据预处理标准化MaxAbsScaler模型

    from sklearn.preprocessing import MaxAbsScaler #数据预处理标准化MaxAbsScaler模型 def test_MaxAbsScaler(): X=[[ ...

  9. 吴裕雄 python 机器学习——数据预处理标准化StandardScaler模型

    from sklearn.preprocessing import StandardScaler #数据预处理标准化StandardScaler模型 def test_StandardScaler() ...

随机推荐

  1. console.log对象全部展开

    挖掘Chrome Console的小秘密 SP_lyu关注 2018.09.15 18:25:32字数 1,697阅读 917 控制台应该是大多数前端开发人员日常开发调试离不开的神器.然而控制台仍有很 ...

  2. Python学习笔记9——异常处理

    处理异常 如果执行到程序中某处抛出了异常,程序就会被终止并退出.你可能会问,那有没有什么办法可以不终止程序,让其照样运行下去呢?答案当然是肯定的,这也就是我们所说的异常处理,通常使用 try 和 ex ...

  3. 【HTML】三种方法使HTML单页面输入密码才能访问

    方法一 <script type="text/javascript"> function password() { var testV = 1; var pass1 = ...

  4. Docker里面运行.net core

    详细可以参考微软官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/docker/building-net-docker ...

  5. python 请求服务器的本质

    不同路由请求不同内容 ''' 根据url中不同路径返回不同内容 ''' import socket sk=socket.socket() sk.bind(("127.0.0.1", ...

  6. 数位dp(Balanced Numbers )

    题意:一个数,如果满足奇数的数字出现偶数次,偶数的数字出现奇数次, 就是符合的数,注比如:12313  就满足,因为1 3出现了偶数次.2出现了奇数次 思路,对于这道题,就是状态压缩加dp: 对于一个 ...

  7. java判断相等

    一.字符串 1.equals():比较内容,推荐 String a=new String("abc"); String b=new String("abc"); ...

  8. linux零碎001

    glibc:GNU发布的libc库,即c运行库.glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc. uclibc:是一个面向嵌入式Linux系统的小型的C标准库.最初 ...

  9. 【vue store的使用方法】(this.$store.state this.$store.getters this.$store.dispatch this.$store.commit)

    vue 页面文件 <template> <div> {{this.$store.state.count}}<br/> {{count}}<br/> {{ ...

  10. [AST Babel] Add function name into the console log 'path.findParent(t.isFunctionDeclaration)'

    Continue with the previous post: https://www.cnblogs.com/Answer1215/p/12337243.html What we want to ...