import numpy as np
import matplotlib.pyplot as plt from sklearn.svm import LinearSVC
from sklearn.linear_model import Lasso
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn.datasets import load_digits,load_diabetes #数据预处理嵌入式特征选择SelectFromModel模型
def test_SelectFromModel():
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)
print("Threshold %s"%selector.threshold_)
print("Support is %s"%selector.get_support(indices=True)) #调用test_SelectFromModel()
test_SelectFromModel()

def load_diabetes():
digits=load_digits()
X=digits.data
y=digits.target
return X,y def test_Lasso(*data):
'''
测试 alpha 与稀疏性的关系
'''
X,y=data
alphas=np.logspace(-2,2)
zeros=[]
for alpha in alphas:
regr=Lasso(alpha=alpha)
regr.fit(X,y)
### 计算零的个数 ###
num=0
for ele in regr.coef_:
if abs(ele) < 1e-5:num+=1
zeros.append(num)
##### 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(alphas,zeros)
ax.set_xlabel(r"$\alpha$")
ax.set_xscale("log")
ax.set_ylim(0,X.shape[1]+1)
ax.set_ylabel("zeros in coef")
ax.set_title("Sparsity In Lasso")
plt.show() X,y = load_diabetes()
test_Lasso(X,y)

def test_LinearSVC(*data):
'''
测试 C 与 稀疏性的关系
'''
X,y=data
Cs=np.logspace(-2,2)
zeros=[]
for C in Cs:
clf=LinearSVC(C=C,penalty='l1',dual=False)
clf.fit(X,y)
### 计算零的个数 ###
num=0
for row in clf.coef_:
for ele in row:
if abs(ele) < 1e-5:num+=1
zeros.append(num)
##### 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(Cs,zeros)
ax.set_xlabel("C")
ax.set_xscale("log")
ax.set_ylabel("zeros in coef")
ax.set_title("Sparsity In SVM")
plt.show() X,y = load_diabetes()
test_LinearSVC(X,y)

吴裕雄 python 机器学习——数据预处理嵌入式特征选择的更多相关文章

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

    from sklearn.svm import LinearSVC from sklearn.datasets import load_iris from sklearn.feature_select ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 使用yaml格式进行接口测试报错

    前言:本人公司使用yaml做接口测试.某日开发写了一个字典嵌套列表,列表里面再嵌套字典的接口. yaml的值应该为下图(注意缩进问题)   加了-代表下面是一个列表 {'uid': '3a61479f ...

  2. Shashlik Cooking

    Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simu ...

  3. LED Decorative Light Supplier - LED Neon Application: 5 Advantages

    In the past 100 years, lighting has gone a long way. LED decorative lighting is now designed to meet ...

  4. Centos7添加软链接

    1.pycharm添加软连接: 命令行模式中输入命令: ln -s /root/pycharm-2018.1/bin/pycharm.sh /usr/bin/pycharm ps:代码中/root/p ...

  5. TNS-12545: Connect failed because target host or object does not exist

    问题描述 $ lsnrctl startLSNRCTL for Linux: Version 12.1.0.2.0 - Production on 26-JUL-2017 09:53:42Copyri ...

  6. 一则关于控制文件全部丢失后如何重新编目RMAN元数据的简单实验

    主题:一则简单的RMAN元数据编目实验,来自于博客园AskScuti. 场景:RMAN备份完整情况下,未使用Catalog目录库.删除了所有的控制文件,在手工重建后,导致记录在控制文件中的RMAN备份 ...

  7. 接口自动化框架(Pytest,Allure,Yaml)

    框架链接:https://www.jianshu.com/p/e31c54bf15ee 目前是基于他的框架做了些改动(主要是session.action()和json格式传参). 后续优化,应该主要思 ...

  8. php中文乱码问题的终极解决方案汇总

    乱码是我们在开发可能经常遇见,也是最让人头疼的一个问题了,下面这篇文章主要介绍了在php开发中,可能遇见中文乱码问题的终极解决方案,文中介绍好几个情况下的解决方法,需要的朋友可以参考借鉴,下面来一起看 ...

  9. 转载:EQ--biquad filter

    http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt https://arachnoid.com/BiQuadDesigner/index.html ...

  10. Vue-移动端开发全家桶

    内容:node.js,vue-cli,vuex,axios,postcss-pxtorem,lib-flexible,vant ,babel-plugin-import 1.安装脚手架工具: npm ...