public
anonymous / gist:4724485
Created

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12
343 def sparsify(m, value=100):
344
345 matrix = m.tolil()
346 rows, columns = matrix.shape
347 sparseindex = matrix.mean(axis=1) * (value/float(100))
348 for r in range(rows):
349 z = np.where(matrix.tocsr()[r].todense() < sparseindex[r])[1]
350 for c in range(z.shape[1]):
351 var = int(z[0,c])
352 matrix[r, var] = 0
353
354 return matrix.tocsr()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.