Cur*_*ous 98 python numpy flatten
有没有一种快速的方法来"压平"或平整一些numpy数组中的第一个维度?
例如,给定一个numpy维度数组(50,100,25)
,结果尺寸将是(5000,25)
Ale*_*der 109
>>> arr = numpy.zeros((50,100,25))
>>> arr.shape
# (50, 100, 25)
>>> new_arr = arr.reshape(5000,25)
>>> new_arr.shape
# (5000, 25)
# One shape dimension can be -1.
# In this case, the value is inferred from
# the length of the array and remaining dimensions.
>>> another_arr = arr.reshape(-1, arr.shape[-1])
>>> another_arr.shape
# (5000, 25)
Run Code Online (Sandbox Code Playgroud)
Pet*_*ter 74
对Alexander的答案略有概括 - np.reshape可以将-1作为参数,意思是"总数组大小除以所有其他列出的维度的乘积":
例如,除了最后一个维度之外的所有内容:
>>> arr = numpy.zeros((50,100,25))
>>> new_arr = arr.reshape(-1, arr.shape[-1])
>>> new_arr.shape
# (5000, 25)
Run Code Online (Sandbox Code Playgroud)
Kei*_*hWM 23
稍微概括一下Peter的答案 - 如果你想要超越三维数组,你可以指定原始数组形状的范围.
例如,除了最后两个维度之外的所有内容:
arr = numpy.zeros((3, 4, 5, 6))
new_arr = arr.reshape(-1, *arr.shape[-2:])
new_arr.shape
# (12, 5, 6)
Run Code Online (Sandbox Code Playgroud)
编辑:稍微概括一下我之前的答案 - 你当然也可以在重塑的开头指定一个范围:
arr = numpy.zeros((3, 4, 5, 6, 7, 8))
new_arr = arr.reshape(*arr.shape[:2], -1, *arr.shape[-2:])
new_arr.shape
# (3, 4, 30, 7, 8)
Run Code Online (Sandbox Code Playgroud)
She*_*man 17
numpy.vstack
非常适合这种情况
import numpy as np
arr = np.ones((50,100,25))
np.vstack(arr).shape
> (5000, 25)
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用stack
,vstack
或hstack
overreshape
因为它reshape
只是扫描数据并且似乎将其强制转换为所需的形状。例如,如果您要获取列平均值,这可能会出现问题。
这是我的意思的一个例子。假设我们有以下数组
>>> arr.shape
(2, 3, 4)
>>> arr
array([[[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]],
[[7, 7, 7, 7],
[7, 7, 7, 7],
[7, 7, 7, 7]]])
Run Code Online (Sandbox Code Playgroud)
我们应用这两种方法来获得形状为 (3,8) 的数组
>>> arr.reshape((3,8)).shape
(3, 8)
>>> np.hstack(arr).shape
(3, 8)
Run Code Online (Sandbox Code Playgroud)
然而,如果我们看看它们在每种情况下是如何被重塑的,那么hstack
我们就可以得到列的总和,我们也可以从原始数组中计算出列的总和。通过重塑,这是不可能的。
>>> arr.reshape((3,8))
array([[1, 2, 3, 4, 1, 2, 3, 4],
[1, 2, 3, 4, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7]])
>>> np.hstack(arr)
array([[1, 2, 3, 4, 7, 7, 7, 7],
[1, 2, 3, 4, 7, 7, 7, 7],
[1, 2, 3, 4, 7, 7, 7, 7]])
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用numpy.resize()
如下所示:
In [37]: shp = (50,100,25)
In [38]: arr = np.random.random_sample(shp)
In [45]: resized_arr = np.resize(arr, (np.prod(shp[:2]), shp[-1]))
In [46]: resized_arr.shape
Out[46]: (5000, 25)
# sanity check with other solutions
In [47]: resized = np.reshape(arr, (-1, shp[-1]))
In [48]: np.allclose(resized_arr, resized)
Out[48]: True
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70477 次 |
最近记录: |