2

I have a scipy CSR matrix that was constructed from a COO matrix as follows:

coord_mat = coo_matrix((data, (row, col)), dtype=np.float64)

It is being used as an input to a library with an underlying C implementation, and I believe that the dtype of my matrix is double(np.float64). However, I'm encountering the following error:

ValueError: Buffer dtype mismatch, expected 'flt' but got 'double'

I went to do some research and found the scipy C-api, which informed me that the NPY_FLOAT data type is converted to a 32-bit float in C, while the current data type I have corresponds to a 64-bit double. Am I on the right track here? If so, how do I cast the type of the array? I'm not entirely sure how I can call on the NPY_FLOAT object in order to cast it.

Any help on this matter would be deeply appreciated!

andre
  • 41
  • 6

1 Answers1

0

I'm not sure about the C interface, I'll try to explain the coo_matrix part.

Since you are using the tuple input it splits that into 3 variables

obj, (row, col) = arg1

then it assigns those to attributes

self.row = np.array(row, copy=copy, dtype=idx_dtype)
self.col = np.array(col, copy=copy, dtype=idx_dtype)
self.data = np.array(obj, copy=copy)

and since you specified a dtype

if dtype is not None:
   self.data = self.data.astype(dtype)

If data, row and col are already arrays, any you didn't specify the dtype, the sparse matrix can use those inputs as attributes without copying. Your dtype parameter will produce a copy.

The sparse matrix is not a numpy array, but rather an object that has 3 arrays as attribute. The matrix accepts the astype method, which probably does that same self.data.astype action. So I think you case comes down to: can you cast any array to that type.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thanks for the input! That's precisely the problem I'm facing, I suspect I need to cast the array to a `NPY_FLOAT` dtype in order to ensure compatibility - alas I'm not sure how to do so and I can't find documentation online so I've no clue as to whether I'm even on the right track or not. – andre Sep 22 '16 at 18:21