Skip to content

Instantly share code, notes, and snippets.

@ikegami-yukino
Last active May 6, 2021 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikegami-yukino/5dab91d87a627e837d09 to your computer and use it in GitHub Desktop.
Save ikegami-yukino/5dab91d87a627e837d09 to your computer and use it in GitHub Desktop.
dict to scipy.sparse
import numpy as np
from scipy.sparse import csr_matrix
def dict2sparse(d):
data = list(d.values())
indices = list(d.keys())
indptr = [0, len(d)]
return csr_matrix((data, indices, indptr), shape=(1, max(d)+1), dtype=np.uint32)
"""
> d = {1: 10, 2: 20, 3: 30}
> X = dict2sparse(d)
> X.toarray()
# => array([[ 0, 10, 20, 30]], dtype=uint32)
"""
import numpy as np
from scipy.sparse import csr_matrix
def counter_to_sparse1(counter):
vec = csr_matrix((1, max(counter)+1), dtype=np.uint32)
for (k, v) in counter.items():
vec[0, k] = v
return vec
def counter_to_sparse2(counter):
data = list(counter.values())
indices = list(counter.keys())
indptr = [0, len(counter)]
X = csr_matrix((data, indices, indptr), shape=(1, max(d)+1), dtype=np.uint32)
return X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment