-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlstm_yield_model.py
More file actions
45 lines (34 loc) · 1.12 KB
/
lstm_yield_model.py
File metadata and controls
45 lines (34 loc) · 1.12 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
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
# Load data
df = pd.read_csv("Train.csv")
df['SDate'] = pd.to_datetime(df['SDate'], errors='coerce', dayfirst=True)
df = df.dropna(subset=['SDate'])
df = df.sort_values('SDate')
df = df.groupby('SDate')['ExpYield'].mean().reset_index()
df.set_index('SDate', inplace=True)
print("After grouping:")
print(df.head())
# Scaling
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df)
def create_sequences(data, seq_length=5):
X, y = [], []
for i in range(len(data) - seq_length):
X.append(data[i:i+seq_length])
y.append(data[i+seq_length])
return np.array(X), np.array(y)
X, y = create_sequences(scaled_data)
print("X shape:", X.shape)
print("y shape:", y.shape)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
model = Sequential([
LSTM(64, activation='relu', input_shape=(X.shape[1], 1)),
Dense(1)
])
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=20, batch_size=16)
model.save("lstm_yield_model.keras")
print("✅ LSTM model trained and saved")