-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
85 lines (68 loc) · 3.05 KB
/
Copy pathpreprocessing.py
File metadata and controls
85 lines (68 loc) · 3.05 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import torch
from tqdm import tqdm
from joblib import Parallel, delayed
def preprocess_dataset(file_path: str, seq_len: int):
# Step 1: Load data
print("Loading dataset...")
df = pd.read_csv(
file_path,
header=0, # 첫 번째 줄을 헤더로 사용
names=["Timestamp", "IO_Type", "Sector", "Size"],
dtype={"Timestamp": float, "IO_Type": str, "Sector": int, "Size": int},
low_memory=False
)
# Step 2: Encode IO_Type (Categorical to Numerical)
print("Encoding IO_Type...")
df["IO_Type"] = df["IO_Type"].map({"A": 0, "Q": 1, "G": 2, "I": 3, "D": 4, "C": 5, "M": 6})
df = df.dropna() # Drop rows with NaN values
# Step 3: Scale the numeric columns
print("Scaling numeric data...")
scaler = MinMaxScaler()
df[["Timestamp", "Sector", "Size"]] = scaler.fit_transform(df[["Timestamp", "Sector", "Size"]])
# Step 4: Prepare sequences for LSTM using parallel processing
print("Preparing sequences...")
batch_size = 10000
array = df.values
# sampleing
sequences = []
for start in tqdm(range(0, len(array) - seq_len + 1, batch_size), desc="Preparing Sequences", unit="batch"):
end = min(start + batch_size, len(array) - seq_len + 1)
batch_sequences = [array[i:i+seq_len] for i in range(start, end)]
sequences.extend(batch_sequences)
# Convert to PyTorch Tensor
print("Converting to PyTorch Tensor...")
data_tensor = torch.tensor(np.array(sequences), dtype=torch.float32)
return data_tensor, scaler, df
def preprocess_dataset_sampleing(file_path: str, seq_len: int):
# Step 1: Load data
print("Loading dataset...")
df = pd.read_csv(
file_path,
header=0, # 첫 번째 줄을 헤더로 사용
names=["Timestamp", "IO_Type", "Sector", "Size"],
dtype={"Timestamp": float, "IO_Type": str, "Sector": int, "Size": int},
low_memory=False
)
# Step 2: Encode IO_Type (Categorical to Numerical)
print("Encoding IO_Type...")
df["IO_Type"] = df["IO_Type"].map({"A": 0, "Q": 1, "G": 2, "I": 3, "D": 4, "C": 5, "M": 6})
df = df.dropna() # Drop rows with NaN values
# Step 3: Scale the numeric columns
print("Scaling numeric data...")
scaler = MinMaxScaler()
df[["Timestamp", "Sector", "Size"]] = scaler.fit_transform(df[["Timestamp", "Sector", "Size"]])
# Step 4: Prepare sequences for LSTM using parallel processing
print("Preparing sequences...")
batch_size = 518
array = df.values
sequences = []
for start in tqdm(range(0, len(array) - seq_len + 1, batch_size), desc="Preparing Sequences", unit="batch"):
sequence = array[start:start +seq_len]
sequences.append(sequence)
# Convert to PyTorch Tensor
print("Converting to PyTorch Tensor...")
data_tensor = torch.tensor(np.array(sequences), dtype=torch.float32)
return data_tensor, scaler, df