Is it possible to cast dtype of scipy CSR matrix to NPY_FLOAT?
<p>I have a scipy CSR matrix that was constructed from a COO matrix as follows:</p>

<pre><code>coord_mat = coo_matrix((data, (row, col)), dtype=np.float64)
</code></pre>

<p>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 <code>double(np.float64)</code>. However, I'm encountering the following error:</p>

<pre><code>ValueError: Buffer dtype mismatch, expected 'flt' but got 'double'
</code></pre>

<p>I went to do some research and found the <a href="http://docs.scipy.org/doc/numpy/reference/c-api.dtype.html#c.NPY_FLOAT" rel="nofollow">scipy C-api</a>, which informed me that the <code>NPY_FLOAT</code> 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 <code>NPY_FLOAT</code> object in order to cast it.</p>

<p>Any help on this matter would be deeply appreciated!</p>

<p>I'm not sure about the <code>C</code> interface, I'll try to explain the <code>coo_matrix</code> part.</p>

<p>Since you are using the tuple input it splits that into 3 variables</p>

<pre><code>obj, (row, col) = arg1
</code></pre>

<p>then it assigns those to attributes</p>

<pre><code>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)
</code></pre>

<p>and since you specified a dtype</p>

<pre><code>if dtype is not None:
   self.data = self.data.astype(dtype)
</code></pre>

<p>If <code>data</code>, <code>row</code> and <code>col</code> 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.</p>

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

python numpy scipy python-c-api