Last active
December 17, 2015 18:59
-
-
Save davidcoallier/5656967 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print forecast_2w | |
forecast_2w[0] == b_0 + (coeff * 12) // array([ True], dtype=bool) | |
forecast_2w[1] == b_0 + (coeff * 13) // array([ True], dtype=bool) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat(forecast_2w) | |
forecast_2w[1] == b_0 + (coeff * 12) // TRUE | |
forecast_2w[2] == b_0 + (coeff * 13) // TRUE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lr = LinearRegression() | |
lr.fit(week, revenue) | |
b_0 = lr.intercept_ | |
coeff = lr.coef_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
revenue_fit <- lm(revenue ~ week, data=data) | |
b_0 <- revenue_fit$coefficients[[1]] | |
coeff <- revenue_fit$coefficients[[2]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not query pretty, but we align our week matrices. | |
predict_week = np.array( | |
[a for a in xrange(max(week)+1, max(week)+3)] | |
)[:, np.newaxis] | |
forecast_2w = lr.predict(predict_week) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's forcast the next 2 weeks. | |
data_ahead <- data.frame(week=c(length(data$week)+1, length(data$week)+2)) | |
forecast_2w <- predict(revenue_fit, data_ahead) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pylab as plt | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import r2_score |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's predict the values for existing weeks (Testing) | |
pred = lr.predict(test_week) | |
plt.scatter(week, rev, color='b') | |
plt.scatter(test_week, pred, color='red') | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test <- data | |
predict_test = predict(revenue_fit, test) | |
# Look at it. | |
plot(revenue ~ week, data=data) | |
lines(predict_test, col="red") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
week | customer_count | support_requests | revenue | support_cost | |
---|---|---|---|---|---|
1 | 10 | 2 | 100 | 12 | |
2 | 15 | 5 | 150 | 15 | |
3 | 25 | 7 | 240 | 20 | |
4 | 33 | 12 | 350 | 20 | |
5 | 51 | 13 | 552 | 20 | |
6 | 134 | 20 | 880 | 36 | |
7 | 150 | 22 | 900 | 38 | |
8 | 200 | 29 | 1020 | 44 | |
9 | 212 | 31 | 1100 | 46 | |
10 | 199 | 23 | 1089 | 45 | |
11 | 220 | 32 | 1145 | 50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rsquared <- function(y_true, y_pred) { | |
numerator <- sum((y_true - y_pred) ** 2) | |
denominator <- sum((y_true - mean(y_true)) ** 2) | |
1 - numerator/denominator | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data <- read.csv("/path/to/revenue-example.csv", header=TRUE) | |
plot(data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = pd.read_csv("/path/to/revenue-example.csv", sep=",") | |
week = data['week'][:, np.newaxis] | |
revenue = data['revenue'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's just test some points. | |
test_week = week[1:7] | |
test_rev = revenue[1:7] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sklearn has an r2_score method. | |
score = r2_score(rev, lr.predict(week[:])) | |
print score | |
# Or you can `score` the one from LinearRegression | |
score = lr.score(week[:], rev[:]) | |
print score |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment