Python: Does 'kron' create sparse matrix when I use ' from scipy.sparse import * '?
<p>For the code below, Mat is a array-type matrix,</p>

<pre><code>a = kron(Mat,ones((8,1)))
b = a.flatten()
</code></pre>

<p>If I don't import scipy.sparse package, <code>a</code> is an <strong>array-type matrix</strong>, <code>b</code> can also be executed.
If I use 'from scipy.sparse import *', <code>a</code> is a <strong>sparse-type matrix</strong>, <code>b</code> <strong>cannot</strong> be exectued.
Can someone tell me why <code>kron</code> gives different results? 
And, whether flatten() can be applied to sparse-type matrix?</p>

<p><code>from module import *</code> is generally considered bad form in application code, for the reason you're seeing - it makes it very hard to tell which modules functions are coming from, especially if you do this for more than one module</p>

<p>Right now, you have:</p>

<pre><code>from numpy import *
# from scipy.sparse import *
a = kron(Mat,ones((8,1)))
b = a.flatten()
</code></pre>

<p>Uncommenting the second line might affect where <code>ones</code> and <code>kron</code> comes from. But unless you look up whether sparse redefines these, you won't know. Better to write it like this:</p>

<pre><code>import numpy as np
from scipy import sparse
a = np.kron(Mat, np.ones((8,1)))
b = a.flatten()
</code></pre>

<p>And then you can swap <code>np</code> for <code>sparse</code> where you want to use the sparse version, and the reader will immediately know which one you're using. And you'll get an error if you try to use a sparse version when in fact there isn't one.</p>

python numpy matrix scipy sparse-matrix