Python x=x[class_id]在NumPy阵列上使用时做什么

Python x=x[class_id]在NumPy阵列上使用时做什么,python,arrays,numpy,random,shuffle,Python,Arrays,Numpy,Random,Shuffle,我正在学习Python并解决一个机器学习问题 class_ids=np.arange(self.x.shape[0]) np.random.shuffle(class_ids) self.x=self.x[class_ids] 这是NumPy中的一个洗牌函数,但我不明白self.x=self.x[class\u id]的意思。因为我认为它将数组的值赋给了一个变量。假设self.x是一个numpy数组: class\u-ids是一个一维numpy数组,在表达式x[class\u-ids]中用作。

我正在学习Python并解决一个机器学习问题

class_ids=np.arange(self.x.shape[0])
np.random.shuffle(class_ids)
self.x=self.x[class_ids]

这是NumPy中的一个洗牌函数,但我不明白
self.x=self.x[class\u id]
的意思。因为我认为它将数组的值赋给了一个变量。

假设
self.x
是一个numpy数组:

class\u-ids
是一个一维numpy数组,在表达式
x[class\u-ids]
中用作。由于前一行已洗牌
class\u id
x[class\u id]
的计算结果为按行洗牌的
self.x

赋值
self.x=self.x[class\u id]
将无序数组赋值给
self.x
这是一种非常复杂的方式来无序排列
self.x
的第一维度。例如:

>>> x = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]])
>>> x
array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5]])
然后使用上述方法

>>> class_ids=np.arange(x.shape[0])  # create an array [0, 1, 2, 3, 4]
>>> np.random.shuffle(class_ids)     # shuffle the array
>>> x[class_ids]                     # use integer array indexing to shuffle x
array([[5, 5],
       [3, 3],
       [1, 1],
       [4, 4],
       [2, 2]])
请注意,由于docstring明确提到:

此函数仅沿多维数组的第一个轴洗牌数组。子数组的顺序已更改,但其内容保持不变

或使用:


在赋值语句前后打印
self.x
。它做什么?什么是
self.x
?你能显示它的初始化吗?代码的目的似乎是洗牌
self.x
,在这种情况下,我假设
np.random.shuffle(self.x)
就足够了上面的代码是加载omniglot数据集的基本步骤,并尝试对其进行洗牌以进行训练或测试,而self.x是那些形状像self.x=np.reformate(self.x,newshape=(1622,20,28,28,1))的图片的数据。@PaulRooney
>>> np.random.shuffle(x)
>>> x
array([[5, 5],
       [3, 3],
       [1, 1],
       [2, 2],
       [4, 4]])
>>> class_ids = np.random.permutation(x.shape[0])  # shuffle the first dimensions indices
>>> x[class_ids]
array([[2, 2],
       [4, 4],
       [3, 3],
       [5, 5],
       [1, 1]])