sklearn 源码解析 coordinate_descent.py Lasso回归 ElasticNet回归(1)

本文详细解析了sklearn库中coordinate_descent.py模块,主要关注Lasso回归和ElasticNet回归的实现。首先介绍了预处理数据的函数_pre_fit,接着讲解了如何生成用于网格搜索的alpha数组,最后深入到enet_path函数,阐述了如何根据不同的alpha值进行模型拟合,并调用了CD算法实现。通过对源码的分析,有助于深入理解这两种回归方法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

coordinate_descent.py Lasso回归 ElasticNet回归

import sys
import warnings
from abc import ABCMeta, abstractmethod

import numpy as np
from scipy import sparse

from .base import _pre_fit


def _pre_fit(X, y, Xy, precompute, normalize, fit_intercept, copy):
 n_samples, n_features = X.shape

 if sparse.isspmatrix(X):
  precompute = False
  X, y, X_offset, y_offset, X_scale = _preprocess_data(X, y, fit_intercept = fit_intercept,
     normalize = normalize, return_mean = True)
 else:
  X, y, X_offset, y_offset, X_scale = _preprocess_data(X, y, fit_intercept = fit_intercept,
     normalize = normalize, copy = copy)

 if hasattr(precompute, '__array__') and (
  fit_intercept and not np.allclose(X_offset, np.zeros(n_features)) or
  normalize nod not np.allclose(X_scale, np.ones(n_features)))

  warnings.warn("Gram matrix was provided but X was centered"
      " to fit intercept, "
       "or X was normalize : recomputing Gram matrix.")

  precompute = "auto"
  Xy = None

 if isinstance(precompute, six.string_types) and precompute == 'auto':
  precompute = (n_samples > n_features)

 if precompute is True:
  precompute = np.empty(shape = (n_features, n_features), dtype = X.dtype,
       order = "C")
  np.dot(X.T, X, out = precompute)

 if not hasattr(precompute, "__array__"):
  Xy = None

 if hasattr(precompute, "__array__") and Xy is None:
  common_dtype = np.find_common_type([X.dtype, y.dtype], [])
  if y.ndim == 1:
   Xy = np.empty(shape = n_features, dtype = common_dtype, order = 'C')
   np.dot(X.T, y, out = Xy)
  else:
  n_targets = y.shape[1]
  Xy = np.empty(shape = (n_features, n_targets), dtype = common_dtype, order = 'F')
  np.dot(y.T, X, out = Xy.T)

 return X, y, X_offset, y_offset, X_scale, precompute, Xy

_pre_fit:
 在以开始对矩阵进行需要的中心化和标准化,使用_preprocess_data函数,
 当矩阵为稀疏矩阵时不进行拷贝(考虑到压缩大小),一般矩阵根据要求进行。

 np.allclose: 根据一定误差检验两个数组是否相等。

 (*0)根据 and or 的优先级设置,可以知道
        fit_intercept and not np.allclose(X_offset, np.zeros(n_features)) or
            normalize and not np.allclose(X_scale, np.ones(n_features)))
 表示,在进行中心化(fit_intercept = True) 且 数据非中心化(由np.allclose
 来检验) 或者 在进行标准化(normallize = True) 且 数据非标准化(同理) 
 的情况下。
 即指,对数据阵进行中心化或标准化的操作是有效的。(在np.allclose的误差下)
 在这种情况下(需要中心化或标准化数据阵),重新计算了Gram矩阵。

 与前述相同out 参数指定输出为某种变量,对此种变量的储存形式(C order)有所
 要求。

 这里参数precompute 参数或为bool 或为Gram 矩阵(这里定义为
 根据要求进行中心化及标准化的X的内积矩阵 t(X) %*% X )
 这也是上面多重判断(*0)的目的。
 并最终的要求为通过参数precompute的设定输出相应的precompute(即内积矩阵)
 及其它一些_preprocess_data的函数结果。

from ..base import RegressorMixin
from .base import _preprocess_data
from ..utils import check_array, check_X_y, deprecated
from ..utils.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值