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. 在多租户(容器)数据库中如何创建PDB:方法5 DBCA远程克隆PDB

    基于版本:19c (12.2.0.3) AskScuti 创建方法:DBCA静默远程克隆PDB.将 CDB1 中的 PDB1 克隆为 CDB2 中的 ERP2 对应路径:Creating a PDB ...

  2. 使用NSIS制作可执行程序的安装包

    使用NSIS制作可执行程序的安装包: 1,NSIS下载地址:https://pan.baidu.com/s/1GzzQNXgAlJPJWgjBzVwceA 下载完成之后解压缩,打开安装程序,默认安装即 ...

  3. js -- 车牌号对应的归属地js文件

    /*车牌号对应的归属地*/ let cardCallerloc = new Map(); // 北京市(京) cardCallerloc.set("京A", "北京市&q ...

  4. scw——03错误,swagger开启错误

    错误: 代码: @Value("${swagger2.enable:false}") private boolean enable = false; 原因:没有开启swagger的 ...

  5. usage: git remote add [<options>] <name> <url> -f, --fetch fetch the remote branches --tags import all tags and associated objects when fetching

    按照git官网提示输入 git pushgit remote add origin git@github.com:***3 / elm-1.git -u 链接git远程仓库 出现错误 usage: g ...

  6. while、for循环结构_python

    一.while循环的基础例子: 例子1:判断是否大于50 例子2:按需打印乘法口诀 例子3:无限循环 while True: print (“true”) 二.for循环 1.for循环的常见范围的用 ...

  7. 题解【SP8002】HORRIBLE - Horrible Queries

    题面 题解 这是一道线段树的模板题. 题目需要我们维护一个支持区间修改.区间查询的一个数据结构,很容易想到线段树. 然后发现和洛谷上线段树的模板1是同一道题. 由于本题中每个数的初始值都为\(0\), ...

  8. 题解【UVA12003】Array Transformer

    题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例#1 10 1 11 1 2 3 4 5 6 7 8 9 10 2 8 6 10 输出样例#1 1 2 3 4 5 6 7 8 9 6 ...

  9. Pacemaker+ISCSI实现Apache高可用-配置

    一.配置文件系统 任意节点用ISCSI的共享磁盘创建LVM node1 pvcreate /dev/sdb vgcreate my_vg /dev/sdb lvcreate -L 1G -n web_ ...

  10. easyui的combogrid

    easyui的combogri下拉框用在项目中很多,有时会出现很多问题,当然也好解决. 1.当向后台传id值时,用户输入的与查询出来的显示值一样,但combogrid为空? 情景:输入‘李四’,和显示 ...