How to sum over columns with some weight in a csr matrix in python -
if have large csr_matrix a, want sum on columns, a.sum(axis=0)
me, right? corresponding axis values: 1->rows, 0->columns?
i stuck when want sum on columns weights specified in list, e.g. [1 2 3 4 5 4 3 ... 4 2 5] same length number of rows in csr_matrix a. more clear, want inner product of each column vector weight vector. how can achieve python?
this part of code:
unifeature = csr_matrix(unifeature) [i,j] = unifeature.shape sumfreq = unifeature.sum(axis=0) sumratings = [] j in range(j): column = unifeature.getcol(j) column = column.toarray() sumtemp = np.dot(ratings,column) sumratings.append(sumtemp) sumfreq = sumfreq.toarray() average = np.true_divide(sumratings,sumfreq)
(numpy imported np) there weight vector "ratings", program supposed output average rating each column of matrix "unifeature".
i experimented dot column=unifeature.getcol(j) directly ratings(which list), there error says format not agree. it's ok after column.toarray() dot ratings. isn't making each column dense form losing point of having sparse matrix , slow? ran above code , it's slow show results. guess there should way dots vector "ratings" each column of sparse matrix efficiently.
thanks in advance!
Comments
Post a Comment