python - Dictionaries containing the biggest values -
i have scipy csr_matrix:
(0, 12114) 0.272571581001 (0, 12001) 0.0598986479579 (0, 11998) 0.137415042369 (0, 11132) 0.0681428952502 (0, 10412) 0.0681428952502 (1, 10096) 0.0990242494495 (1, 10085) 0.216197045661 (1, 9105) 0.1362857905 (1, 8925) 0.042670696769 (1, 8660) 0.0598986479579 (2, 6577) 0.119797295916 (2, 6491) 0.0985172979468 (3, 6178) 0.1362857905 (3, 5286) 0.119797295916 (3, 5147) 0.270246307076 (3, 4466) 0.0540492614153 (4, 3810) 0.0540492614153 (4, 3773) 0.0495121247248
and find way create (in case 4) dictionaries each dictionary contains 2 biggest values each row..
so example, row 0 dictionary be:
dict0 = {12114: '0.27257158100111998', 11998: '0.137415042369'}
and row 1:
dict1 = {10085: '0.216197045661', 9105: '0.1362857905'}
since csr_matrix
not have sort()
method, convenient transform row need array first:
a = m[i,:].toarray().flatten()
to positions of sorted columns:
argsa = a.argsort()
the biggest values @ last columns of argsa
, columns of 2 biggest values are:
argsa[-2:]
to pair column, value
:
argsa[-2:], a[ argsa[-2:] ]
and can transformed in dict:
dict( zip( argsa[-2:], a[ argsa[-2:] ] ) )
your final function can sth like:
def get_from_m(m, i, numc=2): = m[i,:].toarray().flatten() argsa = a.argsort() return dict( zip( argsa[-numc:], a[ argsa[-numc:] ] ) )
Comments
Post a Comment