Created
October 23, 2014 21:57
-
-
Save lkilcher/e5e502af941bacd4dcfb to your computer and use it in GitHub Desktop.
test_sparse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy import sparse as sps | |
import numpy as np | |
z = np.arange(10) | |
N = len(z) | |
c = np.tile(z[:, None], (1, 10)).astype(int) | |
# Change some values: | |
c[:, 3] = 0 | |
c[3, 7] = 100 | |
c[3, 5] = 0 | |
c[8, 8] = 30 | |
c[7, 8] = 0 | |
# Cast this into a sparse matrix: | |
a = sps.csc_matrix(c) | |
# Do the count in a loop: | |
count = 0 | |
for i in range(N): | |
count += (z == a[:, i]).sum() | |
print "The shape of '(z == a[:, i])' is: " + str((z == a[:, i]).shape) | |
print (a == z[:, None]).sum() | |
print count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment