-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
34 lines (26 loc) · 964 Bytes
/
dataset.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
from typing import Dict
import torch
from torch.utils.data import Dataset
import pandas as pd
import numpy as np
class IntrusionDataset(Dataset):
def __init__(self, df: pd.DataFrame, target_name: str, labels_mapping: Dict[str, int]) -> None:
self.df = df
self.target_name = target_name
self.labels_mapping = labels_mapping
def __getitem__(self, index):
row = self.df.iloc[index, :]
features = row.drop(self.target_name)
label = row[self.target_name]
label = self.labels_mapping[label]
return features.to_numpy(dtype=np.float32), label
def __len__(self):
return len(self.df)
class NumpyDataset(Dataset):
def __init__(self, X: np.ndarray, y: np.ndarray) -> None:
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index, :].astype(np.float32), self.y[index]
def __len__(self):
return self.y.shape[0]