- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试设置 XGBoost sklearn API XGBClassifier
根据文档使用自定义目标函数 (brier
):
.. note:: Custom objective function
A custom objective function can be provided for the ``objective``
parameter. In this case, it should have the signature
``objective(y_true, y_pred) -> grad, hess``:
y_true: array_like of shape [n_samples]
The target values
y_pred: array_like of shape [n_samples]
The predicted values
grad: array_like of shape [n_samples]
The value of the gradient for each sample point.
hess: array_like of shape [n_samples]
The value of the second derivative for each sample point
import numpy as np
from xgboost import XGBClassifier
from sklearn.datasets import load_svmlight_file
train_data = load_svmlight_file('~/agaricus.txt.train')
X = train_data[0].toarray()
y = train_data[1]
def brier(y_true, y_pred):
y_pred = 1.0 / (1.0 + np.exp(-y_pred))
grad = 2 * y_pred * (y_true - y_pred) * (y_pred - 1)
hess = 2 * y_pred ** (1 - y_pred) * (2 * y_pred * (y_true + 1) - y_true - 3 * y_pred ** 2)
return grad, hess
m = XGBClassifier(objective=brier, seed=42)
XGBClassifier(base_score=None, booster=None, colsample_bylevel=None,
colsample_bynode=None, colsample_bytree=None, gamma=None,
gpu_id=None, importance_type='gain', interaction_constraints=None,
learning_rate=None, max_delta_step=None, max_depth=None,
min_child_weight=None, missing=nan, monotone_constraints=None,
n_estimators=100, n_jobs=None, num_parallel_tree=None,
objective=<function brier at 0x7fe7ac418290>, random_state=None,
reg_alpha=None, reg_lambda=None, scale_pos_weight=None, seed=42,
subsample=None, tree_method=None, validate_parameters=False,
verbosity=None)
.fit
方法似乎重置
m
反对默认设置:
m.fit(X, y)
m
XGBClassifier(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.300000012, max_delta_step=0, max_depth=6,
min_child_weight=1, missing=nan, monotone_constraints=None,
n_estimators=100, n_jobs=0, num_parallel_tree=1,
objective='binary:logistic', random_state=42, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, seed=42, subsample=1,
tree_method=None, validate_parameters=False, verbosity=None)
objective='binary:logistic'
.我注意到,在调查为什么直接针对
brier
进行优化时,我的 brier 分数会变得更差。比我使用默认
binary:logistic
时,如
here 所述.
XGBClassifier
使用我的功能
brier
作为自定义目标?
最佳答案
我相信您将目标误认为是目标函数(obj 作为参数),xgboost 文档有时会很困惑。
简而言之,您只需要解决这个问题:
m = XGBClassifier(obj=brier, seed=42)
class XGBClassifier(XGBModel, XGBClassifierBase):
def __init__(self, objective="binary:logistic", **kwargs):
super().__init__(objective=objective, **kwargs)
def fit(self, X, y, sample_weight=None, base_margin=None,
eval_set=None, eval_metric=None,
early_stopping_rounds=None, verbose=True, xgb_model=None,
sample_weight_eval_set=None, callbacks=None):
evals_result = {}
self.classes_ = np.unique(y)
self.n_classes_ = len(self.classes_)
xgb_options = self.get_xgb_params() # <-- obj function is set here
if callable(self.objective):
obj = _objective_decorator(self.objective) # <----- here is the mismatch of the names, if you pass objective as your brie func it will become "binary:logistic"
xgb_options["objective"] = "binary:logistic"
else:
obj = None
if self.n_classes_ > 2:
xgb_options['objective'] = 'multi:softprob' # <----- objective is being set here if n_classes> 2
xgb_options['num_class'] = self.n_classes_
+-- 35 lines: feval = eval_metric if callable(eval_metric) else None-----------------------------------------------------------------------------------------------------------------------------------------------------
self._Booster = train(xgb_options, train_dmatrix, # <----- objective is being passed in xgb_options dictionary
self.get_num_boosting_rounds(),
evals=evals,
early_stopping_rounds=early_stopping_rounds,
evals_result=evals_result, obj=obj, feval=feval, # <----- obj function is being passed to lower level api here
verbose_eval=verbose, xgb_model=xgb_model,
callbacks=callbacks)
+-- 12 lines: self.objective = xgb_options["objective"]------------------------------------------------------------------------------------------------------------------------------------------------------------------
return self
reg:squarederror: regression with squared loss.
reg:squaredlogerror: regression with squared log loss 12[𝑙𝑜𝑔(𝑝𝑟𝑒𝑑+1)−𝑙𝑜𝑔(𝑙𝑎𝑏𝑒𝑙+1)]2. All input labels are required to be greater than -1. Also, see metric rmsle for possible issue with this objective.
reg:logistic: logistic regression
binary:logistic: logistic regression for binary classification, output probability
binary:logitraw: logistic regression for binary classification, output score before logistic transformation
binary:hinge: hinge loss for binary classification. This makes predictions of 0 or 1, rather than producing probabilities.
count:poisson –poisson regression for count data, output mean of poisson distribution
max_delta_step is set to 0.7 by default in poisson regression (used to safeguard optimization)
survival:cox: Cox regression for right censored survival time data (negative values are considered right censored). Note that predictions are returned on the hazard ratio scale (i.e., as HR = exp(marginal_prediction) in the proportional hazard function h(t) = h0(t) * HR).
multi:softmax: set XGBoost to do multiclass classification using the softmax objective, you also need to set num_class(number of classes)
multi:softprob: same as softmax, but output a vector of ndata * nclass, which can be further reshaped to ndata * nclass matrix. The result contains predicted probability of each data point belonging to each class.
rank:pairwise: Use LambdaMART to perform pairwise ranking where the pairwise loss is minimized
rank:ndcg: Use LambdaMART to perform list-wise ranking where Normalized Discounted Cumulative Gain (NDCG) is maximized
rank:map: Use LambdaMART to perform list-wise ranking where Mean Average Precision (MAP) is maximized
reg:gamma: gamma regression with log-link. Output is a mean of gamma distribution. It might be useful, e.g., for modeling insurance claims severity, or for any outcome that might be gamma-distributed.
reg:tweedie: Tweedie regression with log-link. It might be useful, e.g., for modeling total loss in insurance, or for any outcome that might be Tweedie-distributed.
class XGBClassifier(XGBModel, XGBClassifierBase):
def __init__(self, objective="binary:logistic", **kwargs):
super().__init__(objective=objective, **kwargs)
def fit(self, X, y, sample_weight=None, base_margin=None,
eval_set=None, eval_metric=None,
early_stopping_rounds=None, verbose=True, xgb_model=None,
sample_weight_eval_set=None, callbacks=None):
+-- 54 lines: evals_result = {}--------------------------------------------------------------------
xgb_options["objective"] = xgb_options["obj"]
self._Booster = train(xgb_options, train_dmatrix,
self.get_num_boosting_rounds(),
evals=evals,
early_stopping_rounds=early_stopping_rounds,
evals_result=evals_result, obj=obj, feval=feval,
verbose_eval=verbose, xgb_model=xgb_model,
callbacks=callbacks)
+-- 14 lines: self.objective = xgb_options["objective"]--------------------------------------------
raise XGBoostError(py_str(_LIB.XGBGetLastError()))
xgboost.core.XGBoostError: [10:09:53] /private/var/folders/z5/mchb9bz51cx3h97nkw9v0wkr0000gn/T/pip-install-kh801rm0/xgboost/xgboost/src/objective/objective.cc:26: Unknown objective function: `<function brier at 0x10b630d08>`
Objective candidate: binary:hinge
Objective candidate: multi:softmax
Objective candidate: multi:softprob
Objective candidate: rank:pairwise
Objective candidate: rank:ndcg
Objective candidate: rank:map
Objective candidate: reg:squarederror
Objective candidate: reg:squaredlogerror
Objective candidate: reg:logistic
Objective candidate: binary:logistic
Objective candidate: binary:logitraw
Objective candidate: reg:linear
Objective candidate: count:poisson
Objective candidate: survival:cox
Objective candidate: reg:gamma
Objective candidate: reg:tweedie
关于python - 为什么调用 fit 会重置 XGBClassifier 中的自定义目标函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61067351/
如果没有 Google Fit 应用程序,是否可以使用 Google Fit API? 我想使用 Google Fit API 来计算步数,但是可以在不安装 Google Fit 应用程序的情况下完成
我的应用程序中实现的代码曾经有效,数据已正确插入/显示在 Google Fit 中,但现在不起作用。 我还测试了 BasicHistoryApi 但它不起作用。( https://github.com
我正在努力显示与 Google Fit 应用程序相同的 Activity 历史记录。我在 session 方面做得很好,但我无法正确掌握自动记录的 Activity 。就像示例中的这两个顶级行走一样。
我在使用 Google Fit Api 获取行进距离时遇到问题。我对计步器使用了类似的方法并且有效。它只是说听众已注册。 大部分代码来自 Github 示例。 有什么问题吗? public class
我正在使用此代码尝试检索过去 14 小时内执行的步骤。 YApp myApp = (mYApp) ctx; mGoogleApiClient = myApp.getMyUser();
使用 google fit api 时是否有配额和请求限制?我想使用 google fit api,我很好奇使用它时是否有限制。 最佳答案 您可以在 Google Developer Console
使用 google fit api 时是否有配额和请求限制?我想使用 google fit api,我很好奇使用它时是否有限制。 最佳答案 您可以在 Google Developer Console
无论是使用 fit$loadings 还是使用 fit$Vaccounted 检查它们,我都得到不同的方差值,这些值由因子分析中的因子解释。我正在使用带有 fa() 函数的 psych 包。如果它们应
如果我进入 google api Playground,我会执行以下步骤: 第 1 步:选择并授权 API。我选择两个范围 https://www.googleapis.com/auth/fitnes
我正在执行 https://developers.google.com/fit/android/get-started 中提到的步骤实现一个简单的健身 Android 应用程序。 但是当我想这样做的时
在过去的 6 个月里,我一直在将我的体重输入 Google Fit,现在我想把我的数据拿出来。 访问 Google Fit REST API 不是问题。然而,在所有可访问的数据中找到我的体重数据让我很
我最近尝试尝试使用 Google Fit 应用程序并尝试了 Google Fit developer site 中给出的步骤.并使用了 Android 示例中给出的代码 BasicSensorApi在
我正在创建可以使用 google fit api 的应用程序。 我想获得 google fit 中可用的所有事件( Action )。这里是 google fit 中的事件列表 Reference 。
我尝试了随机森林回归。 代码如下。 import numpy as np from sklearn.preprocessing import StandardScaler from sklearn.m
Google Play documentation claims this is an API_NOT_CONNECTED code ,但是为了访问 Google Fit API,我已经完成了我(认为
我正在使用google javascript api 。为了获取卡路里,我正在使用下一个数据源: 派生:com.google.calories.expished:com.google.android.
我开发了一个需要显示每日步数的应用程序。为此,我使用了 Google Fit SDK 中提供的 API。 似乎一切正常,但我得到的步数与 Google Fit 官方应用程序中显示的步数不匹配。 例如,
我正在尝试从 google fit API 检索用户的每周步数数据,但我从官方 google fit App 数据中获得了不同的步数结果。例如:星期四通过 google fit api 检索到的步数是
我们已经在我们的用户群中发现,自上次 google fit 应用程序更新以来,数据急剧下降,自开始以来,我们一直试图找出代码中的问题。给出时间,我们认为我们使用的版本(当时是 18.0)是问题所在。
拟合高斯混合模型(X-Y数据集)后,如何获取每个分布的参数?例如每个分布的均值、标准差、权重和角度? 我想我可以找到代码 here : def make_ellipses(gmm, ax):
我是一名优秀的程序员,十分优秀!