Question

I'm trying to take the dot product of a row in a sparse matrix with the transpose of that row using Python. I have a huge sparse matrix called X2. And I am saving the results (which is supposed to be a single number) in a list called Njc.

    X2 = X.transpose()
    for row in X2:
        Njc.append(dot(row,row.transpose()))

However, when I run my program, the results are not single numbers. They look like: (0, 0) 355

(0, 0) 295

(0, 0) 15

(0, 0) 204

(0, 0) 66

....

Unfortunately my sparse matrix is so huge that I can't make it into a dense matrix (my memory will blow up). Is there a way to get only the numbers on the right without the couples on the left?

Was it helpful?

Solution

The dot is returning a sparse matrix. To pick out the one value inside the sparse matrix, you could use .todense().item():

Njc.append((np.dot(row, row.transpose())).todense().item())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top