scipy - How to cross check a python list of dictionaries against a csr matrix -


i have csr matrix:

  (0, 12114) 4   (0, 12001) 1   (0, 11998) 2   (0, 11132) 1   (0, 10412) 7   (1, 10096) 3   (1, 10085) 1   (1, 9105)  8   (1, 8925)  5   (1, 8660)  2   (2, 6577)  2   (2, 6491)  4   (3, 6178)  8   (3, 5286)  1   (3, 5147)  7   (3, 4466)  3 

and list of dictionaries:

[{11998: 0.27257158100079237, 12114: 0.27024630707640002},   {10085: 0.23909781233007368, 9105: 0.57533007741289421},   {6577: 0.45085059256989168, 6491: 0.5895717192325539},   {5286: 0.4482789582819417, 6178: 0.32295433881928487}] 

i'd find way search each dictionary in list against corresponding row in matrix (e.g. row 0 against first dictionary) , replace each value in dictionary value matrix, according key...

so result be:

[{11998: 2, 12114: 4},  {10085: 1, 9105: 8},    {6577: 2, 6491: 4},   {5286: 1, 6178: 8}] 

if x sparse matrix and

d = [{11998: 0.27257158100079237, 12114: 0.27024630707640002},       {10085: 0.23909781233007368, 9105: 0.57533007741289421},       {6577: 0.45085059256989168, 6491: 0.5895717192325539},       {5286: 0.4482789582819417, 6178: 0.32295433881928487}] 

then

for i, d in enumerate(d):     j in d:         d[j] = x[i, j] 

gives desired result:

>>> d [{12114: 4.0, 11998: 2.0}, {9105: 8.0, 10085: 1.0}, {6577: 2.0, 6491: 4.0}, {6178: 8.0, 5286: 1.0}] 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -