Skip to content

Commit 705c6a2

Browse files
committed
added Quantile Regression
1 parent 6580d7d commit 705c6a2

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Python program to visualize quantile regression
2+
3+
# Importing libraries
4+
import numpy as np
5+
import pandas as pd
6+
import statsmodels.api as sm
7+
import statsmodels.formula.api as smf
8+
import matplotlib.pyplot as plt
9+
10+
np.random.seed(0)
11+
12+
# Number of rows
13+
rows = 20
14+
15+
# Constructing Distance column
16+
Distance = np.random.uniform(1, 10, rows)
17+
18+
# Constructing Emission column
19+
Emission = 40 + Distance + np.random.normal(loc=0,
20+
scale=.25*Distance,
21+
size=20)
22+
23+
# Creating a dataset
24+
df = pd.DataFrame({'Distance': Distance,
25+
'Emission': Emission})
26+
27+
# #fit the model
28+
model = smf.quantreg('Emission ~ Distance',
29+
df).fit(q=0.7)
30+
31+
# define figure and axis
32+
fig, ax = plt.subplots(figsize=(10, 8))
33+
34+
# get y values
35+
y_line = lambda a, b: a + Distance
36+
y = y_line(model.params['Intercept'],
37+
model.params['Distance'])
38+
39+
# Plotting data points with the help
40+
# pf quantile regression equation
41+
ax.plot(Distance, y, color='black')
42+
ax.scatter(Distance, Emission, alpha=.3)
43+
ax.set_xlabel('Distance Traveled', fontsize=20)
44+
ax.set_ylabel('Emission Generated', fontsize=20)
45+
46+
# Save the plot
47+
fig.savefig('quantile_regression.png')

0 commit comments

Comments
 (0)