简体   繁体   English

numpy:np_array [index_order_array]更改数组中元素的顺序吗?

[英]Numpy: np_array[index_order_array] changes the order of elements in an array?

I'm new to Python and Numpy 我是Python和Numpy的新手

Why array = [1,2,3,4] and new_array = array[[3,2,0,1]] results in changing the order of elements as mentioned in the inner array? 为什么array = [1,2,3,4]new_array = array[[3,2,0,1]]导致更改内部数组中提到的元素顺序?

import numpy as np

array = np.array([10,20,30,40,50])
array_link = np.array(['A','B','C','D','E'])

new_array = np.ndarray(5, dtype=np.int32)
new_array_link = np.ndarray(5, dtype=np.int32)

perm = np.random.permutation(array.shape[0])

new_array = array[perm]
new_array_link = array_link[perm]

print(new_array)
print(new_array_link)

# Output:
# [30 40 10 50 20]
# ['C' 'D' 'A' 'E' 'B']

Here is the Playground 这是游乐场

Is this how it is supposed to work? 这是应该如何工作的吗? Shouldn't it be initializing a new (perhaps 2D) array with the elements of inner array (as the first row)? 它不应该使用内部数组的元素(作为第一行)初始化一个新的(也许是2D)数组吗?

The first of these 2 lines is useless. 这2行中的第一行是无用的。 python does not require that you initialize or 'pre-define' a variable. python不需要您初始化或“预定义”变量。 The first creates an array; 第一个创建数组;第二个创建数组。 the second also creates one, and reassigns the variable. 第二个也创建一个,然后重新分配变量。 The original value of new_array is discarded. new_array的原始值将被丢弃。

new_array = np.ndarray(5, dtype=np.int32)
...
new_array = array[perm]

And as a general rule, np.ndarray is only used for advanced purposes. 通常, np.ndarray仅用于高级用途。 np.array , np.zeros etc are used to create new arrays. np.arraynp.zeros等用于创建新数组。

array is a poor choice of variable name. array是变量名的不佳选择。 array looks too much like np.array , and actually confused me when I first copied the above lines. array看起来太像np.array ,当我第一次复制以上np.array ,实际上使我感到困惑。

array = np.array([10,20,30,40,50])

In sum your code does: 总之,您的代码可以:

In [28]: arr = np.array([10,20,30,40,50])
In [29]: perm = np.random.permutation(arr.shape[0])
In [30]: perm
Out[30]: array([2, 0, 1, 4, 3])
In [31]: arr1 = arr[perm]
In [32]: arr1
Out[32]: array([30, 10, 20, 50, 40])

arr1 is a new array with values selected from arr . arr1是一个新数组,其值选自arr arr itself is unchanged. arr本身不变。

You could assign values to predefined array this way: 您可以通过以下方式将值分配给预定义的数组:

In [35]: arr2 = np.zeros(5, int)
In [36]: arr2
Out[36]: array([0, 0, 0, 0, 0])
In [37]: arr2[:] = arr[perm]
In [38]: arr2
Out[38]: array([30, 10, 20, 50, 40])

In arr[perm] , the result is the same shape as perm , in this case a 5 element 1d array. arr[perm] ,结果与perm形状相同,在本例中为5个元素的1d数组。 If I turn perm into a (5,1) column array, the result is also a (5,1) array: 如果我将perm变成(5,1)列数组,结果也是(5,1)数组:

In [40]: arr[perm[:,None]]
Out[40]: 
array([[30],
       [10],
       [20],
       [50],
       [40]])
In [41]: _.shape
Out[41]: (5, 1)

Another example of array indexing - with a (2,2) array: 数组索引的另一个示例-具有(2,2)数组:

In [43]: arr[np.array([[0,1],[2,3]])]
Out[43]: 
array([[10, 20],
       [30, 40]])

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

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