Skip to content

Instantly share code, notes, and snippets.

@davidcoallier
Last active December 17, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidcoallier/5656967 to your computer and use it in GitHub Desktop.
Save davidcoallier/5656967 to your computer and use it in GitHub Desktop.
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)
cat(forecast_2w)
forecast_2w[1] == b_0 + (coeff * 12) // TRUE
forecast_2w[2] == b_0 + (coeff * 13) // TRUE
lr = LinearRegression()
lr.fit(week, revenue)
b_0 = lr.intercept_
coeff = lr.coef_
revenue_fit <- lm(revenue ~ week, data=data)
b_0 <- revenue_fit$coefficients[[1]]
coeff <- revenue_fit$coefficients[[2]]
# 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)
# 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)
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
# 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()
test <- data
predict_test = predict(revenue_fit, test)
# Look at it.
plot(revenue ~ week, data=data)
lines(predict_test, col="red")
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
rsquared <- function(y_true, y_pred) {
numerator <- sum((y_true - y_pred) ** 2)
denominator <- sum((y_true - mean(y_true)) ** 2)
1 - numerator/denominator
}
data <- read.csv("/path/to/revenue-example.csv", header=TRUE)
plot(data)
data = pd.read_csv("/path/to/revenue-example.csv", sep=",")
week = data['week'][:, np.newaxis]
revenue = data['revenue']
# Let's just test some points.
test_week = week[1:7]
test_rev = revenue[1:7]
# 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