吴裕雄 python 机器学习——数据预处理嵌入式特征选择
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 机器学习——数据预处理嵌入式特征选择的更多相关文章
- 吴裕雄 python 机器学习——数据预处理包裹式特征选取模型
from sklearn.svm import LinearSVC from sklearn.datasets import load_iris from sklearn.feature_select ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取SelectPercentile模型
from sklearn.feature_selection import SelectPercentile,f_classif #数据预处理过滤式特征选取SelectPercentile模型 def ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取VarianceThreshold模型
from sklearn.feature_selection import VarianceThreshold #数据预处理过滤式特征选取VarianceThreshold模型 def test_Va ...
- 吴裕雄 python 机器学习——数据预处理正则化Normalizer模型
from sklearn.preprocessing import Normalizer #数据预处理正则化Normalizer模型 def test_Normalizer(): X=[[1,2,3, ...
- 吴裕雄 python 机器学习——数据预处理标准化MaxAbsScaler模型
from sklearn.preprocessing import MaxAbsScaler #数据预处理标准化MaxAbsScaler模型 def test_MaxAbsScaler(): X=[[ ...
- 吴裕雄 python 机器学习——数据预处理标准化StandardScaler模型
from sklearn.preprocessing import StandardScaler #数据预处理标准化StandardScaler模型 def test_StandardScaler() ...
- 吴裕雄 python 机器学习——数据预处理标准化MinMaxScaler模型
from sklearn.preprocessing import MinMaxScaler #数据预处理标准化MinMaxScaler模型 def test_MinMaxScaler(): X=[[ ...
- 吴裕雄 python 机器学习——数据预处理二元化OneHotEncoder模型
from sklearn.preprocessing import OneHotEncoder #数据预处理二元化OneHotEncoder模型 def test_OneHotEncoder(): X ...
- 吴裕雄 python 机器学习——数据预处理二元化Binarizer模型
from sklearn.preprocessing import Binarizer #数据预处理二元化Binarizer模型 def test_Binarizer(): X=[[1,2,3,4,5 ...
随机推荐
- C++-随机数的产生
一.随机数 以前学C语言的时候感觉随机数没啥用的,现在想想是自己无知啦,在帮人做一个项目的时候发现随机数还是相当有用的,我们可以利用随机数来生成大量的测试数据. 有两种方法可以让你的程序每次运行结果不 ...
- C#设置一个控件可以鼠标拖动
C#设置一个控件可以鼠标拖动: 新建一个C#项目, 创建一个label控件, 设置label的鼠标按下和抬起事件分别为:label1_MouseDown和label1_MouseUp. 对代码进行如下 ...
- jmeter 登陆--查询存在否-->新建客户-->查询存在否 + 压测
1.登陆 正则表达式提取器和json提取器,都是后置处理器提取token(都可以在响应中以regexp tester 和 json path tester查看提取的对不对) beanshell 后置处 ...
- Spring Cloud Netflix Eureka【服务治理】
一.简介 二.使用 一.源码分析
- E. Vus the Cossack and a Field (求一有规律矩形区域值) (有一结论待证)
E. Vus the Cossack and a Field (求一有规律矩形区域值) 题意:给出一个原01矩阵,它按照以下规则拓展:向右和下拓展一个相同大小的 0 1 分别和原矩阵对应位置相反的矩阵 ...
- 每天进步一点点------Allegro 铺铜详解
铺铜在设计PCB板时很重要,为了加深理解,笔者写下这篇学习的过程. 首先要理解什么是正片和负片,结合网上的资料来理解一下: 正片实际就是能在底片上能看到的就是存在的 负片实际上就是在底片看到的就是不存 ...
- robots.txt 让搜索引擎不再收录网站
有没有担心过自己的隐私会在强大的搜索引擎面前无所遁形?想象一下,如果要向世界上所有的人公开你的私人日记,你能接受吗?的确是很矛盾的问题,站长们大都忧虑“如何让搜索引擎收录的我的网站?”,而我们还是要研 ...
- alibaba-java-style-guide
(一) 命名规约 1.[强制]代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ / name$ / O ...
- MP4转mp3
python实现: 依赖: glob,pydub "Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not wo ...
- 错误记录(一):VSCode
VS Code莫名其妙突然变卡. 后来重新安装,下载以前版本,设置防止循环,都不太管用. 最后想添加VS Code目录到windows扫描白名单,但因为系统之前是英文不太好看懂,所以又调回了中文. 这 ...