-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharima.py
57 lines (47 loc) · 5.41 KB
/
arima.py
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
import pandas as pd
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
# Generate synthetic time series data (or replace this with your actual data)
np.random.seed(0)
data = [78, 65, 45, 75, 12, 34, 51, 29, 8, 76, 52, 12, 39, 8, 6, 75, 35, 24, 31, 28, 75, 46, 68, 80, 8, 86, 64, 42, 13, 35, 57, 79, 97, 75, 56, 67, 80, 97, 42, 12, 34, 4, 3, 7, 89, 90, 86, 52, 13, 71, 31, 56, 79, 35, 41, 46, 78, 94, 37, 42, 47, 85, 24, 57, 86, 24, 68, 97, 53, 78, 22,
68, 98, 53, 48, 97, 53, 21, 25, 67, 13, 75, 25, 69, 41, 57, 8, 46, 75, 24, 79, 68, 42, 2, 86, 34, 89, 9, 53, 21, 46, 32, 67, 42, 67, 87, 63, 37, 42, 79, 42, 74, 87, 53, 53, 67, 64, 65, 35, 79, 75, 34, 34, 21, 47, 90, 17, 93, 6, 21, 10, 29, 38, 47, 56, 65, 74, 83, 92, 1, 2, 98,
25, 96, 85, 44, 43, 11, 33, 68, 46, 34, 79, 65, 35, 78, 66, 43, 33, 68, 90, 77, 54, 78, 43, 69, 64, 42, 46, 80, 76, 43, 67, 89, 6, 4, 4, 5, 67, 90, 86, 54, 67, 89, 43, 79, 46, 7, 8, 99, 54, 78, 99, 65, 33, 27, 4, 3, 72, 62, 15, 12, 68, 31, 68, 72, 63, 72, 75, 8, 56, 23, 57, 52, 57, 87, 36, 31, 58, 92, 69, 62, 37, 27, 9, 42, 3, 68, 48, 90, 32, 26, 78, 37, 14, 61, 79, 23, 12, 78, 42, 25, 23, 68, 64, 14, 68, 64, 23,
58, 27, 2, 15, 80, 86, 53, 41, 58, 41, 34, 68, 41, 78, 65, 41, 79, 56, 27, 89, 73, 23, 35, 79, 42, 18, 90, 23, 68, 52, 68, 79, 31, 58, 9, 52, 78, 10, 9, 36, 8, 20, 57, 95, 69, 30, 64, 68, 43, 46, 8, 40, 68, 64, 47, 2, 79, 32, 89, 63, 25, 80, 63, 47, 89, 2, 34, 8, 47, 96, 24, 78, 42, 56, 89, 58, 5, 37, 4, 31, 58, 94, 62, 37, 8, 42, 72, 34, 78, 41, 57, 89, 4, 21, 23, 74, 79, 52, 46, 89, 31, 23, 45, 12, 67, 53, 13, 42, 46, 78, 41, 34, 75, 35, 74, 25, 86, 12, 57, 90, 66, 32, 34, 68, 65, 23, 12, 34, 67, 31, 36, 74, 31, 68, 94, 13, 78, 74, 31, 34, 67, 34,
56, 47, 43, 89, 34, 67, 34, 65, 31, 35, 6, 3, 23, 34, 56, 32, 67, 94, 19, 85, 42, 37, 64, 78, 5, 80, 14, 47, 79, 42, 79, 41, 37, 95, 21, 68, 96, 31, 34, 68, 42, 68, 32, 68, 90, 61, 27, 95, 3, 13, 27, 96, 37, 49, 73, 34, 68, 42, 78, 41, 47, 89, 63, 12, 35, 79, 52, 46, 87, 34, 68, 41, 23, 57, 75, 31, 79, 41, 47, 80, 42, 58, 36, 85, 23, 47, 82, 69, 51, 13, 57, 89, 66, 33, 16, 89, 62, 47, 8, 43, 78, 64, 35, 78, 74, 34, 68, 94, 21, 86, 31, 3, 68, 12, 31, 57, 82, 16, 84, 15, 89, 34, 78, 68, 62, 16, 89, 71, 57, 89, 41, 86, 53, 67, 74, 5, 60, 65, 35, 29, 62, 47, 57, 46, 4, 86, 4, 31, 74, 79, 14, 69, 61, 20, 69, 18, 18, 98, 49, 68, 12, 49, 69, 54, 32, 12, 35, 67, 95, 42, 19, 95, 19, 59, 67, 81,
23, 59, 68, 1, 46, 57, 3, 66, 43, 12, 35, 67, 52, 12, 11, 35, 67, 64, 31, 57, 65, 41, 23, 56, 76, 54, 32, 1, 95, 60, 39, 23, 39, 35, 65, 75, 95, 39, 13, 67, 63, 21, 6, 54, 59, 32, 15, 69, 95, 43, 3, 41, 91, 94, 6, 41, 46, 91, 69, 57, 13, 69, 78, 5, 57, 15, 6, 62, 57, 31, 35, 53, 68, 76, 90, 9, 67, 87, 75, 23, 44, 43, 23, 56, 79, 52, 67, 64, 21, 68, 98, 64, 16, 43, 70, 80, 55, 85, 62, 68, 41, 47, 96, 34, 79, 34, 25, 7, 34, 42, 57, 74, 35, 63, 14, 48, 97, 1, 46, 3, 13, 46, 44, 3, 21, 45, 69, 53, 46, 85, 45, 74, 34, 67, 52, 24, 57, 42, 47, 41, 46, 88, 92, 31, 47, 42, 14, 68, 62, 41, 57, 78, 52, 31, 46, 85, 31, 23, 67, 75, 32, 23, 57, 43, 22, 78, 15, 78, 21, 15, 7, 15, 68, 97, 54, 42, 34, 68, 55, 41, 23, 78, 65, 32, 21, 22, 36, 78, 38, 9, 22, 79, 82, 73, 81, 94, 13, 59, 7, 91, 39, 68, 31, 38, 69, 13, 34, 12, 35, 68, 31, 74, 31, 68, 74, 78, 51, 57, 88, 92, 15, 76, 41, 22, 46, 78, 53, 13, 47, 51, 46, 31, 23, 57, 84, 31, 68, 31, 57, 89, 46, 78, 42, 56, 74, 57, 6, 74, 64, 75, 85, 5, 42, 51, 47, 99, 52, 67, 14, 78, 31, 46, 6, 31, 45, 42, 24, 73, 23, 78, 42, 74, 82, 91, 36, 25, 95, 14, 69, 37, 45, 56, 63, 15, 94, 2, 7, 97, 35, 31, 36, 85, 74, 23, 46, 31, 73, 25, 32, 69, 32, 35, 32, 76, 32, 35, 78, 67, 35, 8, 4, 33, 21, 68, 53, 36, 8, 85, 31, 35, 78, 64, 25, 87, 33, 14, 26, 94, 84, 52, 68, 42, 31, 47, 63, 13, 67, 15, 77, 32, 46, 79, 70, 60, 51, 26, 89, 14, 58, 8, 24, 89, 5, 21,
13, 69, 73, 26, 90, 25, 87, 33, 68, 46, 85, 22, 57, 21, 96, 25, 85, 26, 36, 41, 47, 96, 75, 89, 41, 36, 35, 79, 52, 21, 36, 89, 74, 25, 64, 68, 96, 25, 78, 63, 25, 79, 63, 2, 4, 67, 32, 46, 24, 43, 54, 57, 63, 68, 31, 3, 7, 23, 68, 32, 36, 75, 23, 78, 53, 12, 24, 57, 43, 31, 89, 37, 26, 73, 57, 33, 68, 31, 84]
print(len(data))
ammount = np.zeros(100)
for i in range(100):
for n in data:
if n == i:
ammount[i] +=1
plt.bar(np.arange(0, 100), ammount)
# Convert to a pandas Series
time_series = pd.Series(data)
# Step 1: Split the data into training and testing sets
train_size = int(len(time_series) * 0.80) # Use 80% for training
train, test = time_series[:train_size+1], time_series[train_size:]
# Step 2: Fit the ARIMA model on the training set
p, d, q = 1, 1, 1 # Adjust these as needed
model = ARIMA(train, order=(p, d, q))
model_fit = model.fit()
# Step 3: Forecast the test set period
# Here, we forecast the exact number of steps as in the test set to compare
forecast = model_fit.forecast(steps=len(test))
# Step 4: Calculate the error of the forecast
error = mean_squared_error(test, forecast)
print(f"Mean Squared Error: {error}")
# Step 5: Plot the training, testing, and forecasted data for comparison
plt.figure(figsize=(12, 6))
plt.plot(train.index, train, label='Training Data')
plt.plot(test.index, test, label='Actual Test Data', color='blue')
plt.plot(test.index, forecast, label='ARIMA Forecast', color='red')
# Add labels and legend
plt.xlabel("Time")
plt.ylabel("Value")
plt.title("ARIMA Forecast vs Actual Data")
plt.legend()
plt.show()