0

在此处输入图像描述有一个csc_matrix稀疏命名为eventPropMatrix,其datatype=float64shape=(13000,7)。我正在应用以下距离计算功能。这里

eventPropMatrix.getrow(i).todense()==[[0. 0. 0. 0. 0. 0. 0.]]

eventPropMatrix.getrow(j).todense()==[[0. 0. 0. 0. 0. 0. 0.]]

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=RuntimeWarning)
    epsim = scipy.spatial.distance.correlation(eventPropMatrix.getrow(i).todense(), eventPropMatrix.getrow(j).todense())

这里 scipy.spatial.distance.correlation 如下:

def correlation(u, v, w=None, centered=True):
    """
    Compute the correlation distance between two 1-D arrays.

    The correlation distance between `u` and `v`, is
    defined as

    .. math::

        1 - \\frac{(u - \\bar{u}) \\cdot (v - \\bar{v})}
                  {{||(u - \\bar{u})||}_2 {||(v - \\bar{v})||}_2}

    where :math:`\\bar{u}` is the mean of the elements of `u`
    and :math:`x \\cdot y` is the dot product of :math:`x` and :math:`y`.

    Parameters
    ----------
    u : (N,) array_like
        Input array.
    v : (N,) array_like
        Input array.
    w : (N,) array_like, optional
        The weights for each value in `u` and `v`. Default is None,
        which gives each value a weight of 1.0

    Returns
    -------
    correlation : double
        The correlation distance between 1-D array `u` and `v`.

    """
    u = _validate_vector(u)
    v = _validate_vector(v)
    if w is not None:
        w = _validate_weights(w)
    if centered:
        umu = np.average(u, weights=w)
        vmu = np.average(v, weights=w)
        u = u - umu
        v = v - vmu
    uv = np.average(u * v, weights=w)
    uu = np.average(np.square(u), weights=w)
    vv = np.average(np.square(v), weights=w)
    dist = 1.0 - uv / np.sqrt(uu * vv)
    return dist

在这里,我大部分时间都将“nan”值作为返回值,如 uu=0.0 和 vv=0.0

我的查询是,对于 13000 行,此计算需要太多时间。它已经运行了 15 多个小时(i5、第 8 代、4 核处理器、12Gb RAM、Ubuntu)。这种巨大的计算有什么办法吗?我正在考虑将代码Cythonize转换为 C,然后编译并运行。这会有帮助吗,如果有那么怎么做???

4

0 回答 0