【问题标题】:Combinations of Columns based on a condition基于条件的列组合
【发布时间】:2019-11-21 23:48:31
【问题描述】:

我有 370 列,我必须生成 2、3 和 4 列的独特组合。但是,我需要在组合上应用这些条件(首先参见下表和我需要在下面应用的条件)

My columns are named in this fashion 
Column 1 Name :   'Real'
Column 2 Name :   'Real_lag1'
Column 3 Name :   'Real_lag2'
Column 4 Name :   'Real_lag3'
Column 5 Name :   'Real_lag4'
Column 6 Name :   'Normal'
Column 7 Name :   'Normal_chng'
Column 9 Name :   'Normal_diff'
Column 10 Name :  'Andi_diff'
Column 11 Name :  'Vari_lag1'
Column 12 Name :  'Teo_diff'
Column 13 Name :  'Lan_diff'
.
.
.
. 

基本上我有 24 个唯一值,这些值已经滞后,总共转换为 370 个变量(如上所示)等。例如,您可以看到 _lag _chng、_diff 都是 24 个唯一列的转换(如“Real” ,'Normal', 'Andi', 'Vari', 'Teo, 'Lan' 等等)

我想生成这 370 个变量的唯一组合,但是只有一个变量可以来自父变量。

例如,“Real”和“Real_lag1”不能一起出现在组合中。

如果尝试运行所有组合(使用 370),总共有 776,741,925 个组合(一次 2 个,一次 3 个,一次 4 个)并对此应用条件将花费大量时间运行这个

【问题讨论】:

  • 这看起来像是数学和编码问题的结合。要生成可能的组合,您可能需要查看itertools.combinations。基于 24 个独特的组,组合 2/3/4 个元素。
  • 是的。我认为这样做的一种方法是将 370 个变量分组到父变量下),然后从每个父变量中选择一个。但是我该怎么做呢?
  • 两位家长:parents2 = list(itertools.combinations(np.arange(24),2))。这将创建一个父组合列表。相应地适应 3/4 的父母。然后对于每对/三/四的父母,检查每组中的所有组合。 itertools 是你的朋友。

标签: python python-3.x pandas jupyter-notebook


【解决方案1】:

假设您的数据位于名为df的数据框中
1. 获取所有列名的列表

col_names = df.columns

2。将父母与孩子的名字分开(我非常愿意接受更好看的代码的建议!)

tmp = [col.split('_') for col in col_names]

parent_child = {}
for col in tmp:
    if len(col)>1:
        if col[0] not in parent_child.keys():
            parent_child[col[0]] = [col[0]]
        if (col[0] in parent_child.keys()) and (col[1] not in parent_child.values()):
            parent_child[col[0]].append(col[1])
    else:
        parent_child[col[0]] = [col[0]]

>> parent_child
{'Andi': ['Andi', 'diff'],
 'Lan': ['Lan', 'diff'],
 'Normal': ['Normal', 'chng', 'diff'],
 'Real': ['Real', 'lag1', 'lag2', 'lag3', 'lag4'],
 'Teo': ['Teo', 'diff'],
 'Vari': ['Vari', 'lag1']}

根据您的示例,我将父名称添加到子列表中,因为独立的父名称也是一个选项。
3.获取2个父母的所有组合(针对3或4个父母进行相应调整)

comb2 = list(itertools.combinations(list(parent_child.keys()),2)
comb2 
('Real', 'Normal')
('Real', 'Andi')
('Real', 'Vari')
...
('Andi', 'Lan')
('Vari', 'Teo')
('Vari', 'Lan')
('Teo', 'Lan')
  1. 生成所有父母组合:
combinations = []
for p in comb2:
    for v1 in parent_child[p[0]]:
        for v2 in parent_child[p[1]]:
            print(v1, p[0], v2, p[1])
            if v1 == p[0]:
                name1 = p[0]
            else:
                name1 = str(p[0]+'_'+v1)
            if v2 == p[1]:
                name2 = p[1]
            else:
                name2 = str(p[1]+'_'+v2)
            combinations.append((name1,name2))
>> combinations
[('Real', 'Normal'),
 ('Real', 'Normal_chng'),
 ('Real', 'Normal_diff'),
 ('Real_lag1', 'Normal'),
 ('Real_lag1', 'Normal_chng'),
 ('Real_lag1', 'Normal_diff'),
 ('Real_lag2', 'Normal'),
 ('Real_lag2', 'Normal_chng'),
 ('Real_lag2', 'Normal_diff'),
 ('Real_lag3', 'Normal'),
...
('Vari', 'Lan'),
 ('Vari', 'Lan_diff'),
 ('Vari_lag1', 'Lan'),
 ('Vari_lag1', 'Lan_diff'),
 ('Teo', 'Lan'),
 ('Teo', 'Lan_diff'),
 ('Teo_diff', 'Lan'),
 ('Teo_diff', 'Lan_diff')]

【讨论】:

  • 这里的 Parent2 是什么?不认为它是在这里定义的吗?是comb2
  • 谢谢,它可以工作,但是如果我将 2 个变量的组合放在一个数据框中,运行时间仍然相对较高。任何减少运行时间的建议
  • 我这样做的重点是对这 370 个变量运行多个多元回归。你认为像这样运行组合并从这些组合的运行回归中有效吗
  • 最慢的部分可能是组合的计算。一般来说,大量的组合会使它变得相当慢。如果您只检查 2 个变量的组合数量,您会发现它非常庞大。恐怕您无法加快速度,因为这不是凸搜索问题。任何(数学)优化方面的专家都可能会提供更好的帮助。也许看看Operations ResearchComputational Science
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多