Python 仅洗牌特定列”;垂直地;在多维数组中

Python 仅洗牌特定列”;垂直地;在多维数组中,python,arrays,numpy,multidimensional-array,shuffle,Python,Arrays,Numpy,Multidimensional Array,Shuffle,我有一个6列多维数组,如: [59, '591', '592', '593', '594', 1582823720], [9, '91', '92', '93', '94', 1582823745], [7, '71', '72', '73', '74', 1582823745], [61, '611', '612', '613', '614', 1582823752], [54, '541', '542', '543', '544', 1582823717], [24, '241', '242

我有一个6列多维数组,如:

[59, '591', '592', '593', '594', 1582823720],
[9, '91', '92', '93', '94', 1582823745],
[7, '71', '72', '73', '74', 1582823745],
[61, '611', '612', '613', '614', 1582823752],
[54, '541', '542', '543', '544', 1582823717],
[24, '241', '242', '243', '244', 1582823706]
有没有一种简单的方法可以只“垂直”移动特定的列,而保留其他列的内容不变

例如,假设我只需要“垂直”洗牌列2-5,而保持列1和列6不变,因此结果将是:

[59, '541', '242', '243', '74', 1582823720],
[9, '591', '542', '593', '94', 1582823745],
[7, '241', '612', '543', '614', 1582823745],
[61, '611', '92', '73', '544', 1582823752],
[54, '71', '72', '613', '594', 1582823717],
[24, '91', '592', '93', '244', 1582823706]
我是Python新手,也许有一个简单的内置解决方案或某个模块可以做到这一点


我遇到了
numpy
库,它使用
random.shuffle()
函数“垂直”洗牌整个数组行非常简单,也许有一个函数可以洗牌特定列?

下面是一个使用numpy的代码

data = [[59, '541', '242', '243', '74', 1582823720],
    [9, '591', '542', '593', '94', 1582823745],
    [7, '241', '612', '543', '614', 1582823745],
    [61, '611', '92', '73', '544', 1582823752],
    [54, '71', '72', '613', '594', 1582823717],
    [24, '91', '592', '93', '244', 1582823706]
]


import numpy as np
import random 


data_numpy = np.array(data)


def shuffle_column(matrix, col_index_to_shuffle):
  """
  """
  current_data = matrix[:, col_index_to_shuffle]
  random.shuffle(current_data)
  matrix[:, col_index_to_shuffle] = current_data
  return matrix


shuffled_matrix = shuffle_column(data_numpy, 2)
shuffled_matrix

array([['59', '541', '242', '243', '74', '1582823720'],
       ['9', '591', '92', '593', '94', '1582823745'],
       ['7', '241', '592', '543', '614', '1582823745'],
       ['61', '611', '612', '73', '544', '1582823752'],
       ['54', '71', '72', '613', '594', '1582823717'],
       ['24', '91', '542', '93', '244', '1582823706']], dtype='<U21')
data=[[59,541,242,243,74,1582823720],
[9, '591', '542', '593', '94', 1582823745],
[7, '241', '612', '543', '614', 1582823745],
[61, '611', '92', '73', '544', 1582823752],
[54, '71', '72', '613', '594', 1582823717],
[24, '91', '592', '93', '244', 1582823706]
]
将numpy作为np导入
随机输入
data\u numpy=np.array(数据)
def shuffle_列(矩阵、列索引到shuffle):
"""
"""
当前数据=矩阵[:,列索引到无序排列]
随机.shuffle(当前_数据)
矩阵[:,列索引到洗牌]=当前数据
返回矩阵
洗牌矩阵=洗牌列(数据,2)
混洗矩阵
数组([[59',541',242',243',74',1582823720'],
['9', '591', '92', '593', '94', '1582823745'],
['7', '241', '592', '543', '614', '1582823745'],
['61', '611', '612', '73', '544', '1582823752'],
['54', '71', '72', '613', '594', '1582823717'],

['24','91','542','93','244','1582823706']],dtype='您可以使用numpy shuffle函数执行此操作

x=np.array(yourlist)    
np.random.shuffle(x[:,1:5])
对于水平洗牌,可以使用转置

np.random.shuffle(x.T[:,1:5])
垂直洗牌示例

x = np.arange(36).reshape(6,6)
x
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])
np.random.shuffle(x[:,1:5])
x
array([[ 0,  7,  8,  9, 10,  5],
       [ 6,  1,  2,  3,  4, 11],
       [12, 19, 20, 21, 22, 17],
       [18, 25, 26, 27, 28, 23],
       [24, 13, 14, 15, 16, 29],
       [30, 31, 32, 33, 34, 35]])

