# Buggy code
def execute(cls, ctx, op: "LGBMTrain"):
    if op.merge:
        return super().execute(ctx, op)

    from lightgbm.basic import _safe_call, _LIB

    data_val = ctx[op.data.key]
    label_val = ctx[op.label.key]
    sample_weight_val = (
        ctx[op.sample_weight.key] if op.sample_weight is not None else None
    )
    init_score_val = ctx[op.init_score.key] if op.init_score is not None else None

    if op.eval_datas is None:
        eval_set, eval_sample_weight, eval_init_score = None, None, None
    else:
        eval_set, eval_sample_weight, eval_init_score = [], [], []
        for data, label in zip(op.eval_datas, op.eval_labels):
            eval_set.append((ctx[data.key], ctx[label.key]))
        for weight in op.eval_sample_weights:
            eval_sample_weight.append(ctx[weight.key] if weight is not None else None)
        for score in op.eval_init_scores:
            eval_init_score.append(ctx[score.key] if score is not None else None)

        eval_set = eval_set or None
        eval_sample_weight = eval_sample_weight or None
        eval_init_score = eval_init_score or None

    params = op.params.copy()
    if ctx.running_mode == RunningMode.distributed:
        params["machines"] = ",".join(op.lgbm_endpoints)
        params["time_out"] = op.timeout
        params["num_machines"] = len(op.lgbm_endpoints)
        params["local_listen_port"] = op.lgbm_port

        if (op.tree_learner or "").lower() not in {"data", "feature", "voting"}:
            logger.warning(
                "Parameter tree_learner not set or set to incorrect value "
                f'{op.tree_learner}, using "data" as default'
            )
            params["tree_learner"] = "data"
        else:
            params["tree_learner"] = op.tree_learner

    try:
        model_cls = get_model_cls_from_type(op.model_type)
        model = model_cls(**params)
        model.fit(
            data_val,
            label_val,
            sample_weight=sample_weight_val,
            init_score=init_score_val,
            eval_set=eval_set,
            eval_sample_weight=eval_sample_weight,
            eval_init_score=eval_init_score,
            **op.kwds,
        )

        if (
            op.model_type == LGBMModelType.RANKER
            or op.model_type == LGBMModelType.REGRESSOR
        ):
            model.set_params(out_dtype_=np.dtype("float"))
        elif hasattr(label_val, "dtype"):
            model.set_params(out_dtype_=label_val.dtype)
        else:
            model.set_params(out_dtype_=label_val.dtypes[0])

        ctx[op.outputs[0].key] = pickle.dumps(model)
    finally:
        _safe_call(_LIB.LGBM_NetworkFree())


# Traceback (TypeError)
Attempt 4: Unexpected error TypeError occurred in executing operand affdad0be8e3430b7b6088cd112ed634 in 10.xxx:8083
Traceback (most recent call last):
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/promise.py", line 100, in _wrapped
result = func(*args, **kwargs)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/worker/calc.py", line 299, in <lambda>
.then(lambda context_dict: _start_calc(context_dict)) \
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/worker/calc.py", line 273, in _start_calc
return self._calc_results(session_id, graph_key, graph, context_dict, chunk_targets)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/utils.py", line 365, in _wrapped
return func(*args, **kwargs)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/worker/calc.py", line 197, in _calc_results
chunk_targets, retval=False).result()
File "src/gevent/event.py", line 383, in gevent._gevent_cevent.AsyncResult.result
File "src/gevent/event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src/gevent/event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src/gevent/event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src/gevent/event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/gevent/_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/gevent/threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "mars/actors/pool/gevent_pool.pyx", line 127, in mars.actors.pool.gevent_pool.GeventThreadPool._wrap_watch.inner
result = fn(*args, **kwargs)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/executor.py", line 690, in execute_graph
res = graph_execution.execute(retval)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/executor.py", line 571, in execute
future.result()
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/concurrent/futures/_base.py", line 435, in result
return self.__get_result()
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/utils.py", line 439, in _inner
return func(*args, **kwargs)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/executor.py", line 443, in _execute_operand
Executor.handle(first_op, results, self._mock)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/executor.py", line 641, in handle
return runner(results, op)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/mars/learn/contrib/lightgbm/train.py", line 298, in execute
eval_init_score=eval_init_score, **op.kwds)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/sklearn.py", line 760, in fit
callbacks=callbacks, init_model=init_model)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/sklearn.py", line 600, in fit
callbacks=callbacks, init_model=init_model)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/engine.py", line 231, in train
booster = Booster(params=params, train_set=train_set)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/basic.py", line 1983, in __init__
train_set.construct()
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/basic.py", line 1325, in construct
categorical_feature=self.categorical_feature, params=self.params)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/basic.py", line 1102, in _lazy_init
params_str = param_dict_to_str(params)
File "/data/platform/anaconda3/envs/mars-dev/lib/python3.7/site-packages/lightgbm/basic.py", line 156, in param_dict_to_str
% (key, type(val).__name__))
TypeError: Unknown type of parameter:out_dtype_, got:dtype

