itertools:获取操作(+*/)和列的组合

2024-04-27 03:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个数值数据框,我想执行加号、减号、乘法和amp;对所有列的组合进行除法

对于3和3以上的组合,最快的方法是什么

下面给出了2个组合的最小可再现示例

import numpy as np
import pandas as pd
from itertools import combinations
from itertools import permutations
from sklearn.datasets import load_boston 

# the dataset
X, y = load_boston(return_X_y=True)
X = pd.DataFrame(X)

combos2 = list(combinations(X.columns,2))
perm3 = list(permutations(X.columns,3))  # how would i do this with out typing out all the permutations
for i in combos2:
    X[f'{i[0]}_X_{i[1]}'] = X.iloc[:,i[0]]*X.iloc[:,i[1]]  # Multiply
    X[f'{i[0]}_+_{i[1]}'] = X.iloc[:,i[0]]+X.iloc[:,i[1]]  # Add
    X[f'{i[0]}_-_{i[1]}'] = X.iloc[:,i[0]]-X.iloc[:,i[1]]  # Subtract
    X[f'{i[0]}_/_{i[1]}'] = X.iloc[:,i[0]]/(X.iloc[:,i[1]]+1e-20)   # Divide

我正在考虑一种方法,将“operators+*-/添加到组合中,这样就可以用比手动键入所有组合更少的行来编写,但我不知道从哪里开始

我想要所有订单:即(a*b+c),(a*b-c),(a*b/c)等

理想情况下不留下重复的列,即(a+b+c)和(c+b+a)

例如,如果我有3列a b c,我想要一个新列(a*b+c)


Tags: columnsthe方法fromimportasloadboston
2条回答

这里有一个简单的解决方案,输出2&;所有列中的3列

  1. 组合列表
  2. 使用运算符包生成函数
  3. 用于循环组合
  4. 这可能有重复的列,因此会删除重复的列
from sklearn.datasets import load_boston 
from itertools import combinations
import operator as op 

X, y = load_boston(return_X_y=True)
X =  pd.DataFrame(X)

comb= list(combinations(X.columns,3))

def operations(x,a,b):
   if (x == '+'): 
      d =  op.add(a,b) 
   if (x == '-'): 
      d =  op.sub(a,b) 
   if (x == '*'): 
      d =  op.mul(a,b)     
   if (x == '/'): # divide by 0 error
      d =  op.truediv(a,(b + 1e-20)) 
   return d


for x in ['*','/','+','-']:
  for y in ['*','/','+','-']:
    for i in comb:
      a = X.iloc[:,i[0]].values
      b = X.iloc[:,i[1]].values
      c = X.iloc[:,i[2]].values
      d = operations(x,a,b)
      e = operations(y,d,c)
      X[f'{i[0]}{x}{i[1]}{y}{i[2]}'] = e
      X[f'{i[0]}{x}{i[1]}'] = d

X = X.loc[:,~X.columns.duplicated()]

我希望这将帮助您开始:

operators = ['-', '+', '*', '/']
operands = ['a', 'b', 'c']

# find out all possible combination of operators first. So if you have 3 operands, that would be all permutations of the operators, taken 2 at a time. Also append the same expression operator combinations to the list

from itertools import permutations
operator_combinations = list(permutations(operators, len(operands)-1))
operator_combinations.extend([op]*(len(operands)-1) for op in operators)

# create a list for each possible expression, appending it with an operand and then an operator and so on, finishing off with an operand.

exp = []
for symbols in operator_combinations:
    temp = []
    for o,s in zip(operands, symbols):
        temp.extend([o,s])
    temp.append(operands[-1])
    exp.append(temp)

for ans in exp:
    print(''.join(ans))

输出:

a-b+c
a-b*c
a-b/c
a+b-c
a+b*c
a+b/c
a*b-c
a*b+c
a*b/c
a/b-c
a/b+c
a/b*c
a-b-c
a+b+c
a*b*c
a/b/c

相关问题 更多 >