Python 如何复杂地定制xgboost(目标函数)

Python 如何复杂地定制xgboost(目标函数),python,xgboost,Python,Xgboost,我正在尝试实现一个自定义的目标函数。我知道怎么做。 但我的情况并不简单,而且在我看来,任何已经存在的方式都无法实现 这里有一些变量 R.shape => (7000, 400000) y.shape => (7000, None) p.shape => (400000, None) 正如我在上面写的,y是R的基本事实 我的最终目标是找到p,这样R*p=y 所以,当我在XGBoost中放置转置(R)时,我想输出p。在这种情况下,目标函数将是(y-y\u hat)**2(R*p=

我正在尝试实现一个自定义的目标函数。我知道怎么做。 但我的情况并不简单,而且在我看来,任何已经存在的方式都无法实现

这里有一些变量

R.shape => (7000, 400000)
y.shape => (7000, None)
p.shape => (400000, None)
正如我在上面写的,
y
R
的基本事实

我的最终目标是找到
p
,这样
R*p=y

所以,当我在XGBoost中放置
转置(R)
时,我想输出
p
。在这种情况下,目标函数将是
(y-y\u hat)**2(R*p=y\u hat)

我知道文件中定义的目标函数如下

import numpy as np
import xgboost as xgb
from typing import Tuple

def gradient(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
    '''Compute the gradient squared log error.'''
    y = dtrain.get_label()
    return (np.log1p(predt) - np.log1p(y)) / (predt + 1)

def hessian(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
    '''Compute the hessian for squared log error.'''
    y = dtrain.get_label()
    return ((-np.log1p(predt) + np.log1p(y) + 1) /
            np.power(predt + 1, 2))

def squared_log(predt: np.ndarray,
                dtrain: xgb.DMatrix) -> Tuple[np.ndarray, np.ndarray]:
    '''Squared Log Error objective. A simplified version for RMSLE used as
    objective function.
    '''
    predt[predt < -1] = -1 + 1e-6
    grad = gradient(predt, dtrain)
    hess = hessian(predt, dtrain)
    return grad, he's
像上面这样复杂地定制xgboost是不可能的吗

dtrain = xgb.DMatrix(transepose(R), label=y) # this is mismatch of the shapes

def gradient(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
    y = dtrain.get_label()
    new_predt = transepose(dtrain.data).dot(predt) # dtrain.data means R
    return (np.log1p(new_predt) - np.log1p(y)) / (new_predt + 1)

def hessian(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
    y = dtrain.get_label()
    new_predt = transepose(dtrain.data).dot(predt)  # dtrain.data means R
    return ((-np.log1p(new_predt) + np.log1p(y) + 1) /
            np.power(new_predt + 1, 2))

def squared_log(predt: np.ndarray,
                dtrain: xgb.DMatrix) -> Tuple[np.ndarray, np.ndarray]:
    new_predt = transepose(dtrain.data).dot(predt)  # dtrain.data means R
    new_predt[new_predt < -1] = -1 + 1e-6
    grad = gradient(new_predt, dtrain)
    hess = hessian(new_predt, dtrain)
    return grad, hess

xgb.train({'tree_method': 'hist', 'seed': 1994},  # any other tree method is fine.
           dtrain=dtrain,
           num_boost_round=10,
           obj=squared_log)