# Fixed code
def execute(cls, ctx, op: "LGBMTrain"):
    if op.merge:
        return super().execute(ctx, op)

    from lightgbm.basic import _safe_call, _LIB

    data_val = ctx[op.data.key]
    label_val = ctx[op.label.key]
    sample_weight_val = (
        ctx[op.sample_weight.key] if op.sample_weight is not None else None
    )
    init_score_val = ctx[op.init_score.key] if op.init_score is not None else None

    if op.eval_datas is None:
        eval_set, eval_sample_weight, eval_init_score = None, None, None
    else:
        eval_set, eval_sample_weight, eval_init_score = [], [], []
        for data, label in zip(op.eval_datas, op.eval_labels):
            eval_set.append((ctx[data.key], ctx[label.key]))
        for weight in op.eval_sample_weights:
            eval_sample_weight.append(ctx[weight.key] if weight is not None else None)
        for score in op.eval_init_scores:
            eval_init_score.append(ctx[score.key] if score is not None else None)

        eval_set = eval_set or None
        eval_sample_weight = eval_sample_weight or None
        eval_init_score = eval_init_score or None

    params = op.params.copy()
    # if model is trained, remove unsupported parameters
    params.pop("out_dtype_", None)
    if ctx.running_mode == RunningMode.distributed:
        params["machines"] = ",".join(op.lgbm_endpoints)
        params["time_out"] = op.timeout
        params["num_machines"] = len(op.lgbm_endpoints)
        params["local_listen_port"] = op.lgbm_port

        if (op.tree_learner or "").lower() not in {"data", "feature", "voting"}:
            logger.warning(
                "Parameter tree_learner not set or set to incorrect value "
                f'{op.tree_learner}, using "data" as default'
            )
            params["tree_learner"] = "data"
        else:
            params["tree_learner"] = op.tree_learner

    try:
        model_cls = get_model_cls_from_type(op.model_type)
        model = model_cls(**params)
        model.fit(
            data_val,
            label_val,
            sample_weight=sample_weight_val,
            init_score=init_score_val,
            eval_set=eval_set,
            eval_sample_weight=eval_sample_weight,
            eval_init_score=eval_init_score,
            **op.kwds,
        )

        if (
            op.model_type == LGBMModelType.RANKER
            or op.model_type == LGBMModelType.REGRESSOR
        ):
            model.set_params(out_dtype_=np.dtype("float"))
        elif hasattr(label_val, "dtype"):
            model.set_params(out_dtype_=label_val.dtype)
        else:
            model.set_params(out_dtype_=label_val.dtypes[0])

        ctx[op.outputs[0].key] = pickle.dumps(model)
    finally:
        _safe_call(_LIB.LGBM_NetworkFree())