关于重塑numpy数组 [英] About reshaping numpy array

查看:103
本文介绍了关于重塑numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

trainX.size == 43120000
trainX = trainX.reshape([-1, 28, 28, 1])

(1)重塑是否接受列表作为argment而不是元组?

(1)Does reshape accept a list as an argment instead of a tuple?

(2)以下两个语句是否等效?

(2)Are the following two statements equivalent?

trainX = trainX.reshape([-1, 28, 28, 1])
trainX = trainX.reshape((55000, 28, 28, 1))

推荐答案

尝试各种版本:

In [1]: np.arange(12).reshape(3,4)
Out[1]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [2]: np.arange(12).reshape([3,4])
Out[2]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [3]: np.arange(12).reshape((3,4))
Out[3]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

使用reshape方法,形状可以是参数,元组或列表. reshape函数必须位于列表或元组中,以将它们与第一个数组参数分开

With the reshape method, the shape can be arguments, a tuple or a list. In the reshape function is has to be in a list or tuple, to separate them from the first array argument

In [4]: np.reshape(np.arange(12), (3,4))
Out[4]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

是的,可以使用一个-1.整形的总大小是固定的,因此可以从其他值中推导出一个值.

and yes, one -1 can be used. The total size of the reshape is fixed, so one value can be deduced from the others.

In [5]: np.arange(12).reshape(-1,4)
Out[5]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

方法文档中有以下注释:

The method documentation has this note:

与自由功能numpy.reshape不同,ndarray上的此方法允许 要作为单独参数传递的shape参数的元素. 例如,a.reshape(10, 11)等效于 a.reshape((10, 11)).

Unlike the free function numpy.reshape, this method on ndarray allows the elements of the shape parameter to be passed in as separate arguments. For example, a.reshape(10, 11) is equivalent to a.reshape((10, 11)).

这是一个内置函数,但签名看起来像x.reshape(*shape),只要值有意义,它就会尝试保持灵活性.

It's a builtin function, but the signature looks like x.reshape(*shape), and it tries to be flexible as long as the values make sense.

这篇关于关于重塑numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