擅长:python、mysql、java
<p>在<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy.reshape">reshape</a>的文档中,有一些关于在无法创建视图时如何确保异常的信息:</p>
<blockquote>
<p>It is not always possible to change the shape of an array without copying the data. If you want an error to be raised if the data is copied, you should assign the new shape to the shape attribute of the array:</p>
</blockquote>
<pre><code>>>> a = np.zeros((10, 2))
# A transpose make the array non-contiguous
>>> b = a.T
# Taking a view makes it possible to modify the shape without modiying the
# initial object.
>>> c = b.view()
>>> c.shape = (20)
AttributeError: incompatible shape for a non-contiguous array
</code></pre>
<p><br/>
<br/>
这并不完全是对你问题的回答,但在某些情况下,它可能同样有用。</p>