对于前面已经提到的类及一些细节不再给出。对于稀疏矩阵的了解是必要的。
from abc import ABCMeta, abstractmethod
import warnings
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 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计算的函数。
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)