吴裕雄 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 ...
随机推荐
- opencv-python常用接口
最直接的是参考官网:https://docs.opencv.org/4.2.0/d6/d00/tutorial_py_root.html
- IE浏览器复选框遍历不兼容问题
obj = document.getElementsByName("userIdCheckbox"); ids = []; for(var k=0;k<obj.length; ...
- 并查集-G - 食物链
G - 食物链 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种 ...
- Pyarm的Pyqt的配置
相关连接: Python PyQt 安装python3.4 x64到c盘根目录. 安装PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x64.exe 安装pycharm-professio ...
- IntelliJ IDEA 2017.3尚硅谷-----模板
https://www.jetbrains.com/help/idea/using-live-templates.html
- mybatis(六):设计模式 - 建造者模式
- P1582 倒水(贪心 + lowbbit)
https://www.luogu.com.cn/problem/P1582 #include <bits/stdc++.h> using namespace std; #define i ...
- mysql分组,然后组内排序取最新的一条
参照: https://blog.csdn.net/qq_16504067/article/details/78589232 https://www.cnblogs.com/w1441639547/p ...
- Python的深拷贝、浅拷贝
浅拷贝 定义:浅拷贝只是对另外一个变量的内存地址的拷贝,这两个变量指向同一个内存地址的变量值. 浅拷贝的特点: 公用一个值: 这两个变量的内存地址一样: 对其中一个变量的值改变,另外一个变量的值也会改 ...
- codeforces 711 D.Directed Roads(tarjan 强连通分量 )
题目链接:http://codeforces.com/contest/711/problem/D 题目大意:Udayland有一些小镇,小镇和小镇之间连接着路,在某些区域内,如果从小镇Ai开始,找到一 ...