简体   繁体   English

Python Numpy初学者:获取形状为((M,N),(M,N))的数组

[英]Python Numpy beginner: getting an array with a shape ((M,N),(M,N))

As input for a code, I need a 'ndarray' (call it C), whose shape is: ((4,N),(4,N)). 作为代码的输入,我需要一个'ndarray'(称为C),其形状为:((4,N),(4,N))。 So, if N=3, I thought that I can construct it in this way: 因此,如果N = 3,我以为可以这样构造它:

import numpy as np
A=np.array([[1, 2,3], [0.1, 0.2,0.3],[0.4,0.5,0.6],[0.7,0.8,0.9]])
B=np.array([[4,5,6], [0.4, 0.5,0.6],[0.7,0.8,0.9],[0.7,0.8,1]])

Therefore, how can I combine A and B to get C, whose shape is ((4,3),(4,3)) (st C[0] should also be A, and C[1] should be B)? 因此,如何组合A和B来得到形状为((4,3),(4,3))的C(st C [0]也应为A,C [1]应为B)?

I tried: 我试过了:

C=np.concatenate(([A], [B]), axis=0)

, but resulting C.shape was (2, 4, 3) instead of ((4,3),(4,3)). ,但是得到的C.shape是(2,4,3)而不是((4,3),(4,3))。 I naively tried then to reshape C: 然后我天真地尝试重塑C:

C.reshape((4,3),(4,3)) 

but then I get an error message. 但是然后我收到一条错误消息。

I am sorry if the question is too basic, I have just started using python. 如果问题太基本了,我感到抱歉,我刚刚开始使用python。

Best, 最好,

Steven 史蒂芬

In order to do what you're asking, it is simpler if you understand how the numpy ndarrays work. 为了执行您要的操作,如果您了解numpy ndarrays的工作原理,则更为简单。 In the example you are listing, you can make an array of exactly that type in 3 dimensions with the following shape: 在您列出的示例中,您可以使用以下形状在3维中精确地创建该类型的数组:

numpy.zeros((4,N,2));

This would create an array with the shape you're asking for. 这将创建一个具有您想要的形状的数组。 The documentation page on numpy array creation has a lot of great information on how to use it. 关于numpy数组创建文档页面上有很多有关如何使用它的重要信息。

However, if you want to merge two arrays, there are many ways to do it. 但是,如果要合并两个数组,有很多方法可以做到。

stacked0=numpy.stack(A,B)                # Stack the arrays along a new axis (a 3rd axis in this case)
stacked0.shape                           # outputs (2,4,3) in the example arrays.      

This created a new axis and merged along it, in this case, the default replaces the first axis (axis=0), and shifts the remaining axes down. 这将创建一个新轴并沿其合并,在这种情况下,默认轴替换第一个轴(axis = 0),并将其余轴向下移动。 This only matters for when you need to index it later and some persnickety performance in advanced applications. 这仅在以后需要索引以及高级应用程序中某些固定性能时才重要。 The most important thing as a beginner is to understand which axis you want to merge along. 作为初学者,最重要的事情是了解要合并的轴。

stacked1=numpy.stack([A,B],axis=1)    # Replaces the second axis to stack
stacked1.shape                        # (4,2,3)
stacked2=numpy.stack([A,B],axis=2)    # Appends a third axis
stacked2.shape                        # (4,3,2)

You can also concatenate them along existing axes. 您也可以沿现有轴将它们串联起来。

concated0 = numpy.concatenate([A,B],axis=0) # merges them along the first axis
concated0.shape                  # (8,3)
concated1 = numpy.concatenate([A,B],axis=1)
concated1.shape                  # (4,6)

See the array manimpulations docs for more options on how to rearrange your arrays. 有关如何重新排列阵列的更多选项,请参见阵列操作文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM