Last active
May 6, 2021 09:01
-
-
Save ikegami-yukino/5dab91d87a627e837d09 to your computer and use it in GitHub Desktop.
dict to scipy.sparse
This file contains 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
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) | |
""" |
This file contains 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
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