coordinate_descent.py Lasso回归 ElasticNet回归
import sys
import warnings
from abc import ABCMeta, abstractmethod
import warnings
from abc import ABCMeta, abstractmethod
import numpy as np
from scipy import sparse
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)
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)))
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.")
" to fit intercept, "
"or X was normalize : recomputing Gram matrix.")
precompute = "auto"
Xy = None
Xy = None
if isinstance(precompute, six.string_types) and precompute == 'auto':
precompute = (n_samples > n_features)
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)
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
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)
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函数,
当矩阵为稀疏矩阵时不进行拷贝(考虑到压缩大小),一般矩阵根据要求进行。
在以开始对矩阵进行需要的中心化和标准化,使用_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矩阵。
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的函数结果。
根据要求进行中心化及标准化的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.
from .base import _preprocess_data
from ..utils import check_array, check_X_y, deprecated
from ..utils.