如何绘制【逻辑回归】中threshold参数的学习曲线

threshold参数的意义是通过筛选掉低于threshold的参数,来对逻辑回归的特征进行降维。

首先导入相应的模块:

python 复制代码
from sklearn.linear_model import LogisticRegression as LR
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import numpy as np
from sklearn.feature_selection import SelectFromModel # 从模型中选择特征
from sklearn.model_selection import cross_val_score # 交叉验证

导入乳腺癌数据集:

python 复制代码
data = load_breast_cancer()
x = data.data
y = data.target

查看数据集特征矩阵的情况:

python 复制代码
data.data.shape # (569, 30)

这个时候有30个特征。实例化一个逻辑回归模型,并使用交叉验证评估模型性能:

python 复制代码
LR_ = LR(solver="liblinear", C=0.8, random_state=420)
cross_val_score(LR_, x, y, cv=10).mean() # 0.9508145363408522

使用select_from_model函数根据模型的权重系数或特征重要性等信息,选择重要的特征,并将选择后的特征矩阵返回给x_embedded:

python 复制代码
X_embedded = SelectFromModel(LR_, threshold = 0.8, norm_order=1).fit_transform(x, y) # norm_order=1表示L1正则,模型会删除L1正则化后系数为0的特征,threshold表示阈值,当特征的系数小于阈值时,删除该特征
X_embedded.shape # (569, 9)

可以发现现在特征只剩下9个了。在这里我们设置了threshold = 0.8,也就是说小于0.8的权重系数被删除掉了。但是我们怎么知道设置哪个threshold值后得到的特征矩阵去训练模型,会得到最优的模型效果呢?

接下来我们开始绘制threshold的学习曲线,也就是不同的threshold值对模型效果的影响。在绘制之前,我们先训练模型,看一下权重系数的最大值,找到threshold的取值范围:

python 复制代码
# 画threshod的学习曲线
LR_.fit(x, y) # 训练模型
LR_.coef_ # 查看训练后各变量的系数
LR_.coef_.shape # (1, 30)
LR_.coef_.max() # 1.9376881066687164

为了对比特征选择前和选择后模型的效果,我们设置了一组对照,同时确定了threshold的取值范围:

python 复制代码
fullx = [] # 创建特征选择前的交叉验证的空列表
fsx = [] # 创建特征选择后的交叉验证的空列表
threshold = np.linspace(0, abs(LR_.fit(x, y).coef_).max(), 20) # 从0到最大系数之间取20个数

接下来绘制函数图像:

python 复制代码
k = 0
for i in threshold:
    x_embedded = SelectFromModel(LR_, threshold=i).fit_transform(x, y) # threshold表示阈值,当特征的系数小于阈值时,删除该特征。此行代码是形成新的特征矩阵
    fullx.append(cross_val_score(LR_, x, y, cv=5).mean()) # 特征选择前进行交叉验证
    fsx.append(cross_val_score(LR_, x_embedded, y, cv=5).mean()) # 特征选择后进行交叉验证
    print((threshold[k], x_embedded.shape[1])) # 打印每次循环取到的阈值和降维后的特征数
    k += 1
plt.figure(figsize=(20, 5))
plt.plot(threshold, fullx, label="full")
plt.plot(threshold, fsx, label="feature selection")
plt.xticks(threshold)
plt.legend()
plt.show()

结果如下:

由图可知,随着threshold的值逐渐变大,被删除的特征越多,模型效果越差。这不是我们想要的结果,因此我们将范围缩小,将threshold的取值范围缩小(0,0.1),再来跑一下模型:

python 复制代码
fullx = [] # 创建特征选择前的交叉验证的空列表
fsx = [] # 创建特征选择后的交叉验证的空列表
threshold = np.linspace(0, 0.1, 20) # 从0到最大系数之间取20个数
k = 0
for i in threshold:
    x_embedded = SelectFromModel(LR_, threshold=i).fit_transform(x, y) # threshold表示阈值,当特征的系数小于阈值时,删除该特征。此行代码是形成新的特征矩阵
    fullx.append(cross_val_score(LR_, x, y, cv=5).mean()) # 特征选择前进行交叉验证
    fsx.append(cross_val_score(LR_, x_embedded, y, cv=5).mean()) # 特征选择后进行交叉验证
    print((threshold[k], x_embedded.shape[1])) # 打印每次循环取到的阈值和降维后的特征数
    k += 1
plt.figure(figsize=(20, 5))
plt.plot(threshold, fullx, label="full")
plt.plot(threshold, fsx, label="feature selection")
plt.xticks(threshold)
plt.legend()
plt.show()

结果如下:

可以发现,当threshold取0.0053时,模型可以获得最好的效果。

相关推荐
Eric.Lee202115 分钟前
数据集-目标检测系列- 螃蟹 检测数据集 crab >> DataBall
python·深度学习·算法·目标检测·计算机视觉·数据集·螃蟹检测
林辞忧25 分钟前
算法修炼之路之滑动窗口
算法
￴ㅤ￴￴ㅤ9527超级帅35 分钟前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
liuyang-neu36 分钟前
力扣 简单 110.平衡二叉树
java·算法·leetcode·深度优先
penguin_bark43 分钟前
LCR 068. 搜索插入位置
算法·leetcode·职场和发展
牛哥带你学代码1 小时前
交叠型双重差分法
人工智能·深度学习·机器学习
学步_技术1 小时前
自动驾驶系列—线控系统:驱动自动驾驶的核心技术解读与应用指南
人工智能·机器学习·自动驾驶·线控系统·转向系统
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
ROBIN__dyc1 小时前
表达式
算法
无限大.1 小时前
c语言实例
c语言·数据结构·算法