赞
踩
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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。