Skip to content

Sparseness measure returns 1 for array of zeros #64

@EricThomson

Description

@EricThomson

I'm playing around with the faces example, and the sparseness metric seems to be acting a little strange in some cases (the code is here).

I pulled it out to test it in different cases:

def sparseness_nimfa(x):
    """
    reimplementation of sparseness measure
    """
    from nimfa.utils.linalg import multiply

    eps = np.finfo(x.dtype).eps if 'int' not in str(x.dtype) else 1e-9
    x1 = np.sqrt(x.size) - (abs(x).sum() + eps) / \
        (np.sqrt(multiply(x, x).sum()) + eps)
    x2 = np.sqrt(x.size) - 1
    return x1 / x2

This is the function that sparseness() averages over for W and H. The problem is that for an array of zeros, the above returns a 1, and we want it to return a 0. This happens fairly frequently in practice with some parameter settings for NMF.

To test it:

test_array1 = np.ones(5000)
test_array2 = np.zeros(5000)

print(sparseness_nimfa(test_array1)) # 0
print(sparseness_nimfa(test_array2)) # 1

What I have personally been doing is checking to see if the input is an array of zeros (within some rounding error), and if so, return 0. This hack seems to work well when W/H are nonnegative (though be careful of the more general case).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions