以对数方式缩放(应用函数?)稀疏矩阵

Scale (apply function?) sparse matrix logarithmically

我正在使用 scikit-learn preprocessing scaling 作为 稀疏 矩阵。

我的目标是 "scale" 每个特征列通过取对数为底的列最大值。我的措辞可能不准确。我试着解释一下。

假设特征列有值:0, 8, 2:

是的,我可以使用 FunctionTransformer, but I want the base of the log change (based on) each column (in particular, the maximum value). That is, I want to do something like the MaxAbsScaler 轻松应用任何基于函数的转换器,只需取对数即可。

我看到 MaxAbsScaler 首先得到每列最大值的向量 (scale) (code) and then multiples the original matrix times 1 / scale in code.

但是,如果我想基于scale向量取对数,我不知道该怎么做。甚至可以将对数运算转换为乘法 (?) 还是我有其他可能的 scipy 稀疏运算是有效的?

我希望我的意图是明确的(并且是可能的)。

x 以 b 为底的对数与 log(x)/log(b) 相同,其中对数是自然的。因此,您描述的过程相当于首先对所有内容应用 log(x+1) 转换,然后按最大绝对值进行缩放。方便地,log(x+1) 是一个 built-in 函数,log1p。示例:

from sklearn.preprocessing import FunctionTransformer, maxabs_scale
from scipy.sparse import csc_matrix
import numpy as np
logtran = FunctionTransformer(np.log1p, accept_sparse=True)
X = csc_matrix([[ 1., 0, 8], [ 2., 0,  0], [ 0,  1., 2]])
Y = maxabs_scale(logtran.transform(X))

输出(稀疏矩阵 Y):

  (0, 0)        0.630929753571
  (1, 0)        1.0
  (2, 1)        1.0
  (0, 2)        1.0
  (2, 2)        0.5