Эффективное вычисление cosine_distance csc_sparse_matrix с использованием scipy.spatial.distance - PullRequest
0 голосов
/ 18 февраля 2020

enter image description here Иметь csc_matrix разреженный файл с именем eventPropMatrix , имеющий тип данных = float64 с форма = (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-й Gen, 4-ядерный процессор, 12 ГБ ОЗУ, Ubuntu). Есть ли какой-нибудь способ для этого огромного расчета. Я собираюсь Cythonize код в C, а затем скомпилировать и запустить. Поможет ли это, если да, то как это сделать ???

...