我不确定其他库中是否存在这种功能,尽管我认为应该存在这种功能。但是,我不需要
numpy
来实现:

  • 变换数组的位置
  • 洗牌你需要的子阵
  • 将数组调回原处
洗牌第四列的代码示例如下:

import random
# I am using pprint to beautify the output on the terminal
from pprint import pprint
arr = [[59, '591', '592', '593', '594', 1582823720],
       [9, '91', '92', '93', '94', 1582823745],
       [7, '71', '72', '73', '74', 1582823745],
       [61, '611', '612', '613', '614', 1582823752],
       [54, '541', '542', '543', '544', 1582823717],
       [24, '241', '242', '243', '244', 1582823706]
      ]
t_arr = [*zip(*arr)]
# I am converting array elements to lists as the zip() function produce tuples instead of lists.
t_arr = [list(sub_arr) for sub_arr in t_arr]
random.shuffle(t_arr[3])
arr_b = [*zip(*t_arr)]
# Again, converting back to lists
arr_b = [list(sub_arr) for sub_arr in arr_b]
# printing out the results :)
pprint(arr_b)
以下是输出:

[[59, '591', '592', '73', '594', 1582823720],
 [9, '91', '92', '243', '94', 1582823745],
 [7, '71', '72', '543', '74', 1582823745],
 [61, '611', '612', '93', '614', 1582823752],
 [54, '541', '542', '613', '544', 1582823717],
 [24, '241', '242', '593', '244', 1582823706]]

numpy shuffle可以在适当的位置洗牌子阵列

如果您想让这4列保持水平一致性,只需这样做

data = np.array(data)
np.random.shuffle(data[1:5])
前面带有
np.random.seed(0)
,它给出

array([['59', '591', '592', '593', '594', '1582823720'],
       ['61', '611', '612', '613', '614', '1582823752'],
       ['54', '541', '542', '543', '544', '1582823717'],
       ['7', '71', '72', '73', '74', '1582823745'],
       ['9', '91', '92', '93', '94', '1582823745'],
       ['24', '241', '242', '243', '244', '1582823706']], dtype='<U11')
array([['59', '241', '92', '613', '244', '1582823720'],
       ['9', '71', '612', '243', '74', '1582823745'],
       ['7', '91', '542', '93', '614', '1582823745'],
       ['61', '611', '592', '73', '544', '1582823752'],
       ['54', '591', '72', '543', '94', '1582823717'],
       ['24', '541', '242', '593', '594', '1582823706']], dtype='<U11')
前面带有
np.random.seed(0)
,它给出

array([['59', '591', '592', '593', '594', '1582823720'],
       ['61', '611', '612', '613', '614', '1582823752'],
       ['54', '541', '542', '543', '544', '1582823717'],
       ['7', '71', '72', '73', '74', '1582823745'],
       ['9', '91', '92', '93', '94', '1582823745'],
       ['24', '241', '242', '243', '244', '1582823706']], dtype='<U11')
array([['59', '241', '92', '613', '244', '1582823720'],
       ['9', '71', '612', '243', '74', '1582823745'],
       ['7', '91', '542', '93', '614', '1582823745'],
       ['61', '611', '592', '73', '544', '1582823752'],
       ['54', '591', '72', '543', '94', '1582823717'],
       ['24', '541', '242', '593', '594', '1582823706']], dtype='<U11')
数组([[59',241',92',613',244',1582823720'],
['9', '71', '612', '243', '74', '1582823745'],
['7', '91', '542', '93', '614', '1582823745'],
['61', '611', '592', '73', '544', '1582823752'],
['54', '591', '72', '543', '94', '1582823717'],

[24',541',242',593',594',1582823706'],dtype='这似乎是将2-5列作为一个整行块进行洗牌,我需要按照示例输出中所示对每一列进行单独洗牌。不需要。它会单独洗牌每一列。检查我添加的示例以获得矢量化解决方案:
a[:,1:5]=沿\ u轴洗牌(a[:,1:5],axis=0)
from.@Divikar遇到了
TypeError:列表索引必须是整数或切片,而不是元组
错误,当尝试使用
shaffle\u沿轴
函数时…是的,我假设输入
a
是一个数组,正如你在问题中提到的
多维数组
。“要单独洗牌的列”我想要的是case,您的代码似乎很有效,但似乎有一些打字错误:第二行应该以
数据作为参数,而不是
tdata
,第三行应该是
np.random.shuffle
而不是
np.shuffle
作为第一个“水平一致性”在这种情况下,它似乎只在我将
数据[:,1:5]
作为shuffle参数时才起作用。