根据特定条件选择性地调用 class 内的方法

selectively call methods within class based on certain condition

我想创建一个 class,它根据用户输入的内容应用某种 pandas 方法。

更具体地说,class 可以应用 pandas 方法 sub()、mul() 或 add(),其中将序列减去、乘以或添加到数据帧的不同变量。但是 class 应该只应用用户在 init 中指定的方法,最好是按照用户指定的顺序。

例如:

_PERMITTED_FUNCTIONS = ["add", "sub", "mul"]

class RelativeFeatures:

    def __init__(
        self,
        variables: List[Union[str, int]],
        reference: List[Union[str, int]],
        func: List[str] = _PERMITTED_FUNCTIONS,
    ) -> None:

        self.variables = variables
        self.reference = reference
        self.func = func

   def _sub(self, X):
        for reference in self.reference:
            varname = [
                str(var) + "_sub_" + str(reference)
                for var in self.variables
            ]
            X[varname] = X[self.variables].sub(X[reference], axis=0)
        return X

    def _add(self, X):
        for reference in self.reference:
            varname = [
                str(var) + "_add_" + str(reference)
                for var in self.variables
            ]
            X[varname] = X[self.variables].add(X[reference], axis=0)
        return X

    def _mul(self, X):
        for reference in self.reference:
            varname = [
                str(var) + "_mul_" + str(reference)
                for var in self.variables
            ]
            X[varname] = X[self.variables].mul(X[reference], axis=0)
        return X

   def transform(X):

      for method in self.func:
         # apply the method that matches the string in the list

因此,如果用户创建以下 class:

tr = RelativeFeatures(
   variables = ["var1", "var2]
   reference = ["var3],
   func = ["mul", "add"]
)

然后传一个dataframe X[["var1", "var2", "var3"]]给transform()方法,结果是

XX[["var1", "var2", "var3", "var1_mul_var3", "var2_mul_var3", "var1_add_var3", "var2_add_var3 "]]

有没有一种方法可以按指定顺序调用方法?

这些行的内容:

for function in self.func:
   apply corresponding method

谢谢!

Furas 的建议很好,谢谢!

我post这里的答案:

_PERMITTED_FUNCTIONS = ["add", "sub", "mul"]

class RelativeFeatures:

    def __init__(
        self,
        variables: List[Union[str, int]],
        reference: List[Union[str, int]],
        func: List[str] = _PERMITTED_FUNCTIONS,
    ) -> None:

        self.variables = variables
        self.reference = reference
        self.func = func

   def _sub(self, X):
        for reference in self.reference:
            varname = [
                str(var) + "_sub_" + str(reference)
                for var in self.variables
            ]
            X[varname] = X[self.variables].sub(X[reference], axis=0)
        return X

    def _add(self, X):
        for reference in self.reference:
            varname = [
                str(var) + "_add_" + str(reference)
                for var in self.variables
            ]
            X[varname] = X[self.variables].add(X[reference], axis=0)
        return X

    def _mul(self, X):
        for reference in self.reference:
            varname = [
                str(var) + "_mul_" + str(reference)
                for var in self.variables
            ]
            X[varname] = X[self.variables].mul(X[reference], axis=0)
        return X

   def transform(X):

       methods_dict = {
            "add": self._add,
            "mul": self._mul,
            "sub": self._sub,
       }

       for func in self.func:
            methods_dict[func](X)
       
        return X