-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcaffeine_forecast_github.py
More file actions
58 lines (43 loc) · 2.13 KB
/
caffeine_forecast_github.py
File metadata and controls
58 lines (43 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import matplotlib.pyplot as plt
class CoffeeSalesPredictor:
def __init__(self, days, sales):
""" Initialize the model with historical sales data """
self.days = np.array(days).reshape(-1, 1) # Reshape to column vector
self.sales = np.array(sales)
# Compute the trend using Least Squares Method (Simple Linear Regression)
ones = np.ones((self.days.shape[0], 1)) # Add intercept (bias term)
X = np.hstack((ones, self.days)) # Stack ones and days
# Calculate regression coefficients (m and b)
self.coefficients = np.linalg.inv(X.T @ X) @ X.T @ self.sales
self.b, self.m = self.coefficients[0], self.coefficients[1]
def predict(self, future_days):
""" Predict coffee sales for given future days """
future_days = np.array(future_days).reshape(-1, 1)
ones = np.ones((future_days.shape[0], 1)) # Add intercept
X_future = np.hstack((ones, future_days))
return X_future @ self.coefficients
def visualize(self):
""" Visualize the historical data and the trend line """
plt.scatter(self.days, self.sales, color='brown', label="Real Sales Data")
predicted_sales = self.predict(self.days)
plt.plot(self.days, predicted_sales, color='orange', label="Trend Line")
plt.xlabel("Days")
plt.ylabel("Coffee Sales")
plt.title("Coffee Sales Prediction")
plt.legend()
plt.grid(True)
plt.show()
def main():
# Simulated real-world coffee sales data (e.g., sales over 10 days)
days = np.arange(1, 11) # Days from 1 to 10
sales = np.array([50, 55, 53, 60, 65, 68, 72, 70, 75, 78]) # Coffee sales per day
# Create predictor
predictor = CoffeeSalesPredictor(days, sales)
predictor.visualize()
# Predict sales for future days
future_days = [11, 12, 13, 14, 15]
predicted_sales = predictor.predict(future_days)
print(f"Predicted coffee sales for future days {future_days}: {np.round(predicted_sales, 2)} ☕")
if __name__ == "__main__":
main()