是否可以将scipy CSR矩阵的dtype转换为NPY_FLOAT? [英] Is it possible to cast dtype of scipy CSR matrix to NPY_FLOAT?
问题描述
我有一个根据COO矩阵构造的scipy CSR矩阵,如下所示:
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)
它被用作具有基础C实现的库的输入,我相信我矩阵的dtype是double(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'
我去做一些研究,发现 scipy C-api ,它告诉我NPY_FLOAT
数据类型在C中转换为32位浮点数,而我当前拥有的数据类型对应于64位double.我在正确的轨道上吗?如果是这样,我该如何转换数组的类型?我不完全确定如何调用NPY_FLOAT
对象以进行投射.
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!
推荐答案
我不确定C
接口,我将尝试解释coo_matrix
部分.
I'm not sure about the C
interface, I'll try to explain the coo_matrix
part.
由于您正在使用元组输入,因此会将其分为3个变量
Since you are using the tuple input it splits that into 3 variables
obj, (row, col) = arg1
然后将其分配给属性
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)
并且由于您指定了dtype
and since you specified a dtype
if dtype is not None:
self.data = self.data.astype(dtype)
如果data
,row
和col
已经是数组,则只要您未指定dtype,稀疏矩阵都可以将这些输入用作属性,而无需复制.您的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.
稀疏矩阵不是numpy
数组,而是一个具有3个数组作为属性的对象.矩阵接受astype
方法,该方法可能执行相同的self.data.astype
操作.因此,我认为您的情况可以归结为:您可以将任何数组强制转换为该类型吗?
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.
这篇关于是否可以将scipy CSR矩阵的dtype转换为NPY_FLOAT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!