分位數(shù)回歸模型quantile regeression應(yīng)用詳解及示例教程
普通最小二乘法如何處理異常值? 它對待一切事物都是一樣的——它將它們平方! 但是對于異常值,平方會顯著增加它們對平均值等統(tǒng)計數(shù)據(jù)的巨大影響。
我們從描述性統(tǒng)計中知道,中位數(shù)對異常值的魯棒性比均值強(qiáng)。 這種理論也可以在預(yù)測統(tǒng)計中為我們服務(wù),這正是分位數(shù)回歸的意義所在——估計中位數(shù)(或其他分位數(shù))而不是平均值。 通過選擇任何特定的分位數(shù)閾值,我們既可以緩和異常值,也可以調(diào)整錯誤的正/負(fù)權(quán)衡。我們還可以處理需要分位數(shù)界限的情況,例如:嬰兒的安全出生體重,頂級競技電子競技玩家的技能水平,等等。
什么是分位數(shù)?
分位數(shù)(Quantile),亦稱分位點(diǎn),是指將一個隨機(jī)變量的概率分布范圍分為幾個等份的數(shù)值點(diǎn),常用的有中位數(shù)(即二分位數(shù))、四分位由3個部分組成(第25、50和75個百分位,常用于箱形圖)和百分位數(shù)等。
什么是分位數(shù)回歸?
分位數(shù)回歸是簡單的回歸,就像普通的最小二乘法一樣,但不是最小化平方誤差的總和,而是最小化從所選分位數(shù)切點(diǎn)產(chǎn)生的絕對誤差之和。 如果 q=0.50(中位數(shù)),那么分位數(shù)回歸會出現(xiàn)一個特殊情況 - 最小絕對誤差(因為中位數(shù)是中心分位數(shù))。我們可以通過調(diào)整超參數(shù) q,選擇一個適合平衡特定于需要解決問題的誤報和漏報的閾值。
statsmodels中的分位數(shù)回歸
分位數(shù)回歸是一種不太常見的模型,但 Python中的StatsModel庫提供了他的實現(xiàn)。這個庫顯然受到了R的啟發(fā),并從它借鑒了各種語法和API。
StatsModel使用的范例與scikit-learn稍有不同。但是與scikit-learn一樣,對于模型對象來說,需要公開一個.fit()方法來實際訓(xùn)練和預(yù)測。但是不同的是scikit-learn模型通常將數(shù)據(jù)(作為X矩陣和y數(shù)組)作為.fit()的參數(shù),而StatsModel是在初始化對象時傳入數(shù)據(jù),而fit方法只傳遞一些可以調(diào)試的超參數(shù)。
下面是來自statsmodel的例子(Engel數(shù)據(jù)集包含在與statmodels中)
%matplotlib inline import numpy as np import pandas as pd import statsmodels.api as sm import statsmodels.formula.api as smf import matplotlib.pyplot as plt data = sm.datasets.engel.load_pandas().data mod = smf.quantreg("foodexp ~ income", data) res = mod.fit(q=0.5) print(res.summary())
我們可以看看quantile regression model fit的幫助文檔:
help(quant_mod.fit)
分位數(shù)回歸與線性回歸
標(biāo)準(zhǔn)最小二乘回歸模型僅對響應(yīng)的條件均值進(jìn)行建模,并且計算成本較低。 相比之下,分位數(shù)回歸最常用于對響應(yīng)的特定條件分位數(shù)進(jìn)行建模。 與最小二乘回歸不同,分位數(shù)回歸不假設(shè)響應(yīng)具有特定的參數(shù)分布,也不假設(shè)響應(yīng)具有恒定方差。
下表總結(jié)了線性回歸和分位數(shù)回歸之間的一些重要區(qū)別:
xgboost的分位數(shù)回歸
最后如果想使用xgboost,又想試試分位數(shù)回歸,那么可以參考以下代碼
class XGBQuantile(XGBRegressor): def __init__(self,quant_alpha=0.95,quant_delta = 1.0,quant_thres=1.0,quant_var =1.0,base_score=0.5, booster='gbtree', colsample_bylevel=1, colsample_bytree=1, gamma=0, learning_rate=0.1, max_delta_step=0,max_depth=3, min_child_weight=1, missing=None, n_estimators=100, n_jobs=1, nthread=None, objective='reg:linear', random_state=0,reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,silent=True, subsample=1): self.quant_alpha = quant_alpha self.quant_delta = quant_delta self.quant_thres = quant_thres self.quant_var = quant_var super().__init__(base_score=base_score, booster=booster, colsample_bylevel=colsample_bylevel, colsample_bytree=colsample_bytree, gamma=gamma, learning_rate=learning_rate, max_delta_step=max_delta_step, max_depth=max_depth, min_child_weight=min_child_weight, missing=missing, n_estimators=n_estimators, n_jobs= n_jobs, nthread=nthread, objective=objective, random_state=random_state, reg_alpha=reg_alpha, reg_lambda=reg_lambda, scale_pos_weight=scale_pos_weight, seed=seed, silent=silent, subsample=subsample) self.test = None def fit(self, X, y): super().set_params(objective=partial(XGBQuantile.quantile_loss,alpha = self.quant_alpha,delta = self.quant_delta,threshold = self.quant_thres,var = self.quant_var) ) super().fit(X,y) return self def predict(self,X): return super().predict(X) def score(self, X, y): y_pred = super().predict(X) score = XGBQuantile.quantile_score(y, y_pred, self.quant_alpha) score = 1./score return score @staticmethod def quantile_loss(y_true,y_pred,alpha,delta,threshold,var): x = y_true - y_pred grad = (x<(alpha-1.0)*delta)*(1.0-alpha)- ((x>=(alpha-1.0)*delta)& (x<alpha*delta) )*x/delta-alpha*(x>alpha*delta) hess = ((x>=(alpha-1.0)*delta)& (x<alpha*delta) )/delta grad = (np.abs(x)<threshold )*grad - (np.abs(x)>=threshold )*(2*np.random.randint(2, size=len(y_true)) -1.0)*var hess = (np.abs(x)<threshold )*hess + (np.abs(x)>=threshold ) return grad, hess @staticmethod def original_quantile_loss(y_true,y_pred,alpha,delta): x = y_true - y_pred grad = (x<(alpha-1.0)*delta)*(1.0-alpha)-((x>=(alpha-1.0)*delta)& (x<alpha*delta) )*x/delta-alpha*(x>alpha*delta) hess = ((x>=(alpha-1.0)*delta)& (x<alpha*delta) )/delta return grad,hess @staticmethod def quantile_score(y_true, y_pred, alpha): score = XGBQuantile.quantile_cost(x=y_true-y_pred,alpha=alpha) score = np.sum(score) return score @staticmethod def quantile_cost(x, alpha): return (alpha-1.0)*x*(x<0)+alpha*x*(x>=0) @staticmethod def get_split_gain(gradient,hessian,l=1): split_gain = list() for i in range(gradient.shape[0]): split_gain.append(np.sum(gradient[:i])/(np.sum(hessian[:i])+l)+np.sum(gradient[i:])/(np.sum(hessian[i:])+l)-np.sum(gradient)/(np.sum(hessian)+l) ) return np.array(split_gain)
https://gist.github.com/benoitdescamps/af5a8e42d5cfc7981e960e4d559dad19#file-xgboostquantile-py
對于LightGBM這里有一篇詳細(xì)的實現(xiàn)文章:
以上就是分位數(shù)回歸quantile regeression詳解及示例教程的詳細(xì)內(nèi)容,更多關(guān)于分位數(shù)回歸quantile regeression的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于OpenCV python3實現(xiàn)證件照換背景的方法
這篇文章主要介紹了基于OpenCV python3實現(xiàn)證件照換背景的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Python獲取時光網(wǎng)電影數(shù)據(jù)的實例代碼
這篇文章主要介紹了Python獲取時光網(wǎng)電影數(shù)據(jù),基本原理是先通過requests庫,通過時光網(wǎng)自帶的電影數(shù)據(jù)API接口,獲取到指定的電影數(shù)據(jù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09使用Python實現(xiàn)大學(xué)座位預(yù)約功能
這篇文章主要介紹了如何用Python實現(xiàn)大學(xué)座位預(yù)約,今天這個教程教你如何搶到座位,有座位了還怕聽不到課嗎?感興趣的朋友一起看看吧2022-03-03Python列表推導(dǎo)式,元組推導(dǎo)式,字典推導(dǎo)式,集合推導(dǎo)式
這篇文章主要介紹了Python列表推導(dǎo)式,元組推導(dǎo)式,字典推導(dǎo)式,集合推導(dǎo)式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-09-09Python Opencv實現(xiàn)單目標(biāo)檢測的示例代碼
這篇文章主要介紹了Python Opencv實現(xiàn)單目標(biāo)檢測的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09