File size: 1,354 Bytes
e8ac98a |
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 |
import os
import numpy as np
from warnings import filterwarnings
filterwarnings('ignore')
""" Get Datasets """
async def get_datasets(str datasets_path):
cdef list items = os.listdir(datasets_path)
cdef list csv_files = []
cdef str item
for item in items:
if os.path.isfile(os.path.join(datasets_path, item)) and item.endswith('.csv'):
csv_files.append(item)
return sorted(csv_files)
""" Create Sequences """
async def create_sequences(df, int sequence_length):
cdef list labels = []
cdef list sequences = []
cdef int i
for i in range(len(df) - sequence_length):
seq = df.iloc[i:i + sequence_length].values
label = df.iloc[i + sequence_length].values[0]
sequences.append(seq)
labels.append(label)
return np.array(sequences), np.array(labels)
""" Pre-Process Data """
async def preprocess_data(dataframe):
cdef str col
for col in dataframe.columns:
if dataframe[col].isnull().any():
if dataframe[col].dtype == 'object':
dataframe[col].fillna(dataframe[col].mode()[0], inplace=True)
else:
dataframe[col].fillna(dataframe[col].mean(), inplace=True)
return dataframe
""" Scale Data """
async def scale_data(dataframe, scaler_cls):
scaler = scaler_cls()
dataframe['Close'] = scaler.fit_transform(dataframe[['Close']])
return scaler, dataframe
|