简体   繁体   English

从 Python 中其他数组的部分创建一个 Numpy 数组

[英]Create a Numpy array from parts of other arrays in Python

EDIT编辑

So I just understand now that Python and Numpy just don't do polymorphism very well.所以我现在才明白 Python 和 Numpy 的多态性做得不是很好。 Given the same data, it has to be put into the right form before most functions can use it.给定相同的数据,在大多数功能可以使用它之前,必须将其放入正确的形式。 So expecting Python to be able to 'one-line' something is beyond its capabilities.因此,期望 Python 能够“单行”处理某些事情超出了它的能力。

Just for comparison, MATLAB doesn't do polymorphism very well either but it's much less noticeable as by default it keeps all numeric data as a 2D array so the majority of functions will work with the majority of data - making it soooo much easier只是为了比较,MATLAB 的多态性也不是很好,但它不太引人注目,因为默认情况下它将所有数值数据保存为 2D 数组,因此大多数函数将处理大多数数据 - 使其变得更容易

EDIT编辑

I'm pretty new to Python and struggling to create new arrays out of existing arrays:我对 Python 还很陌生,正在努力从现有数组中创建新数组:

In MATLAB the following works to create a column vector from other column vectors:在 MATLAB 中,以下工作用于从其他列向量创建列向量:

a = [b(5:6); a = [b(5:6); c(N:M); c(N:M); d(1:P); d(1:P); e(Q)] e(Q)]

with a lot of computational flexibility (Q could be a vector for example).具有很大的计算灵活性(例如 Q 可以是一个向量)。

In Python, I can't find a nice command to add multiple 1D NumPy arrays together and it seems to have lots of issues with single values as it changes them from NumPy arrays to some other format, WHY?!在 Python 中,我找不到一个很好的命令来将多个 1D NumPy 数组添加在一起,而且它似乎有很多关于单个值的问题,因为它将它们从 NumPy 数组更改为其他格式,为什么?!

Can anyone give me a single line of code to carry out the above?谁能给我一行代码来执行上述操作? It'd be great to see - so far all I've got is lines and lines of checking for the indexing variables (N, M, P, Q) and soooo many np.array(..)'s everywhere to try and keep things the same data type.很高兴看到 - 到目前为止,我所拥有的只是检查索引变量(N,M,P,Q)的行和行,以及到处都可以尝试的 np.array(..)保持相同的数据类型。

I've tried np.append but that doesn't work for multiple vectors (I could nest them but that seems very ugly, esp if I need to add many arrays) and np.concatenate complains that something is 0-dimensional, I don't understand that at all.我试过 np.append 但这不适用于多个向量(我可以嵌套它们但看起来很丑,尤其是如果我需要添加许多数组)并且 np.concatenate 抱怨某些东西是 0 维的,我不完全不明白。

concatenate has no problems with a bunch of 1d array: concatenate一堆一维数组没有问题:

In [52]: np.concatenate([np.array([1,2,3]), np.ones((2,)), np.array([1])])
Out[52]: array([1., 2., 3., 1., 1., 1.])

If one argument is scalar:如果一个参数是标量:

In [53]: np.concatenate([np.array([1,2,3]), np.ones((2,)), 1])
Traceback (most recent call last):
  File "<ipython-input-53-51b00c09f677>", line 1, in <module>
    np.concatenate([np.array([1,2,3]), np.ones((2,)), 1])
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 2 has 0 dimension(s)

It make a array from the last它从最后一个数组

In [57]: np.array(1)
Out[57]: array(1)

but that's a 0d array.但那是一个 0d 数组。 In MATLAB that would be 2d - everything is 2d, there's not true scalars.在 MATLAB 中,这将是 2d - 一切都是 2d,没有真正的标量。 Remember, numpy works in python, which has scalars and lists.请记住, numpy在 python 中工作,它具有标量和列表。 MATLAB is matrices all the way down ... MATLAB 一直是矩阵......

Also numpy arrays can be 0d or 1d. numpy 数组也可以是 0d 或 1d。 There's no artificial 2d lower bound.没有人为的 2d 下界。 It's a general array language, not just matrices.它是一种通用的数组语言,而不仅仅是矩阵。 In MATLAB even 3d is a tweak on the original 2d.在 MATLAB 中,甚至 3d 也是对原始 2d 的调整。

hstack adds a tweak to make sure all arguments are at least 1d: hstack添加了一个调整以确保所有参数至少为 1d:

In [54]: np.hstack([np.array([1,2,3]), np.ones((2,)), 1])
Out[54]: array([1., 2., 3., 1., 1., 1.])

Even in MATLAB/Octave mismatched dimensions give problems:即使在 MATLAB/Octave 中,尺寸不匹配也会产生问题:

>> a = [3:5; [1,2,3]]
a =    
   3   4   5
   1   2   3

>> a = [3:5; [1,2,3]; 4]
error: vertical dimensions mismatch (2x3 vs 1x1)

>> a = [3:5; [1,2,3,4]]
error: vertical dimensions mismatch (1x3 vs 1x4)
>> a = [3:5, [1,2,3,4]]
a =    
   3   4   5   1   2   3   4

>> a = [3:5, [1,2,3,4],5]
a =
   3   4   5   1   2   3   4   5

你会想看看 np.concatenate 它采用数组和轴的序列来连接它们

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

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