我想重塑从三维到二维的描绘的numpy数组.不幸的是,订单不正确.
假设有一个numpy数组(1024,64,100),并希望将其转换为(1024*100,64).
有人知道如何维持秩序吗?
我有一个示例数据
data[0,0,0]=1
data[0,1,0]=2
data[0,2,0]=3
data[0,3,0]=4
data[1,0,0]=5
data[1,1,0]=6
data[1,2,0]=7
data[1,3,0]=8
data[2,0,0]=9
data[2,1,0]=10
data[2,2,0]=11
data[2,3,0]=12
data[0,0,1]=20
data[0,1,1]=21
data[0,2,1]=22
data[0,3,1]=23
data[1,0,1]=24
data[1,1,1]=25
data[1,2,1]=26
data[1,3,1]=27
data[2,0,1]=28
data[2,1,1]=29
data[2,2,1]=30
data[2,3,1]=31
Run Code Online (Sandbox Code Playgroud)
我希望得到这样的结果:
array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[ 20., 21., 22., 23.],
[ 24., 25., 26., 27.],
[ 28., 29., 30., 31.]])
Run Code Online (Sandbox Code Playgroud)
此外,我还希望以另一种方式进行重塑,即:
array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[ 20., 21., 22., 23.], …Run Code Online (Sandbox Code Playgroud) 我想以"pythonic"方式将3D numpy数组拆分为3D块.我正在处理有些大型数组(1000X1200X1600)的图像序列,所以我需要将它们分成几块来进行处理.
我已经写了这样做的函数,但是我想知道是否有一种本地的numpy方法来实现这一点 - numpy.split似乎没有做我想要的3D数组(但也许我不理解它的功能)
要明确:下面的代码完成了我的任务,但我正在寻求一种更快的方法来完成它.
def make_blocks(x,t):
#x should be a yXmXn matrix, and t should even divides m,n
#returns a list of 3D blocks of size yXtXt
down = range(0,x.shape[1],t)
across = range(0,x.shape[2],t)
reshaped = []
for d in down:
for a in across:
reshaped.append(x[:,d:d+t,a:a+t])
return reshaped
def unmake_blocks(x,d,m,n):
#this takes a list of matrix blocks of size dXd that is m*n/d^2 long
#returns a 2D array of size mXn
rows = []
for i in range(0,int(m/d)):
rows.append(np.hstack(x[i*int(n/d):(i+1)*int(n/d)])) …Run Code Online (Sandbox Code Playgroud) 我使用此功能将(512x512)2维阵列划分为2x2块.
skimage.util.view_as_blocks (arr_in, block_shape)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> B = view_as_blocks(A, block_shape=(2, 2))
>>> B[0, 0]
array([[0, 1],
[4, 5]])
>>> B[0, 1]
array([[2, 3],
[6, 7]])
Run Code Online (Sandbox Code Playgroud)
现在我需要在操作之后将相同的块放到原始位置,但是我在skimage中看不到任何功能.
合并非重叠数组的最佳方法是什么?
谢谢!
我有以下数组:
x = np.arange(24).reshape((2,3,2,2))
array([[[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]],
[[[12, 13],
[14, 15]],
[[16, 17],
[18, 19]],
[[20, 21],
[22, 23]]]])
Run Code Online (Sandbox Code Playgroud)
我想将它重塑为一个 (3,4,2) 数组,如下所示:
array([[[ 0, 1],
[ 2, 3],
[12, 13],
[14, 15]],
[[ 4, 5],
[ 6, 7],
[16, 17],
[18, 19]],
[[ 8, 9],
[10, 11],
[20, 21],
[22, 23]]])
Run Code Online (Sandbox Code Playgroud)
我尝试使用 reshape 但它给了我以下不是我想要的。
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]], …Run Code Online (Sandbox Code Playgroud) 让我们说我有A = [1:8; 11:18; 21:28; 31:38; 41:48]现在我想将第4列的所有内容移动到行位置.我该如何实现这一目标?
A =
1 2 3 4 5 6 7 8
11 12 13 14 15 16 17 18
21 22 23 24 25 26 27 28
31 32 33 34 35 36 37 38
41 42 43 44 45 46 47 48
Run Code Online (Sandbox Code Playgroud)
至
A2 =
1 2 3 4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
5 6 7 8
15 16 …Run Code Online (Sandbox Code Playgroud) 我想重塑以下数组:
>>> test
array([ 11., 12., 13., 14., 21., 22., 23., 24., 31., 32., 33.,
34., 41., 42., 43., 44.])
Run Code Online (Sandbox Code Playgroud)
为了获得:
>>> test2
array([[ 11., 12., 21., 22.],
[ 13., 14., 23., 24.],
[ 31., 32., 41., 42.],
[ 33., 34., 43., 44.]])
Run Code Online (Sandbox Code Playgroud)
我尝试过"重塑"之类的东西
>>> test.reshape(4,4)
array([[ 11., 12., 13., 14.],
[ 21., 22., 23., 24.],
[ 31., 32., 33., 34.],
[ 41., 42., 43., 44.]])
Run Code Online (Sandbox Code Playgroud)
和
>>> test.reshape(2,2,2,2)
array([[[[ 11., 12.],
[ 13., 14.]],
[[ 25., 26.],
[ 27., …Run Code Online (Sandbox Code Playgroud) 我想将X,Y,Z numpy数组转换为(X * Z)* Y numpy数组。
代码(慢):
def rearrange(data):
samples,channels,t_insts=data.shape
append_data=np.empty([0,channels])
for sample in range(0,samples):
for t_inst in range(0,t_insts):
channel_data=data[sample,:,t_inst]
append_data=np.vstack((append_data,channel_data))
return append_data.shape
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更好的矢量化方法
我想创建一个3D NumPy数组,其序列号如下:
[[[11 27 43]
[12 28 44]
[13 29 45]
[14 30 46]]
[[15 31 47]
[16 32 48]
[17 33 49]
[18 34 50]]
[[19 35 51]
[20 36 52]
[21 37 53]
[22 38 54]]
[[23 39 55]
[24 40 56]
[25 41 57]
[26 42 58]]]
Run Code Online (Sandbox Code Playgroud)
我做到了这一点:A = np.arange(11, 59).reshape((4, 4, 3))但我得到了这个:
[[[11 12 13]
[14 15 16]
[17 18 19]
[20 21 22]]
[[23 24 25]
[26 27 28]
[29 30 31]
[32 …Run Code Online (Sandbox Code Playgroud) 我有输入数组:
np.random.seed(45)
a = np.random.randint(100,size=(5,21))
print (a)
[[75 30 3 32 95 61 85 35 68 15 65 14 53 57 72 87 46 8 53 12 34]
[24 12 17 68 30 56 14 36 31 86 36 57 61 79 17 6 42 11 8 49 77]
[75 63 42 54 16 24 95 63 98 22 27 32 16 75 58 60 54 96 70 32 16]
[59 92 55 88 5 81 93 79 67 55 …Run Code Online (Sandbox Code Playgroud) numpy ×8
python ×7
arrays ×5
reshape ×2
indices ×1
matlab ×1
matrix ×1
performance ×1
python-2.7 ×1
scikit-image ×1