在类中交换值

时间:2018-11-23 10:45:10

标签: pandas discrete-mathematics

如何在类之间交换值?

如下表所示:

-----------之前-----------------之后-----------

SWAP

我想这样做,因为它超出了采样数据。这是非常重复的操作,这会导致机器学习工具过度适应。

1 个答案:

答案 0 :(得分:1)

好,尝试一下:

# Setup example dataframe
df = pd.DataFrame({"Class" : [1,2,1,3,1,2,1,3,1,2,1,3,1,2,1,3], 
                1:[1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1], 
                2:[0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0],
                3:[0,0,1,1,1,0,1,1,0,0,1,1,1,0,1,1],
                4:[1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1], 
                5:[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
                6:[0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1]}).set_index("Class")
# Do a filter on class, and store the positions/index of matching contents
class_to_edit=3
swappable_indices = np.where(df.index==class_to_edit)[0]
# Extract the column to edit
column_to_edit=1
column_values = df[column_to_edit].values

# Decide how many values to swap, and randomly assign swaps
# No guarantee here that the swaps will not contain the same values i.e. you could
# end up swapping 1's for 1's and 0's for 0's here - it's entirely random. 
number_of_swaps = 2
swap_pairs = np.random.choice(swappable_indices,number_of_swaps*2, replace=False)

# Using the swap pairs, build a map of substitutions, 
# starting with a vanilla no-swap map, then updating it with the generated swaps
swap_map={e:e for e in range(0,len(column_values))}
swap_map.update({swappable_indices[e]:swappable_indices[e+1] for e in range(0,len(swap_pairs),2)})
swap_map.update({swappable_indices[e+1]:swappable_indices[e] for e in range(0,len(swap_pairs),2)})

# Having built the swap-map, apply it to the data in the column, 
column_values=[column_values[swap_map[e]] for e,v in enumerate(column_values)]
# and then plug the column back into the dataframe
df[column_to_edit]=column_values

这有点肮脏,而且我敢肯定有一种更干净的方法可以在单行列表理解中构建替换映射-但这应该可以解决问题。

或者,还有一个np.permute函数可能会在增加一些噪音(尽管不是通过执行离散交换)方面取得一些成果。

[edit]为了进行测试,请尝试使用刚度稍低的数据集,这是一个随机生成的数据集的示例。如果要在数据集中强加一些顺序,只需编辑出要用固定值替换的列。

df = pd.DataFrame({"Class" : [1,2,1,3,1,2,1,3,1,2,1,3,1,2,1,3], 
            1:np.random.choice([0,1],16), 
            2:np.random.choice([0,1],16),
            3:np.random.choice([0,1],16),
            4:np.random.choice([0,1],16), 
            5:np.random.choice([0,1],16),
            6:np.random.choice([0,1],16)}).set_index("Class")