Python中執(zhí)行分位數(shù)回歸的示例詳解
線性回歸被定義為根據(jù)給定的變量集構(gòu)建因變量和自變量之間關(guān)系的統(tǒng)計(jì)方法。在執(zhí)行線性回歸時(shí),我們對(duì)計(jì)算響應(yīng)變量的平均值感到好奇。相反,我們可以使用稱為分位數(shù)回歸的機(jī)制來(lái)計(jì)算或估計(jì)響應(yīng)值的分位數(shù)(百分位數(shù))值。例如,第30百分位、第50百分位等。
分位數(shù)回歸
分位數(shù)回歸是線性回歸的擴(kuò)展版本。分位數(shù)回歸構(gòu)建一組變量(也稱為自變量)和分位數(shù)(也稱為因變量)之間的關(guān)系。
在Python中執(zhí)行分位數(shù)回歸
計(jì)算分位數(shù)回歸是一個(gè)逐步的過程。所有步驟詳細(xì)討論如下:
創(chuàng)建演示數(shù)據(jù)集
現(xiàn)在,讓我們創(chuàng)建一個(gè)數(shù)據(jù)集。例如,我們正在創(chuàng)建一個(gè)數(shù)據(jù)集,其中包含20輛不同品牌的汽車的總行駛距離和總排放量的信息。
# Python program to create a dataset # Importing libraries import numpy as np import pandas as pd import statsmodels.api as sm import statsmodels.formula.api as smf import matplotlib.pyplot as plt np.random.seed(0) # Specifying the number of rows rows = 20 # Constructing Distance column Distance = np.random.uniform(1, 10, rows) # Constructing Emission column Emission = 20 + np.random.normal(loc=0, scale=.25*Distance, size=20) # Creating a dataframe df = pd.DataFrame({'Distance': Distance, 'Emission': Emission}) df.head()
輸出
Distance Emission
0 5.939322 22.218454
1 7.436704 19.618575
2 6.424870 20.502855
3 5.903949 18.739366
4 4.812893 16.928183
構(gòu)建分位數(shù)回歸
現(xiàn)在,我們將構(gòu)建分位數(shù)回歸模型,
- 行駛距離:作為預(yù)測(cè)變量
- 排放量:作為響應(yīng)變量
現(xiàn)在,我們將利用這個(gè)模型來(lái)估計(jì)基于汽車行駛的總距離產(chǎn)生的排放的第70個(gè)百分位數(shù)。
# Python program to illustrate # how to estimate quantile regression # Importing libraries import numpy as np import pandas as pd import statsmodels.api as sm import statsmodels.formula.api as smf import matplotlib.pyplot as plt np.random.seed(0) # Number of rows rows = 20 # Constructing Distance column Distance = np.random.uniform(1, 10, rows) # Constructing Emission column Emission = 40 + Distance + np.random.normal(loc=0, scale=.25*Distance, size=20) # Creating the data set df = pd.DataFrame({'Distance': Distance, 'Emission': Emission}) # fit the model model = smf.quantreg('Emission ~ Distance', df).fit(q=0.7) # view model summary print(model.summary())
從該程序的輸出中,可以推導(dǎo)出估計(jì)的回歸方程為:
val = 39.5647 + 1.3042 * X (distance in km)
這意味著行駛X km的所有汽車的排放的第70百分位數(shù)預(yù)期為val。
可視化分位數(shù)回歸
為了可視化和理解分位數(shù)回歸,我們可以使用散點(diǎn)圖擬合分位數(shù)回歸。
# Python program to visualize quantile regression # Importing libraries import numpy as np import pandas as pd import statsmodels.api as sm import statsmodels.formula.api as smf import matplotlib.pyplot as plt np.random.seed(0) # Number of rows rows = 20 # Constructing Distance column Distance = np.random.uniform(1, 10, rows) # Constructing Emission column Emission = 40 + Distance + np.random.normal(loc=0, scale=.25*Distance, size=20) # Creating a dataset df = pd.DataFrame({'Distance': Distance, 'Emission': Emission}) # #fit the model model = smf.quantreg('Emission ~ Distance', df).fit(q=0.7) # define figure and axis fig, ax = plt.subplots(figsize=(10, 8)) # get y values y_line = lambda a, b: a + Distance y = y_line(model.params['Intercept'], model.params['Distance']) # Plotting data points with the help # pf quantile regression equation ax.plot(Distance, y, color='black') ax.scatter(Distance, Emission, alpha=.3) ax.set_xlabel('Distance Traveled', fontsize=20) ax.set_ylabel('Emission Generated', fontsize=20) # Save the plot fig.savefig('quantile_regression.png')
到此這篇關(guān)于Python中執(zhí)行分位數(shù)回歸的示例詳解的文章就介紹到這了,更多相關(guān)Python分位數(shù)回歸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python?PyAutoGUI實(shí)現(xiàn)自動(dòng)化鼠標(biāo)鍵盤等常用操作
這篇文章主要介紹了python?PyAutoGUI實(shí)現(xiàn)自動(dòng)化鼠標(biāo)鍵盤等常用操作使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12基于并發(fā)服務(wù)器幾種實(shí)現(xiàn)方法(總結(jié))
下面小編就為大家分享一篇基于并發(fā)服務(wù)器幾種實(shí)現(xiàn)方法(總結(jié)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2017-12-12Ubuntu權(quán)限不足無(wú)法創(chuàng)建文件夾解決方案
這篇文章主要介紹了Ubuntu權(quán)限不足無(wú)法創(chuàng)建文件夾解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python編程學(xué)習(xí)使用管道Pipe編寫優(yōu)化代碼
大家好,今天這篇文章我將詳細(xì)講解 Pipe 如何讓你的代碼更加簡(jiǎn)潔的方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Python 如何給圖像分類(圖像識(shí)別模型構(gòu)建)
這篇文章主要介紹了Python 教你如何給圖像分類,今天的文章主要是講圖像識(shí)別模型如何構(gòu)建,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06