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è)逐步的過(guò)程。所有步驟詳細(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?groupby函數(shù)實(shí)現(xiàn)分組選取最大值與最小值
這篇文章主要介紹了python?groupby函數(shù)實(shí)現(xiàn)分組選取最大值與最小值,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識(shí)別的方法
這篇文章主要介紹了python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識(shí)別的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python?selenium把歌詞評(píng)論做成詞云圖
大家好,本篇文章主要講的是Python?selenium把歌詞評(píng)論做成詞云圖,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Python新建項(xiàng)目自動(dòng)添加介紹和utf-8編碼的方法
這篇文章主要介紹了Python新建項(xiàng)目自動(dòng)添加介紹和utf-8編碼的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Python wxPython創(chuàng)建文件復(fù)制工具
wxPython是一個(gè)功能強(qiáng)大的 GUI 框架,它允許開(kāi)發(fā)者通過(guò) Python 輕松構(gòu)建跨平臺(tái)的桌面應(yīng)用,本文將使用wxPython創(chuàng)建一個(gè)文件復(fù)制工具,感興趣的可以了解下2025-02-02
python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維
今天小編就為大家分享一篇python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

