Skip to content Skip to sidebar Skip to footer

Copying Values Of A Numpy Array Into Specific Location Of Sparse Matrix

So I am trying to copy values from one numpy array into a sparse matrix. The first array looks like this: results_array = [[ 3.00000000e+00 1.00000000e+00 4.00000000e+00 1.0

Solution 1:

The first element in the tuple you pass to csc_matrix needs to be a vector of values, whereas you are passing it an integer. More fundamentally, you're trying to call the csc_matrix constructor multiple times in a loop so that it would overwrite sparse_matrix on each iteration.

You want to call csc_matrixonce with a vector for each parameter, like this:

values = results_array[:, 3]
row_idx = results_array[:, 2]
col_idx = results_array[:, 1]

sparse_array = csc_matrix((values, (row_idx, col_idx)), shape=(14, 14))

Post a Comment for "Copying Values Of A Numpy Array Into Specific Location Of Sparse Matrix"