sklearn 源码解析 基本线性模型 岭回归 ridge.py(1)

对于前面已经提到的类及一些细节不再给出。对于稀疏矩阵的了解是必要的。from abc import ABCMeta, abstractmethod import warnings import numpy as np from scipy import linalg from scipy import sparse from scipy.sparse import
摘要由CSDN通过智能技术生成
对于前面已经提到的类及一些细节不再给出。对于稀疏矩阵的了解是必要的。
from abc import ABCMeta, abstractmethod
import warnings

import numpy as np
from scipy import linalg
from scipy import sparse
from scipy.sparse import linalg as sp_linalg

from .base import LinearClassifierMixin, LinearModel, _rescale_data
from .sag import sag_solver :是一种随机平均梯度下降式的求解岭回归与logistic回归的包,
  使用梯度下降法,
   这种算法收敛很快。
from ..base import RegressorMixin
from ..utils.extmath import safe_aparse_dot
from ..utils.extmath import row_norms: 行范数,不支持稀疏矩阵。
from ..utils import check_X_y
from ..utils import check_array :转换为ndarray类型。
from ..utils import check_consistent_length: 检查一个ndarray的list 是否所有元素第一个
   维度都相等。(i.e. same length)
from ..utils import column_or_1d: 特殊的拉直函数,接受类似feature形式的ndarray,
   即在第二个维度上只有1维,并将其按列拉直为以为数组。
from ..preprocessing import LabelBinarizer:
   对一对多问题将标签进行二值化。
from ..model_selection import GridSearchCV
from ..externsls import six
from ..metrics.scorer import check_scoring: 对能够进行返回score估计的模型,返回进行
   score计算的函数。


下面先不对具体的类,而是接口进行说明。(无组织架构)
sp_linalg.aslinearoperator: 将对象(ndarray, sparse, matrix an so on)转换为线性算子。
np.empty(shape): 返回相应shape的未初始化的ndarray.
sp_linalg.cg: 使用共轭梯度(Conjugate Gradient)法解线性系统。
sq_linalg.lsqr: lsqr :QR分解。
ndarray.flat:(。。。)
np.atleast_1d: 标量化为一维数组,高维保持。

def _solve_sparse_cg(X, y, alpha, max_iter = None, tol = 1e-3, verbose = 0):
 n_samples, n_features = X.shape
 X1 = sp_linalg.aslinearoperator(X)
 coefs = np.empty((y.shape[1], n_features))

 if n_features > n_samples:
  def create_mv(curr_alpha):
   def _mv(x):
    return X1.matvec(X1.rmatvec(x)) + curr_alpha * x
   return _mv 
 else:
  def create_mv(curr_alpha):
   def _mv(curr_alpha):
    return X1.rmatvec(X1.matvec(x)) + curr_alpha * x 
   return _mv 

 for i in range(y.shape[1]):
  y_column = y[:, i]

  mv = create_mv(alpha[i])
  if n_features > n_samples:
   C = sp_linalg.LinearOperator((n_samples, n_samples), matvec = mv, dtype = x.dtype)
   coef, info = sp_linalg.cg(C, y_column, tol = tol)
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值