版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21904665/article/details/52315642

ElasticNet 是一种使用L1和L2先验作为正则化矩阵的线性回归模型.这种组合用于只有很少的权重非零的稀疏模型,比如:class:Lasso, 但是又能保持:class:Ridge 的正则化属性.我们可以使用 l1_ratio 参数来调节L1和L2的凸组合(一类特殊的线性组合)。

当多个特征和另一个特征相关的时候弹性网络非常有用。Lasso 倾向于随机选择其中一个,而弹性网络更倾向于选择两个.
在实践中,Lasso 和 Ridge 之间权衡的一个优势是它允许在循环过程(Under rotate)中继承 Ridge 的稳定性.
弹性网络的目标函数是最小化:

ElasticNetCV 可以通过交叉验证来用来设置参数 alpha () 和 l1_ratio ()

  1.  
    print(__doc__)
  2.  
     
  3.  
    import numpy as np
  4.  
    import matplotlib.pyplot as plt
  5.  
     
  6.  
    from sklearn.linear_model import lasso_path, enet_path
  7.  
    from sklearn import datasets
  8.  
     
  9.  
    diabetes = datasets.load_diabetes()
  10.  
    X = diabetes.data
  11.  
    y = diabetes.target
  12.  
     
  13.  
    X /= X.std(axis=0) # Standardize data (easier to set the l1_ratio parameter)
  14.  
     
  15.  
    # Compute paths
  16.  
     
  17.  
    eps = 5e-3 # the smaller it is the longer is the path
  18.  
     
  19.  
    print("Computing regularization path using the lasso...")
  20.  
    alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)
  21.  
     
  22.  
    print("Computing regularization path using the positive lasso...")
  23.  
    alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
  24.  
    X, y, eps, positive=True, fit_intercept=False)
  25.  
    print("Computing regularization path using the elastic net...")
  26.  
    alphas_enet, coefs_enet, _ = enet_path(
  27.  
    X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)
  28.  
     
  29.  
    print("Computing regularization path using the positve elastic net...")
  30.  
    alphas_positive_enet, coefs_positive_enet, _ = enet_path(
  31.  
    X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)
  32.  
     
  33.  
    # Display results
  34.  
     
  35.  
    plt.figure(1)
  36.  
    ax = plt.gca()
  37.  
    ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
  38.  
    l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)
  39.  
    l2 = plt.plot(-np.log10(alphas_enet), coefs_enet.T, linestyle='--')
  40.  
     
  41.  
    plt.xlabel('-Log(alpha)')
  42.  
    plt.ylabel('coefficients')
  43.  
    plt.title('Lasso and Elastic-Net Paths')
  44.  
    plt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')
  45.  
    plt.axis('tight')
  46.  
     
  47.  
     
  48.  
    plt.figure(2)
  49.  
    ax = plt.gca()
  50.  
    ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
  51.  
    l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)
  52.  
    l2 = plt.plot(-np.log10(alphas_positive_lasso), coefs_positive_lasso.T,
  53.  
    linestyle='--')
  54.  
     
  55.  
    plt.xlabel('-Log(alpha)')
  56.  
    plt.ylabel('coefficients')
  57.  
    plt.title('Lasso and positive Lasso')
  58.  
    plt.legend((l1[-1], l2[-1]), ('Lasso', 'positive Lasso'), loc='lower left')
  59.  
    plt.axis('tight')
  60.  
     
  61.  
     
  62.  
    plt.figure(3)
  63.  
    ax = plt.gca()
  64.  
    ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
  65.  
    l1 = plt.plot(-np.log10(alphas_enet), coefs_enet.T)
  66.  
    l2 = plt.plot(-np.log10(alphas_positive_enet), coefs_positive_enet.T,
  67.  
    linestyle='--')
  68.  
     
  69.  
    plt.xlabel('-Log(alpha)')
  70.  
    plt.ylabel('coefficients')
  71.  
    plt.title('Elastic-Net and positive Elastic-Net')
  72.  
    plt.legend((l1[-1], l2[-1]), ('Elastic-Net', 'positive Elastic-Net'),
  73.  
    loc='lower left')
  74.  
    plt.axis('tight')
  75.  
    plt.show()

