我有一个形状为(A,B)的数组,其中A=4,B=2:[[1 1] [2 2] [3 3] [4 4]]
我想修改数组,使每行由其他行中随机选择的值组成,同一行中没有重复。一个示例结果是:[[3 2] [1 3] [2 4] [3 1]]
我尝试使用np.random.shuffle和np.random.choice,但我不知道如何排除行本身,以及如何替换每个值而不是每行。np.random.shuffle导致:[[4 4] [2 2] [3 3] [1 1]]
和np.random.choice给我错误,因为我的数组是2D而不是1D
我是一个初学者,我觉得这应该是显而易见的,但我一直在绞尽脑汁一整天…任何帮助将非常感谢
2条答案
按热度按时间gc0ot86w1#
假设
A=N
矩阵的值总是从1到N,下面将生成一个随机矩阵,其中的值来自该集合,但没有一行包含该行的索引。字符串
生成
型
如果你想确保每一列都包含所有可能的值,但是同样没有值出现在值等于其从一开始的索引的行中。
型
示例
型
bqf10yzr2#
将
np.random.shuffle
分别应用于每列字符串
生成
型