inplace_row_scale#

sklearn.utils.sparsefuncs.inplace_row_scale(X, scale)[源代码]#

CSR或CSC矩阵的就地行扩展。

通过乘以调用者提供的特定比例来缩放数据矩阵的每一行,假设形状为(n_samples,n_features)。

参数:
X形状稀疏矩阵(n_samples,n_features)

待缩放的矩阵。它应该是CSR或CSC格式。

scale形状的nd数组(n_features,),dype ={np.float32,np.float64}

用于缩放的预先计算的样本值数组。

示例

>>> from sklearn.utils import sparsefuncs
>>> from scipy import sparse
>>> import numpy as np
>>> indptr = np.array([0, 2, 3, 4, 5])
>>> indices = np.array([0, 1, 2, 3, 3])
>>> data = np.array([8, 1, 2, 5, 6])
>>> scale = np.array([2, 3, 4, 5])
>>> csr = sparse.csr_matrix((data, indices, indptr))
>>> csr.todense()
matrix([[8, 1, 0, 0],
        [0, 0, 2, 0],
        [0, 0, 0, 5],
        [0, 0, 0, 6]])
>>> sparsefuncs.inplace_row_scale(csr, scale)
>>> csr.todense()
 matrix([[16,  2,  0,  0],
         [ 0,  0,  6,  0],
         [ 0,  0,  0, 20],
         [ 0,  0,  0, 30]])