Skip to content

Instantly share code, notes, and snippets.

@lkilcher
Created October 23, 2014 21:57
Show Gist options
  • Save lkilcher/e5e502af941bacd4dcfb to your computer and use it in GitHub Desktop.
Save lkilcher/e5e502af941bacd4dcfb to your computer and use it in GitHub Desktop.
test_sparse
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