Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparseness measure returns 1 for array of zeros #64

Open
EricThomson opened this issue Oct 10, 2022 · 0 comments
Open

Sparseness measure returns 1 for array of zeros #64

EricThomson opened this issue Oct 10, 2022 · 0 comments

Comments

@EricThomson
Copy link

EricThomson commented Oct 10, 2022

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant