Python Numpy`resize`在使用-1语法时提供了意外的数组大小
我正在试图理解Python Numpy`resize`在使用-1语法时提供了意外的数组大小,python,numpy,Python,Numpy,我正在试图理解numpy.resize和numpy.resforme之间的区别。我知道,resize将返回一个新数组,resformate将维护相同的基础数据,并且只调整大小(我假设它通过更改步幅来实现这一点)。但是,对于这两个函数,我希望能够使用-1语法来指定轴的大小。但是,这似乎只适用于重塑。例如,尝试将此一维形状数组(444,)重塑为形状数组(4111),根据您是使用resize还是resize,会产生两种不同的结果: 将numpy导入为np 测试=np.arange(0444) 打印(测
numpy.resize
和numpy.resforme
之间的区别。我知道,resize
将返回一个新数组,resformate
将维护相同的基础数据,并且只调整大小(我假设它通过更改步幅来实现这一点)。但是,对于这两个函数,我希望能够使用-1
语法来指定轴的大小。但是,这似乎只适用于重塑
。例如,尝试将此一维形状数组(444,)
重塑为形状数组(4111)
,根据您是使用resize
还是resize
,会产生两种不同的结果:
将numpy导入为np
测试=np.arange(0444)
打印(测试形状)
打印(np.调整大小(测试,(4,-1)).形状)
打印(np.整形(测试,(4,-1)).形状)
印刷品
(444,)
(4, 110)
(4, 111)
我假设我遗漏了
resize
函数的某些内容,但我希望它要么输出与(444,)
兼容的形状,要么抛出一个错误。np。未记录-1
的resize
行为。但可以从Python代码或以下示例中推断:
In [312]: np.resize(np.arange(12),(1,-1))
Out[312]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
In [313]: np.resize(np.arange(12),(2,-1))
Out[313]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [314]: np.resize(np.arange(12),(3,-1))
Out[314]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [315]: np.resize(np.arange(12),(4,-1))
Out[315]:
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
# (5,-1) error
In [317]: np.resize(np.arange(12),(6,-1))
Out[317]:
array([[0],
[1],
[2],
[3],
[4],
[5]])
一般来说
a = np.arange(n)
np.resize(a, (m,-1))
np.reshape(a[:(n-m)], (m,-1))
也就是说,它通过m
元素剪裁输入,并尝试重塑
。不是很有用,是吗
在您的情况下,
测试[:-4]。重塑(4,-1)
np。不记录使用-1
调整
行为的大小。但可以从Python代码或以下示例中推断:
In [312]: np.resize(np.arange(12),(1,-1))
Out[312]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
In [313]: np.resize(np.arange(12),(2,-1))
Out[313]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [314]: np.resize(np.arange(12),(3,-1))
Out[314]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [315]: np.resize(np.arange(12),(4,-1))
Out[315]:
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
# (5,-1) error
In [317]: np.resize(np.arange(12),(6,-1))
Out[317]:
array([[0],
[1],
[2],
[3],
[4],
[5]])
一般来说
a = np.arange(n)
np.resize(a, (m,-1))
np.reshape(a[:(n-m)], (m,-1))
也就是说,它通过m
元素剪裁输入,并尝试重塑
。不是很有用,是吗
在您的示例中,
test[:-4]。重塑(4,-1)
通过的源代码,您可以看到发生了什么,并且-1
从来没有打算作为输入:
def resize(a, new_shape):
if isinstance(new_shape, (int, nt.integer)):
new_shape = (new_shape,)
a = ravel(a)
Na = len(a) # Na = 444
total_size = um.multiply.reduce(new_shape) # total_size = 4 * -1 = -4 (?!?)
if Na == 0 or total_size == 0:
return mu.zeros(new_shape, a.dtype)
n_copies = int(total_size / Na) # n_copies = 0, luckily
extra = total_size % Na # -4 % 444 = 440 (this is where the 110 comes from)
if extra != 0: # True
n_copies = n_copies + 1 # n_copies = 1
extra = Na - extra # extra = 444 - 440 = 4
a = concatenate((a,) * n_copies) # a stays same
if extra > 0: # True
a = a[:-extra] # a = a[:-4]
return reshape(a, new_shape) # this is .reshape(4, -1) which now gives (4, 110)
浏览的源代码,您可以看到正在发生的事情,并且
-1
从未打算用作输入:
def resize(a, new_shape):
if isinstance(new_shape, (int, nt.integer)):
new_shape = (new_shape,)
a = ravel(a)
Na = len(a) # Na = 444
total_size = um.multiply.reduce(new_shape) # total_size = 4 * -1 = -4 (?!?)
if Na == 0 or total_size == 0:
return mu.zeros(new_shape, a.dtype)
n_copies = int(total_size / Na) # n_copies = 0, luckily
extra = total_size % Na # -4 % 444 = 440 (this is where the 110 comes from)
if extra != 0: # True
n_copies = n_copies + 1 # n_copies = 1
extra = Na - extra # extra = 444 - 440 = 4
a = concatenate((a,) * n_copies) # a stays same
if extra > 0: # True
a = a[:-extra] # a = a[:-4]
return reshape(a, new_shape) # this is .reshape(4, -1) which now gives (4, 110)
数组本身的
resize
方法明确禁止负维,我想知道这个方法是否也应该这样做。@user3483203啊哈,你说得对。也许这实际上应该是numpy repo中的一个bug…resize
不经常使用,如果您想更改元素的总数,这是最好的。也就是说,它可以夹或垫。注意函数和方法的行为完全不同resize
无法更改元素总数,广泛用于将维度总数更改为2d或1d等。我不会将它们视为备选方案。在文档中,np.resize
解释了-1的用法吗?数组上的resize
方法本身明确禁止负维度,我不知道这个方法是否应该做同样的事情。@user3483203啊哈,你说得对。也许这实际上应该是numpy repo中的一个bug…resize
不经常使用,如果您想更改元素的总数,这是最好的。也就是说,它可以夹或垫。注意函数和方法的行为完全不同<代码>重塑无法更改元素总数,广泛用于将维度总数更改为2d或1d等。我不会将它们视为备选方案。在文档中,np.resize
在哪里解释了-1的用法?