Create inference.py
Browse files- inference.py +76 -0
inference.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from statsmodels.tsa.arima.model import ARIMA
|
| 4 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 5 |
+
from sklearn.metrics import mean_squared_error
|
| 6 |
+
from tensorflow.keras.models import Sequential
|
| 7 |
+
from tensorflow.keras.layers import LSTM, Dense
|
| 8 |
+
|
| 9 |
+
def arima_forecast(ts_data, order=(5,1,0), steps=5):
|
| 10 |
+
"""
|
| 11 |
+
ts_data: list of historical stock prices
|
| 12 |
+
steps: number of future steps to forecast
|
| 13 |
+
"""
|
| 14 |
+
ts_series = pd.Series(ts_data)
|
| 15 |
+
model = ARIMA(ts_series, order=order)
|
| 16 |
+
model_fit = model.fit()
|
| 17 |
+
forecast = model_fit.forecast(steps=steps)
|
| 18 |
+
return forecast.tolist()
|
| 19 |
+
|
| 20 |
+
def lstm_forecast(ts_data, look_back=60, steps=5, epochs=20):
|
| 21 |
+
"""
|
| 22 |
+
ts_data: list of historical stock prices
|
| 23 |
+
steps: number of future steps to forecast
|
| 24 |
+
"""
|
| 25 |
+
# Normalize
|
| 26 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 27 |
+
scaled_data = scaler.fit_transform(np.array(ts_data).reshape(-1,1))
|
| 28 |
+
|
| 29 |
+
# Create sequences
|
| 30 |
+
def create_sequences(dataset, look_back):
|
| 31 |
+
X, Y = [], []
|
| 32 |
+
for i in range(len(dataset) - look_back):
|
| 33 |
+
X.append(dataset[i:(i+look_back), 0])
|
| 34 |
+
Y.append(dataset[i + look_back, 0])
|
| 35 |
+
return np.array(X), np.array(Y)
|
| 36 |
+
|
| 37 |
+
X, y = create_sequences(scaled_data, look_back)
|
| 38 |
+
|
| 39 |
+
# Train/test split (use all for training in deployment)
|
| 40 |
+
X = np.reshape(X, (X.shape[0], X.shape[1], 1))
|
| 41 |
+
y = y
|
| 42 |
+
|
| 43 |
+
# Build LSTM
|
| 44 |
+
model = Sequential()
|
| 45 |
+
model.add(LSTM(50, return_sequences=True, input_shape=(look_back,1)))
|
| 46 |
+
model.add(LSTM(50))
|
| 47 |
+
model.add(Dense(1))
|
| 48 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
| 49 |
+
|
| 50 |
+
# Train
|
| 51 |
+
model.fit(X, y, epochs=epochs, batch_size=32, verbose=0)
|
| 52 |
+
|
| 53 |
+
# Forecast future steps
|
| 54 |
+
last_seq = scaled_data[-look_back:].reshape(1, look_back,1)
|
| 55 |
+
predictions = []
|
| 56 |
+
for _ in range(steps):
|
| 57 |
+
pred = model.predict(last_seq, verbose=0)
|
| 58 |
+
predictions.append(pred[0,0])
|
| 59 |
+
last_seq = np.append(last_seq[:,1:,:], [[pred]], axis=1)
|
| 60 |
+
|
| 61 |
+
predictions = scaler.inverse_transform(np.array(predictions).reshape(-1,1))
|
| 62 |
+
return predictions.flatten().tolist()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def infer(model_type: str, input_data: list, steps: int = 5):
|
| 66 |
+
"""
|
| 67 |
+
model_type: 'arima' or 'lstm'
|
| 68 |
+
input_data: list of recent stock prices
|
| 69 |
+
steps: number of future days to forecast
|
| 70 |
+
"""
|
| 71 |
+
if model_type.lower() == 'arima':
|
| 72 |
+
return arima_forecast(input_data, steps=steps)
|
| 73 |
+
elif model_type.lower() == 'lstm':
|
| 74 |
+
return lstm_forecast(input_data, steps=steps)
|
| 75 |
+
else:
|
| 76 |
+
return {"error": "Invalid model_type. Use 'arima' or 'lstm'."}
|