擅长:python、mysql、java
<p>以防有人想要。这里有一个一般的方法</p>
<pre><code>import functools
def combine_dims(a, i=0, n=1):
"""
Combines dimensions of numpy array `a`,
starting at index `i`,
and combining `n` dimensions
"""
s = list(a.shape)
combined = functools.reduce(lambda x,y: x*y, s[i:i+n+1])
return np.reshape(a, s[:i] + [combined] + s[i+n+1:])
</code></pre>
<p>有了这个功能,您可以这样使用它:</p>
<pre><code>imgs = combine_dims(imgs, 1) # combines dimension 1 and 2
# imgs.shape = (100, 718*686, 3)
</code></pre>