pandas - 在类中交换值
问题描述
请问如何在类中交换值?
如下表所示:
- - - - - - - - - 之前 - - - - - - - - - - - - - - - - - 之后 - - - - - - - - -
我想这样做是因为它是过度采样的数据。这是非常重复的,这会导致机器学习工具过度拟合。
解决方案
好的,试试这个:
# 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函数,它可能会在增加一些噪音方面取得一些成果(尽管不是通过执行离散交换)。
[编辑] 为了进行测试,请尝试使用刚性较小的数据集,这是一个更随机生成的数据集的示例。如果您想在数据集中强加一些顺序,只需编辑出您想用固定值替换的列。
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")
推荐阅读
- css - wp_is_mobile() 的替代品
- flutter - 关注用户部分在 Instagram 中是如何工作的?
- sql-server - 具有 Null 和一对一关系的 ERD
- ios - 关闭模态视图控制器在(iOS13.5 Xcode 11.5)中显示黑屏
- flask-sqlalchemy - 错误:flask_appbuilder.security.sqla.manager:将新用户添加到数据库时出错
- r - R - 从数据集中恢复的患者数量
- c# - 在 Visual Studio 中对 tsx 和 ts 文件中未使用的导入进行排序和删除
- python - 以 JSON 格式输出到 html 结果干净
- solr - 如何在 solr 中向现有文档添加新字段
- php - PHP允许一些HTML标签,在屏幕上写代码