Skip to content

Instantly share code, notes, and snippets.

@bentona
Created April 26, 2017 16:41
Show Gist options
  • Save bentona/27eb812b3a7b3697017ff55880c9f631 to your computer and use it in GitHub Desktop.
Save bentona/27eb812b3a7b3697017ff55880c9f631 to your computer and use it in GitHub Desktop.
Linear Regression Carbon Five Summit
%matplotlib nbagg
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import collections as mc
x = [2000, 2001, 2007, 2008, 2013, 2014, 2015, 2016]
y = [5, 8, 14, 18, 38, 46, 63, 72]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
np_x = np.array(x)
np_y = np.array(y)
z2 = np.polyfit(x, y, 2)
s2 = (z2[0] * (t**2)) + (z2[1] * t) + z2[2]
z3 = np.polyfit(x, y, 3)
print(z3)
s3 = (z3[0] * (t**3)) + (z3[1] * (t**2)) + (z3[2] * t) + z3[3]
lines = [[(x[i], y[i]), (x[i], slope*x[i] + intercept)] for i in range(len(x))]
c = np.array([(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1)])
lc = mc.LineCollection(lines, colors=(1,0,0))#, linewidths=2)
ax = plt.axes()
#ax.add_collection(lc)
t = np.arange(2000, 2025, 1)
s = slope*t + intercept
#plt.plot(t, s)
plt.plot(t, s2)
#plt.plot(t, s3)
plt.plot(x, y, 'ro')
plt.xlabel('Year')
plt.ylabel('Carbon Five employee count')
plt.title('Carbon Five Employee Growth Over Time')
plt.grid(True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment