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

bug for log1p on csr_matrix #8228

Closed
XinyuHua opened this issue Dec 18, 2017 · 1 comment · Fixed by #8232
Closed

bug for log1p on csr_matrix #8228

XinyuHua opened this issue Dec 18, 2017 · 1 comment · Fixed by #8232
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse.linalg
Milestone

Comments

@XinyuHua
Copy link

I'm trying to use log1p function on csr_matrix. I use two different ways to construct the same csr_matrix but got different results after applying log1p.

Create the matrix by directly using a python list:

>>> mat_1_raw = [[2, 2, 0, 0, 0], [0, 0, 1, 1, 1]]
>>> mat_1 = csr_matrix(mat_1_raw)
>>> mat_1.log1p().toarray()
array([[ 1.09861229,  1.09861229,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.69314718,  0.69314718,  0.69314718]])

Create the matrix by using indices:

>>> indices = [0, 1, 0, 1, 2, 3, 4]
>>> indptr = [0, 4, 7]
>>> data = [1, 1, 1, 1, 1, 1, 1]
>>> mat_2 = csr_matrix((data, indices, indptr), shape=(2, 5))
>>> mat_2.toarray()
array([[ 2.,  2.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.]])
>>> mat_2.log1p().toarray()
array([[ 1.38629436,  1.38629436,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.69314718,  0.69314718,  0.69314718]])

Apparently in the second case there are two separate 1s and two separate 0s added to the same entry in the final csr_matrix, and the corresponding entry in the log1p result is log(1+1) * 2 = 1.39, where it should be log(1 + 2*1) = 1.099.

@pv pv reopened this Dec 18, 2017
@XinyuHua
Copy link
Author

Solved by adding the following before applying log1p:
>>> mat_2.sum_duplicates()

@ilayn ilayn added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse.linalg labels Dec 18, 2017
perimosocordiae added a commit to perimosocordiae/scipy that referenced this issue Dec 19, 2017
@pv pv closed this as completed in #8232 Apr 7, 2018
@pv pv added this to the 1.1.0 milestone Apr 7, 2018
StrahinjaLukic pushed a commit to StrahinjaLukic/scipy that referenced this issue Apr 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse.linalg
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants