-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
47 lines (36 loc) · 1.52 KB
/
load_data.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
class DataLoader:
def __init__(self, filepath, window_size, test_size=0.1, drop_columns=["Year"]):
self.filepath = filepath
self.window_size = window_size
self.test_size = test_size
self.drop_columns = drop_columns
self.scaler = MinMaxScaler()
def load_data(self):
# Load the dataset
data = pd.read_csv(self.filepath)
# Store column names for later use, excluding dropped columns
self.column_names = [col for col in data.columns if col not in self.drop_columns]
# Drop the columns
if self.drop_columns:
data = data.drop(columns=self.drop_columns)
# Normalize the data
data_normalized = self.scaler.fit_transform(data)
# Create sequences
X, y = self.create_sequences(data_normalized)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.test_size, shuffle=False)
return X_train, X_test, y_train, y_test
def create_sequences(self, data):
X, y = [], []
for i in range(self.window_size, len(data)):
X.append(data[i-self.window_size:i])
y.append(data[i])
X = np.array(X)
y = np.array(y)
# Reshape X to fit the model input shape
X = X.reshape(X.shape[0], X.shape[1], data.shape[1])
return X, y