4.弹性网络( Elastic Net)的更多相关文章

  1. 【笔记】简谈L1正则项L2正则和弹性网络

    L1,L2,以及弹性网络 前情提要: 模型泛化与岭回归与LASSO 正则 ridge和lasso的后面添加的式子的格式上其实和MSE,MAE,以及欧拉距离和曼哈顿距离是非常像的 虽然应用场景不同,但是 ...

  2. 机器学习:模型泛化(L1、L2 和弹性网络)

    一.岭回归和 LASSO 回归的推导过程 1)岭回归和LASSO回归都是解决模型训练过程中的过拟合问题 具体操作:在原始的损失函数后添加正则项,来尽量的减小模型学习到的 θ 的大小,使得模型的泛化能力 ...

  3. 机器学习算法--Elastic Net

    1) alpha : float, optional Constant that multiplies the penalty terms. Defaults to 1.0. See the note ...

  4. [源码解析] 深度学习分布式训练框架 horovod (20) --- Elastic Training Operator

    [源码解析] 深度学习分布式训练框架 horovod (20) --- Elastic Training Operator 目录 [源码解析] 深度学习分布式训练框架 horovod (20) --- ...

  5. 基于C#的机器学习--颜色混合-自组织映射和弹性神经网络

    自组织映射和弹性神经网络 自组织映射(SOM),或者你们可能听说过的Kohonen映射,是自组织神经网络的基本类型之一.自组织的能力提供了对以前不可见的输入数据的适应性.它被理论化为最自然的学习方式之 ...

  6. 阿里云弹性裸金属服务器-神龙架构(X-Dragon)揭秘

    在5月16日的飞天技术会新品直播中,特别邀请了业界知名大咖狒哥以及阿里云虚拟化资深专家旭卿作为现场直播的嘉宾.本次直播主要从产品背景到“X-Dragon架构”,从硬件设备到软件应用来深度的剖析“X-D ...

  7. [Machine Learning] 机器学习常见算法分类汇总

    声明:本篇博文根据http://www.ctocio.com/hotnews/15919.html整理,原作者张萌,尊重原创. 机器学习无疑是当前数据分析领域的一个热点内容.很多人在平时的工作中都或多 ...

  8. Spark入门实战系列--8.Spark MLlib(上)--机器学习及SparkMLlib简介

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .机器学习概念 1.1 机器学习的定义 在维基百科上对机器学习提出以下几种定义: l“机器学 ...

  9. 转:netflix推荐系统竞赛

    原文链接:Netflix recommendations: beyond the 5 stars (Part 1), (Part 2) 原文作者:Xavier Amatriain and Justin ...

随机推荐

  1. c# 字段成员

  2. lvs+keepalived集群架构服务

    一,LVS功能详解 1.1 LVS(Linux Virtual Server)介绍 LVS是Linux Virtual Server 的简写(也叫做IPVS),意即Linux虚拟服务器,是一个虚拟的服 ...

  3. js、jquery实现列表模糊搜索功能

    实现的搜索功能: 1. 可以匹配输入的字符串找出列表中匹配的项,列表框的高度跟随搜索出的列表项的多少改变 2. 可以点击某一项进行选中列表项 3. 可以按下上.下.回车键来控制列表项 4. 按下回车键 ...

  4. G1混合式GC与三色标记算法详解【纯理论】

    继续基于上一次https://www.cnblogs.com/webor2006/p/11146273.html的理论进一步了解G1. G1收集概览: G1算法将堆划分为若干个区域(Region),它 ...

  5. git daemon 安装和使用

    git daemon 安装和使用 系统:Cent OS 8 安装 git 和 git daemon(不同系统有不同的安装命令) yum install -y git yum install -y gi ...

  6. 【NOI2019集训题2】 序列 后缀树+splay+dfs序

    题目大意:给你一个长度为$n$的序列$a_i$,还有一个数字$m$,有$q$次询问 每次给出一个$d$和$k$,问你对所有的$a_i$都在模$m$意义下加了$d$后,第$k$小的后缀的起点编号. 数据 ...

  7. JZOJ 5870 地图

    直接解释题解,记录一下.

  8. java文件上传下载组件

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

  9. 百度UEditor编辑器从word粘贴公式

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...

  10. P2679 子串 DP

    P2679 子串 DP 从字符串A中取出\(k\)段子串,按原顺序拼接,问存在多少个方案使拼接的字符串与字符串B相同 淦,又是这种字符串dp 设状态\(ans[i][j][k]\)表示A串位置\(i\ ...