{"id":50,"date":"2022-01-18T22:27:00","date_gmt":"2022-01-18T22:27:00","guid":{"rendered":"https:\/\/tensor.agenthub.uk\/?p=50"},"modified":"2024-05-23T10:12:42","modified_gmt":"2024-05-23T10:12:42","slug":"xgboost%e8%87%aa%e5%ae%9a%e4%b9%89%e7%9b%ae%e6%a0%87%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/tensorzen.blog\/?p=50","title":{"rendered":"XGBoost\u81ea\u5b9a\u4e49\u76ee\u6807\u51fd\u6570"},"content":{"rendered":"\n<p>xgboost\u5185\u7f6e\u4e86\u8db3\u591f\u4e30\u5bcc\u7684\u76ee\u6807\u51fd\u6570(objective function)\uff0c\u6b63\u5e38\u6765\u8bf4\u662f\u80fd\u591f\u5e94\u4ed8\u65e5\u5e38\u9700\u6c42\u7684\uff0c\u5982\u679c\uff5e\u4e07\u4e00\uff5e\u4f60\u6709\u7279\u6b8a\u9700\u6c42\uff0c\u5b83\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\uff0c\u6216\u8005\u53eb\u635f\u5931\u51fd\u6570(loss function)<\/p>\n\n\n\n<p>xgboost\u7684documentation\u91cc\u6709\u4ecb\u7ecd\u5982\u4f55\u81ea\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\uff0chttps:\/\/xgboost.readthedocs.io\/en\/stable\/tutorials\/custom_metric_obj.html<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Squared Log Error<\/h2>\n\n\n\n<p>\u6309\u7167\u6587\u6863\u4e2dSquared Log Error (SLE)\uff0c\u8bb0\u5f55\u4e00\u4e0b\u8fd9\u4e2a\u8fc7\u7a0b<\/p>\n\n\n\n<p>Squared Log Error:<\/p>\n\n\n\n<p>$$\\frac{1}{2}\\left [ \\log (\\hat{y} + 1) &#8211; \\log (y + 1) \\right ]^2$$<\/p>\n\n\n\n<p>\u5176\u4e2d$y$\u662f\u5b9e\u9645\u503c\uff0c$\\hat{y}$\u662f\u9884\u6d4b\u503c,$\\log$\u91cc\u9762\u52a01\u662f\u907f\u514d\u51fa\u73b0$\\log(0)$\u7684\u60c5\u51b5\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u7528\u635f\u5931\u51fd\u6570\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u597d\u574f\uff0c\u6216\u8005\u7528metric\u6765\u8bc4\u4f30\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\uff0c\u6587\u6863\u91cc\u53eb\u5b83Root Mean Squared Log Error(RMSLE)<\/p>\n\n\n\n<p>$$\\sqrt{\\frac{1}{N}\\left [ \\log (\\hat{y} + 1) &#8211; \\log (y + 1) \\right ]^2}$$<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u51fd\u6570\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u53ef\u4ee5\u628a\u6211\u4eec\u81ea\u5df1\u5199\u7684\u76ee\u6807\u51fd\u6570\u4e22\u7ed9xgboost.train\u65b9\u6cd5\uff0c\u8fd9\u6837\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u76ee\u6807\u51fd\u6570\u548cmetric\u5c31\u4f1a\u6309\u7167\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u8fdb\u884cfit\u3002\u770b\u4e00\u4e0bdocumentation\u4e2dxgboost.train\u7684\u90e8\u5206\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/05\/image-3-1024x613.png\" alt=\"\" class=\"wp-image-585\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/05\/image-3-1024x613.png 1024w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/05\/image-3-300x179.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/05\/image-3-768x459.png 768w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/05\/image-3.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/xgboost.readthedocs.io\/en\/stable\/python\/python_api.html\">https:\/\/xgboost.readthedocs.io\/en\/stable\/python\/python_api.html<\/a><\/figcaption><\/figure>\n\n\n\n<p>\u6b63\u5982\u4f60\u60f3\u8c61\u7684\u90a3\u6837\uff0cobj\u53c2\u6570\u63a5\u6536\u7684\u662f\u81ea\u5b9a\u4e49\u7684\u76ee\u6807\u51fd\u6570\uff0cfeval\u662f\u81ea\u5b9a\u4e49\u7684metric\uff0c\u76ee\u6807\u51fd\u6570(obj)\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u662fpredt, np.ndarray\u683c\u5f0f\u7684\uff0c\u8868\u793a\u524d\u9762$i-1$\u8f6e\u8fed\u4ee3\u540e\u8f93\u51fa\u7684\u9884\u6d4b\u503c$\\hat{y}$\uff0c\u53ef\u4ee5\u6807\u8bb0\u4e3a:<\/p>\n\n\n\n<p>$$F_{i-1}(x) = \\sum_{i-1}f_i(x)$$<\/p>\n\n\n\n<p>\u5176\u4e2d$f_i$\u662f\u57fa\u5b66\u4e60\u5668\u3002\u53e6\u4e00\u4e2adtrain, xgb.DMatrix\u683c\u5f0f\u7684\uff0c\u88c5\u4e86\u4e00\u4e9b\u8bad\u7ec3\u96c6\u7684\u4fe1\u606f\uff0cfeatures\u5e76\u4e0d\u4f1a\u4f20\u9001\u8fc7\u6765\uff0c\u56e0\u4e3a\u592a\u5927\u4e86\uff0c\u5e76\u4e14\u8ba1\u7b97object\u4e5f\u7528\u4e0d\u4e0a\u3002\u9700\u8981\u8fd4\u56de\u4e24\u4e2anp.ndarray\u7684\u53d8\u91cf\uff0c\u4e00\u4e2a\u662fgradient, \u53e6\u4e00\u4e2a\u662fhessian\uff0c\u5373\u4e00\u9636\u548c\u4e8c\u9636\u5bfc\u6570\u3002\u5982\u679c\u4f60\u770b\u8fc7\u4e0a\u7bc7\u7684\u5185\u5bb9\uff0c\u53ef\u80fd\u4f1a\u6709\u7591\u95ee\u4e3a\u4ec0\u4e48\u8981\u7528\u4e8c\u9636\u5bfc\u6570\uff1f\u8fd9\u4e2a\u5728\u540e\u9762\u7684\u6587\u7ae0\u518d\u4ecb\u7ecd\u5427\uff5e<\/p>\n\n\n\n<p>Gradient:<\/p>\n\n\n\n<p>$$\\frac{\\partial \\text{obj}}{\\partial \\hat{y}} = \\frac{\\log(\\hat{y} + 1 &#8211; \\log(y+1)}{\\hat{y} + 1}$$<\/p>\n\n\n\n<p>Hessian:<\/p>\n\n\n\n<p>$$\\frac{\\partial ^2\\text{obj}}{\\partial \\hat{y}^2} = \\frac{1 + \\log (\\hat{y} + 1) &#8211; \\log(y+1)}{(\\hat{y} + 1)^2}$$<\/p>\n\n\n\n<p>\u7528Python\u5b9e\u73b0\u4e0a\u8ff0\u4e24\u4e2a\u516c\u5f0f\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def gradient(y_pred, y_true):\n    numerator = np.log(y_pred + 1) - np.log(y_true + 1)\n    denominator = y_pred + 1\n    return numerator \/ denominator\n\ndef hessian(y_pred, y_true):\n    numerator = 1 + np.log(y_true + 1) - np.log(y_pred + 1)\n    denominator = np.power(y_pred + 1, 2)\n    return numerator \/ denominator\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">gradient<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">y_pred<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">y_true<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    numerator <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> np.log(y_pred <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> np.log(y_true <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    denominator <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> y_pred <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> numerator <\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\"> denominator<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">hessian<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">y_pred<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">y_true<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    numerator <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> np.log(y_true <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> np.log(y_pred <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    denominator <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> np.power(y_pred <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> numerator <\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\"> denominator<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u6309\u7167API\u7684\u8981\u6c42\u5b9e\u73b0obj\u51fd\u6570\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def objective_function(pred, dtrain):\n    y_true = dtrain.get_label()\n    grad = gradient(pred, y_true)\n    hess = hessian(pred, y_true)\n    return grad, hess\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">objective_function<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">pred<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">dtrain<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    y_true <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dtrain.get_label()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    grad <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> gradient(pred, y_true)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    hess <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> hessian(pred, y_true)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> grad, hess<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u987a\u4fbf\u6309\u7167\u8981\u6c42\u628ametric\u4e5f\u5b9e\u73b0\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def evaluate_function(pred, dtrain):\n    y_true = dtrain.get_label()\n    n = len(pred)\n    evaluate_name = 'rmsle'\n    evaluate_value = np.sqrt(np.mean(np.power(np.log(pred + 1) - np.log(y_true + 1), 2)))\n    return evaluate_name, evaluate_value\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">evaluate_function<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">pred<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">dtrain<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    y_true <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> dtrain.get_label()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    n <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">len<\/span><span style=\"color: #F6F6F4\">(pred)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    evaluate_name <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">rmsle<\/span><span style=\"color: #DEE492\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    evaluate_value <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> np.sqrt(np.mean(np.power(np.log(pred <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> np.log(y_true <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">), <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">)))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> evaluate_name, evaluate_value<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u7136\u540e\u5728train\u6a21\u578b\u7684\u65f6\u5019\u50cf\u8fd9\u6837\u88c5\u586b\u4e00\u4e0b\u6211\u4eec\u81ea\u5b9a\u4e49\u7684obj\u548cfeval<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"bst = xgb.train(param, \n                train_data, \n                num_boost_round=2, \n                evals=[(test_data, 'test')], \n                verbose_eval=True, \n                obj = objective_function,\n                feval = evaluate_function\n               )\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">bst <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> xgb.train(param, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                train_data, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                <\/span><span style=\"color: #FFB86C; font-style: italic\">num_boost_round<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                <\/span><span style=\"color: #FFB86C; font-style: italic\">evals<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">[(test_data, <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">test<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">)], <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                <\/span><span style=\"color: #FFB86C; font-style: italic\">verbose_eval<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">True<\/span><span style=\"color: #F6F6F4\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                <\/span><span style=\"color: #FFB86C; font-style: italic\">obj<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> objective_function,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                <\/span><span style=\"color: #FFB86C; font-style: italic\">feval<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> evaluate_function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">               )<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u5927\u6982\u5c31\u662f\u8fd9\u4e9b\u4e86\uff0cxgboost\u53ea\u662f\u4e2a\u5f15\u5b50\uff0c\u540e\u9762\u6211\u4eec\u5c55\u5f00\u804a\u4e00\u4e0bLightGBM\u81ea\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\u7684\u4e8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>xgboost\u5185\u7f6e\u4e86\u8db3\u591f\u4e30\u5bcc\u7684\u76ee\u6807\u51fd\u6570(objective function)\uff0c\u6b63\u5e38\u6765\u8bf4\u662f\u80fd\u591f\u5e94\u4ed8\u65e5\u5e38\u9700\u6c42\u7684\uff0c\u5982\u679c\uff5e\u4e07\u4e00\uff5e\u4f60\u6709\u7279\u6b8a\u9700\u6c42\uff0c\u5b83\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\uff0c\u6216\u8005\u53eb\u635f\u5931\u51fd\u6570(loss function)\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4e0b\u600e\u4e48\u81ea\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[27,28],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-coding","category-machine-learning","tag-gbdt","tag-xgboost"],"_links":{"self":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=50"}],"version-history":[{"count":7,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":775,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/50\/revisions\/775"}],"wp:attachment":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}