更快捷地应用动态生成的函数

问题描述

我创建了一个类,该类依赖于某些配置为模型创建评估函数。它创建的函数如下:

class Model:
    def __init__(self,factors):
        self.attr_factors = factors

    def _get_evaluator(self):
        attr_factors = self.attr_factors
        def evaluator(row):
            return (np.prod([(math.pow(row[attr_factor["name"]],row[attr_factor["factor"]])) for attr_factor in attr_factors]))/math.exp(row.D_ij*row.DF)
        return evaluator

评估器将收到一个 pandas 行,并根据对象 attr_factors 中描述的配置执行计算。

我想要的是接收函数“评估器”,就好像它已经用 attr_factors 中的信息进行了硬编码。然而,这似乎不是结果。这是因为当我使用 inspect.getsource(evaluator) 时,输出显示了上面写的函数,而不是用 attr_factors 对象的值填充。

我可以使用 pandas apply 函数,但是当我使用 swifter 时,出现错误。我认为原因是 swifter 没有可用的 attr_factors 对象,但我可能走错了路。

熊猫适用:

input_matrix["AA_ij"] = input_matrix.apply(
            evaluator,axis=1)

更快捷的申请:

 input_matrix["AA_ij"] = input_matrix.swifter.apply(
            evaluator,axis=1)

异常:

[ERROR] SubprocessError: Exception occurred in preexec_fn.
Traceback (most recent call last):
  File "/var/task/fit_model/fit_model.py",line 36,in lambda_handler
    fitting_results = fit_model(model,input_matrix)
  File "/var/task/fit_model/fit_model.py",line 60,in fit_model
    result = dual_annealing(
  File "/opt/python/lib/python3.8/site-packages/scipy/optimize/_dual_annealing.py",line 631,in dual_annealing
    energy_state.reset(func_wrapper,rand_state,x0)
  File "/opt/python/lib/python3.8/site-packages/scipy/optimize/_dual_annealing.py",line 171,in reset
    self.current_energy = func_wrapper.fun(self.current_location)
  File "/opt/python/lib/python3.8/site-packages/scipy/optimize/_dual_annealing.py",line 373,in fun
    return self.func(x,*self.args)
  File "/var/task/common/models/attr_model.py",line 230,in model_evaluator
    input_matrix = matrix_processor_instance(parameters)
  File "/var/task/common/models/attr_model.py",line 186,in matrix_processor
    input_matrix["AA_ij"] = input_matrix.swifter.apply(
  File "/mnt/localmount/python/swifter/swifter.py",line 358,in apply
    return self._modin_apply(func,axis,raw,result_type,*args,**kwds)
  File "/mnt/localmount/python/swifter/swifter.py",line 259,in _modin_apply
    import modin.pandas as md
  File "/mnt/localmount/python/modin/pandas/__init__.py",line 182,in <module>
    execution_engine.subscribe(_update_engine)
  File "/mnt/localmount/python/modin/__init__.py",line 93,in subscribe
    callback(self)
  File "/mnt/localmount/python/modin/pandas/__init__.py",line 118,in _update_engine
    initialize_ray()
  File "/mnt/localmount/python/modin/engines/ray/utils.py",line 139,in initialize_ray
    ray.init(
  File "/mnt/localmount/python/ray/worker.py",line 696,in init
    _global_node = ray.node.Node(
  File "/mnt/localmount/python/ray/node.py",line 198,in __init__
    self.start_head_processes()
  File "/mnt/localmount/python/ray/node.py",line 800,in start_head_processes
    self.start_redis()
  File "/mnt/localmount/python/ray/node.py",line 568,in start_redis
    process_infos) = ray.services.start_redis(
  File "/mnt/localmount/python/ray/services.py",line 739,in start_redis
    port,p = _start_redis_instance(
  File "/mnt/localmount/python/ray/services.py",line 913,in _start_redis_instance
    process_info = start_ray_process(
  File "/mnt/localmount/python/ray/services.py",line 490,in start_ray_process
    process = ConsolePopen(
  File "/var/lang/lib/python3.8/subprocess.py",line 854,in __init__
    self._execute_child(args,executable,preexec_fn,close_fds,File "/var/lang/lib/python3.8/subprocess.py",line 1703,in _execute_child
    raise child_exception_type(err_msg)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)