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
| def data_set_split(data_set, valid_ratio, seed): valid_data_size = int(len(data_set) * valid_ratio) train_data_size = len(data_set) - valid_data_size train_set, valid_set = random_split(data_set, [train_data_size, valid_data_size], generator=torch.Generator().manual_seed(seed)) return np.array(train_set), np.array(valid_set)
def select_feature(train_set, valid_set, test_set): label_train_set = train_set[:, -1] label_valid_set = valid_set[:, -1]
feature_train_set = train_set[:, :-1] feature_valid_set = valid_set[:, :-1] feature_test_set = test_set
return feature_train_set, feature_valid_set, feature_test_set, label_train_set, label_valid_set
class COVID19Dataset(Dataset): def __init__(self, features, targets=None): if targets is None: self.targets = targets else: self.targets = torch.FloatTensor(targets) self.features = torch.FloatTensor(features)
def __getitem__(self, idx): if self.targets is None: return self.features[idx] else: return self.features[idx], self.targets[idx]
def __len__(self): return len(self.features)
|