Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Cyber/GenDs.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (61 sloc)
2.33 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get data | |
import pandas as pd | |
import numpy as np | |
import tensorflow as tf | |
raw_data = pd.read_pickle("./data/raw_feature.pkl") | |
X_train = pd.read_pickle("./data/X_train.pkl") | |
y_train = pd.read_pickle("./data/y_train.pkl") | |
X_test = pd.read_pickle("./data/X_test.pkl") | |
y_test = pd.read_pickle("./data/y_test.pkl") | |
X_test_raw = X_test.copy() | |
# preprocess data | |
from sklearn.preprocessing import StandardScaler,MinMaxScaler | |
std = StandardScaler() | |
train_shape = y_train.shape | |
test_shape = y_test.shape | |
y_train = std.fit_transform(np.reshape(y_train,(-1,y_train.shape[-2]*y_train.shape[-1]))) | |
y_train = y_train.reshape(train_shape) | |
y_test = std.transform(np.reshape(y_test,(-1,y_test.shape[-2]*y_test.shape[-1]))) | |
y_test = y_test.reshape(test_shape) | |
# gen dataset | |
from numpy.lib.stride_tricks import as_strided | |
def create_rolling_window(matrix,t): | |
matrix_shape = matrix.shape | |
return_length = matrix_shape[0] - t | |
dataset = tf.data.Dataset.from_tensor_slices(matrix) | |
windows = dataset.window(t,shift = 1,drop_remainder=True) | |
windows = windows.take(return_length) | |
windows = windows.flat_map(lambda window: window.batch(t)) | |
return windows | |
def create_result_ds(matrix,delay): | |
dataset = tf.data.Dataset.from_tensor_slices(matrix) | |
dataset = dataset.skip(delay) | |
return dataset | |
def combine_ds(ds1,ds2): | |
combined_ds = tf.data.Dataset.zip(((ds1),ds2)) | |
combined_ds = combined_ds.batch(batch_size=32) | |
return combined_ds | |
def gen_train_ds(X_train,y_train,step_len): | |
X = create_rolling_window(y_train,step_len) | |
y = create_result_ds(y_train,step_len) | |
train_ds = combine_ds(X,y) | |
return train_ds | |
def gen_test_ds(X_test,y_test,step_len): | |
X = create_rolling_window(y_test,step_len) | |
y = create_result_ds(y_test,step_len) | |
test_ds = combine_ds(X,y) | |
return test_ds | |
# gen dataset | |
time_step = 48 | |
train_ds = gen_train_ds(X_train,y_train,step_len=time_step) | |
test_ds = gen_test_ds(X_test,y_test,step_len=time_step) | |
class GenDs(object): | |
def gen_train(time_step = 48): | |
train_ds = gen_train_ds(X_train,y_train,step_len=time_step) | |
return train_ds,std | |
def gen_test(time_step = 48): | |
test_ds = gen_test_ds(X_test,y_test,step_len=time_step) | |
return test_ds | |
def gen_raw_test(time_step = 48): | |
raw_test_ds = gen_test_ds(X_test_raw,y_test,step_len=time_step) | |
return raw_test_ds |