1
\$\begingroup\$

I reimplemented the functions https://rdrr.io/cran/fclust/man/RI.F.html, https://rdrr.io/cran/fclust/man/ARI.F.html and https://rdrr.io/cran/fclust/man/JACCARD.F.html of the R package https://rdrr.io/cran/fclust/ in Python. However, it seems - even though I use numpy a lot, that the result is pretty slow. Much slower than I though. E.g. for data where I expected 0.2 secs at most, it took 2 secs. What could be the reason? What am I missing, what could I improve?

The PyCharm profiler shows that amax takes about 30% of the time, but I am not sure how to improve that. Next are outer (13%) and einsum (9%).

import numpy as np
import collections

partition_comp_return = collections.namedtuple('partition_comp_return', ["Rand_F", "adjRand_F", "Jaccard_F"])


def partition_comp_fast_short(HardClust: np.ndarray, Fuzzy: np.ndarray, minimum_instead_of_product: bool = True) -> partition_comp_return:
    if HardClust.ndim == 1:
        outliers = HardClust < 0
        HardClust[outliers] = -1
        HardClust_unique, HardClust_inverse = np.unique(HardClust, return_inverse=True)
        HardClust = np.zeros((HardClust.size, HardClust_unique.size), dtype=int)
        HardClust[range(0, HardClust.shape[0]), HardClust_inverse] = 1
        if np.any(outliers):
            HardClust = HardClust[:, 1:]

    assert HardClust.ndim == 2
    assert Fuzzy.ndim == 2
    assert HardClust.shape[0] == Fuzzy.shape[0]

    R: np.ndarray = HardClust
    Q: np.ndarray = Fuzzy

    nb_obj, nb_class = R.shape
    nb_clust = Q.shape[1]

    m = np.minimum(R[np.newaxis, :, :], R[:, np.newaxis, :]) if minimum_instead_of_product else R[np.newaxis, :, :] * R[:, np.newaxis, :]
    V = m.max(axis=-1)
    V[np.tril_indices(nb_obj)] = 0.0

    m = np.minimum.outer(R, R) if minimum_instead_of_product else np.einsum('io,jp->iojp', R, R)
    m[:, np.arange(nb_class), :, np.arange(nb_class)] = 0.0
    X = np.amax(m, axis=(1, 3))
    X[np.tril_indices(nb_obj)] = 0.0

    m = np.minimum(Q[np.newaxis, :, :], Q[:, np.newaxis, :]) if minimum_instead_of_product else Q[np.newaxis, :, :] * Q[:, np.newaxis, :]
    Y = m.max(axis=-1)
    Y[np.tril_indices(nb_obj)] = 0.0

    m = np.minimum.outer(Q, Q) if minimum_instead_of_product else np.einsum('io,jp->iojp', Q, Q)
    m[:, np.arange(nb_clust), :, np.arange(nb_clust)] = 0.0
    Z = np.amax(m, axis=(1, 3))
    Z[np.tril_indices(nb_obj)] = 0.0

    if minimum_instead_of_product:
        a = np.minimum(V, Y).sum()
        b = np.minimum(V, Z).sum()
        c = np.minimum(X, Y).sum()
        d = np.minimum(X, Z).sum()
    else:
        a = (V * Y).sum()
        b = (V * Z).sum()
        c = (X * Y).sum()
        d = (X * Z).sum()

    Rand_F = (a + d) / (a + b + c + d)
    ARand_F = (2 * (a * d - b * c)) / ((pow(b, 2)) + (pow(c, 2)) + (2 * a * d) + ((a + d) * (c + b)))
    Jaccard_F = a / (a + b + c)

    return partition_comp_return(Rand_F=Rand_F, adjRand_F=ARand_F, Jaccard_F=Jaccard_F)

    HardClust = VC = np.array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                               3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                               5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
                               4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4])
    Fuzzy = U = np.array([[0.0076787166, 7.113048e-03, 5.736663e-04, 8.412011e-04, 5.764825e-04, 9.832169e-01],
                          [0.0057712212, 2.872151e-03, 3.961336e-04, 5.816540e-04, 2.172711e-04, 9.901616e-01],
                          [0.0034208846, 3.050199e-03, 3.589399e-04, 6.086319e-04, 3.139661e-04, 9.922474e-01],
                          [0.0279150737, 8.779513e-03, 9.568724e-04, 1.851549e-03, 6.226982e-04, 9.598743e-01],
                          [0.0019726466, 1.861342e-03, 2.266066e-04, 3.723195e-04, 1.861308e-04, 9.953810e-01],
                          [0.0217428288, 2.218815e-02, 3.433951e-03, 4.792441e-03, 2.781921e-03, 9.450607e-01],
                          [0.1615720209, 4.214874e-02, 9.118115e-03, 1.452836e-02, 8.501583e-03, 7.641312e-01],
                          [0.0600584649, 4.323160e-02, 4.124345e-03, 8.649931e-03, 4.302435e-03, 8.796332e-01],
                          [0.0557432560, 3.884050e-02, 3.367109e-03, 7.601076e-03, 3.847401e-03, 8.906007e-01],
                          [0.0211994239, 2.590075e-02, 3.655883e-03, 6.272988e-03, 3.443107e-03, 9.395278e-01],
                          [0.0006062638, 4.403985e-04, 4.811967e-05, 7.791419e-05, 3.942910e-05, 9.987879e-01],
                          [0.0225039734, 1.068112e-02, 3.015998e-03, 4.895759e-03, 1.256603e-03, 9.576465e-01],
                          [0.9893402312, 3.221614e-03, 1.258685e-03, 1.992848e-03, 3.556565e-04, 3.830965e-03],
                          [0.8072825402, 4.402183e-02, 4.313880e-02, 3.991697e-02, 4.887156e-03, 6.075271e-02],
                          [0.9445378991, 2.115730e-02, 2.683231e-03, 6.099076e-03, 1.090781e-03, 2.443171e-02],
                          [0.7607765170, 7.492751e-02, 2.108496e-02, 3.280546e-02, 4.144342e-03, 1.062612e-01],
                          [0.9972648979, 9.878626e-04, 2.320282e-04, 3.614420e-04, 8.663132e-05, 1.067138e-03],
                          [0.8502374014, 4.208990e-02, 2.498097e-02, 2.404460e-02, 3.807866e-03, 5.483926e-02],
                          [0.9876004761, 4.728819e-03, 1.011830e-03, 1.693659e-03, 4.591506e-04, 4.506065e-03],
                          [0.9902351342, 3.780967e-03, 5.828589e-04, 1.068010e-03, 2.643451e-04, 4.068685e-03],
                          [0.9696105676, 1.154675e-02, 1.943744e-03, 5.277962e-03, 8.883482e-04, 1.073263e-02],
                          [0.9614931237, 1.801615e-02, 1.993549e-03, 3.634203e-03, 9.752303e-04, 1.388774e-02],
                          [0.6806979493, 1.420196e-01, 2.953315e-02, 3.200260e-02, 1.376224e-02, 1.019844e-01],
                          [0.9151662384, 2.573136e-02, 1.545858e-02, 1.715242e-02, 3.215249e-03, 2.327615e-02],
                          [0.7922914311, 9.595908e-02, 2.059959e-02, 2.233133e-02, 9.106945e-03, 5.971163e-02],
                          [0.6081442334, 1.836722e-01, 3.252763e-02, 4.358491e-02, 1.815709e-02, 1.139139e-01],
                          [0.1253379793, 6.593496e-01, 2.904808e-02, 4.463583e-02, 1.393875e-02, 1.276898e-01],
                          [0.2117828527, 3.886916e-01, 1.932635e-02, 3.680607e-02, 3.526595e-02, 3.081272e-01],
                          [0.0399338067, 8.917992e-01, 5.688628e-03, 9.069212e-03, 4.930668e-03, 4.857848e-02],
                          [0.0145935206, 9.651403e-01, 1.641272e-03, 2.454668e-03, 2.864280e-03, 1.330600e-02],
                          [0.2549469315, 3.650576e-01, 2.280736e-02, 3.351912e-02, 1.280618e-01, 1.956071e-01],
                          [0.0378589642, 9.157816e-01, 3.709495e-03, 5.018086e-03, 7.382535e-03, 3.024928e-02],
                          [0.0325314090, 9.442939e-01, 2.247034e-03, 5.083816e-03, 1.897973e-03, 1.394585e-02],
                          [0.7192770918, 1.426605e-01, 1.150844e-02, 2.977222e-02, 2.395716e-02, 7.282461e-02],
                          [0.0097196605, 9.847744e-01, 4.671710e-04, 9.843888e-04, 4.411863e-04, 3.613147e-03],
                          [0.0418776051, 9.107750e-01, 6.617200e-03, 8.042554e-03, 5.911076e-03, 2.677652e-02],
                          [0.1009396098, 8.606003e-01, 4.936793e-03, 7.626892e-03, 3.700602e-03, 2.219580e-02],
                          [0.0016298188, 1.263283e-03, 3.076376e-04, 9.214896e-04, 9.947889e-01, 1.088826e-03],
                          [0.5739998066, 7.433837e-02, 5.305344e-02, 1.833748e-01, 2.960194e-02, 8.563165e-02],
                          [0.9402506893, 2.334657e-02, 3.784468e-03, 1.206866e-02, 3.172323e-03, 1.737729e-02],
                          [0.0371007566, 9.036780e-01, 2.152564e-03, 3.186572e-03, 2.670612e-03, 5.121152e-02],
                          [0.7799436329, 1.133283e-01, 8.338929e-03, 1.167779e-02, 1.110773e-02, 7.560364e-02],
                          [0.0408534276, 9.216228e-01, 2.001232e-03, 3.308181e-03, 1.967015e-03, 3.024739e-02],
                          [0.4469709279, 1.358457e-01, 1.228568e-01, 1.007856e-01, 7.886580e-02, 1.146752e-01],
                          [0.1439993585, 3.531563e-02, 3.047881e-01, 4.578719e-01, 1.697605e-02, 4.104897e-02],
                          [0.0020298426, 9.474874e-04, 9.928119e-01, 2.907400e-03, 2.766365e-04, 1.026779e-03],
                          [0.0075241433, 2.198371e-03, 9.825371e-01, 5.073278e-03, 4.065325e-04, 2.260567e-03],
                          [0.0032096198, 1.117871e-03, 9.915901e-01, 2.644219e-03, 2.181051e-04, 1.220082e-03],
                          [0.0058390748, 2.900377e-03, 9.771325e-01, 9.925009e-03, 9.179128e-04, 3.285112e-03],
                          [0.0016969194, 5.190993e-04, 9.956088e-01, 1.509492e-03, 1.029136e-04, 5.628123e-04],
                          [0.0017098054, 6.265384e-04, 9.950579e-01, 1.762449e-03, 1.304945e-04, 7.128089e-04],
                          [0.0072687741, 3.376713e-03, 9.744052e-01, 9.839404e-03, 1.317107e-03, 3.792781e-03],
                          [0.0067838794, 1.745023e-03, 9.845139e-01, 4.628078e-03, 4.303587e-04, 1.898777e-03],
                          [0.0019331928, 5.975788e-04, 9.950722e-01, 1.572958e-03, 1.471497e-04, 6.769124e-04],
                          [0.0088429051, 3.590130e-03, 9.659274e-01, 1.626815e-02, 1.312687e-03, 4.058729e-03],
                          [0.1700967967, 1.175906e-01, 2.529414e-01, 2.626294e-01, 7.304362e-02, 1.236981e-01],
                          [0.1132191453, 3.108558e-02, 2.038415e-01, 6.029075e-01, 1.390396e-02, 3.504226e-02],
                          [0.9828429894, 8.037918e-03, 7.089755e-04, 1.573363e-03, 4.677252e-04, 6.369028e-03],
                          [0.9506949785, 1.734455e-02, 4.652054e-03, 7.685413e-03, 9.992133e-04, 1.862379e-02],
                          [0.9827122045, 8.142491e-03, 6.875906e-04, 1.466719e-03, 4.417760e-04, 6.549219e-03],
                          [0.9486784724, 1.941422e-02, 4.480298e-03, 6.977198e-03, 9.718182e-04, 1.947800e-02],
                          [0.9274426398, 3.975402e-02, 2.204948e-03, 4.472365e-03, 1.843656e-03, 2.428237e-02],
                          [0.9785789273, 9.539753e-03, 1.309212e-03, 2.029959e-03, 3.574182e-04, 8.184730e-03],
                          [0.0501320878, 3.027312e-02, 5.269740e-03, 7.981853e-03, 4.365882e-03, 9.019773e-01],
                          [0.3491308150, 1.305319e-01, 1.104979e-01, 1.161382e-01, 1.690700e-01, 1.246313e-01],
                          [0.0013259843, 4.388210e-04, 9.900583e-04, 9.965949e-01, 1.398863e-04, 5.103753e-04],
                          [0.0010000144, 3.818781e-04, 9.017760e-04, 9.971793e-01, 1.282587e-04, 4.087553e-04],
                          [0.0026944112, 9.814845e-04, 1.353360e-03, 9.933426e-01, 4.824645e-04, 1.145645e-03],
                          [0.0011870826, 4.382037e-04, 7.885286e-04, 9.969268e-01, 1.927880e-04, 4.665496e-04],
                          [0.0014449851, 5.028457e-04, 1.400346e-03, 9.958792e-01, 2.252785e-04, 5.473215e-04],
                          [0.1350226531, 1.015236e-01, 5.752991e-02, 7.809351e-02, 5.333599e-01, 9.447039e-02],
                          [0.0103180013, 1.206290e-02, 3.385997e-03, 7.096829e-03, 9.563710e-01, 1.076523e-02],
                          [0.0015640286, 1.548431e-03, 3.997652e-04, 9.765905e-04, 9.941919e-01, 1.319290e-03],
                          [0.0533482161, 4.712243e-02, 1.246132e-02, 3.920942e-02, 8.104133e-01, 3.744533e-02],
                          [0.0538531714, 1.345452e-02, 1.120729e-01, 7.994274e-01, 5.125327e-03, 1.606672e-02],
                          [0.0047903044, 2.119704e-03, 2.319098e-03, 9.873113e-01, 1.188173e-03, 2.271456e-03],
                          [0.0002620750, 8.125551e-05, 1.447590e-04, 9.993888e-01, 3.155190e-05, 9.157041e-05],
                          [0.0071613091, 3.093923e-03, 3.355745e-03, 9.803475e-01, 1.716550e-03, 4.325015e-03],
                          [0.0027432974, 1.018156e-03, 2.427727e-03, 9.919502e-01, 3.991500e-04, 1.461454e-03],
                          [0.0049388231, 2.339685e-03, 2.854995e-03, 9.863335e-01, 1.060640e-03, 2.472382e-03],
                          [0.0025401470, 9.412550e-04, 1.125002e-03, 9.939489e-01, 3.982413e-04, 1.046472e-03]])

    Rand_F, adjRand_F, Jaccard_F = partition_comp_fast(HardClust, Fuzzy, minimum_instead_of_product=True)
    assert np.allclose(np.array([Rand_F, adjRand_F, Jaccard_F]), np.array([0.7987886, 0.3959664, 0.3538935]))

    Rand_F, adjRand_F, Jaccard_F = partition_comp_fast(HardClust, Fuzzy, minimum_instead_of_product=False)
    assert np.allclose(np.array([Rand_F, adjRand_F, Jaccard_F]), np.array([0.803873, 0.4055851, 0.3595149]))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.