分位数回归 | xgboost的分位数回归(Python)

分位数回归 | xgboost的分位数回归(Python)

import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt

创建示例数据

np.random.seed(0)
X = np.random.rand(100, 1)
y = 2 * X + 1 + np.random.normal(0, 0.5, size=(100, 1))

创建分位数回归模型,估计不同分位数下的系数

quantiles = [0.25, 0.5, 0.75]
model = sm.QuantReg(y, sm.add_constant(X))
results = [model.fit(q=q) for q in quantiles]

绘制数据和回归线

plt.scatter(X, y, alpha=0.5)
for i, q in enumerate(quantiles):
y_pred = results[i].predict(sm.add_constant(X))
plt.plot(X, y_pred, label=f’q = {q}')
plt.legend()
plt.xlabel(‘X’)
plt.ylabel(‘Y’)
plt.show()

你可能感兴趣的:(分位数回归(QR),数据回归算法(DR),回归,python,数据挖掘)