import numpy as np
from {{import_library}} import {{ model_name }}

{% if model_arg == "HPO_noRandomSeed" %}
__model = {{ model_name }}(**best_params)
{% elif model_arg == "HPO_RandomSeed" %}
random_state_model = {{ pipeline.task.seed_for_model}}
__model = {{ model_name }}(random_state=random_state_model, **best_params)
{% elif model_arg == "noHPO_noRandomSeed" %}
__model = {{ model_name }}({{ params }})
{% elif model_arg == "noHPO_RandomSeed" %}
random_state_model = {{ pipeline.task.seed_for_model}}
__model = {{ model_name }}(random_state=random_state_model, {{ params }})
{% endif %}

{% if is_multioutput_regression%}
from sklearn.multioutput import MultiOutputRegressor

__model = MultiOutputRegressor(__model)
{% elif is_multioutput_classification %}
from sklearn.multioutput import MultiOutputClassifier

__model = MultiOutputClassifier(__model)
{% endif %}
{% set xgbclassifier = "XGBClassifier" %}
{% if model_name == xgbclassifier %}
from sklearn.preprocessing import LabelEncoder

_le = LabelEncoder()
__target_train = pd.DataFrame(_le.fit_transform(__target_train), columns=_TARGET_COLUMNS)
{% endif %}
{% if pipeline.task.target_columns|length == 1 %}
__model.fit(__feature_train, __target_train.values.ravel())
{% else %}
__model.fit(__feature_train, __target_train)
{% endif %}
__y_pred = __model.predict(__feature_test)
{% if flag_predict_proba and (not pipeline.adaptation_metric.startswith("MAP_")) and (not pipeline.adaptation_metric == "LogLoss") and (pipeline.adaptation_metric not in metric_needing_predict_proba) %}
__y_pred = __model.classes_[np.argmax(__y_pred, axis=1)].reshape(-1, 1)
{% endif %}
{% if model_name == xgbclassifier and (not pipeline.adaptation_metric.startswith("MAP_")) and (not pipeline.adaptation_metric == "LogLoss") and (pipeline.adaptation_metric not in metric_needing_predict_proba) %}
__y_pred = _le.inverse_transform(__y_pred).reshape(-1, 1)
{% endif %}