Skip to content

Instantly share code, notes, and snippets.

@fcostin
Created February 27, 2010 14:11
Show Gist options
  • Save fcostin/316713 to your computer and use it in GitHub Desktop.
Save fcostin/316713 to your computer and use it in GitHub Desktop.
def to_sparse(p_dense):
states = numpy.indices(numpy.shape(p_dense))
p_sparse = {}
for state in states.transpose():
state = tuple(state)
probability = p_dense[state]
if probability > 0:
p_sparse[state] = p_dense[state]
return p_sparse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment