Is it possible to cast dtype of scipy CSR matrix to NPY_FLOAT?

回眸只為那壹抹淺笑 提交于 2020-01-04 06:07:17

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/39624043/is-it-possible-to-cast-dtype-of-scipy-csr-matrix-to-npy-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!