Custom Datasets
author: Juma Shafara date: "2024-09-04" title: Custom Datasets Practice keywords: [Training Two Parameter, Mini-Batch Gradient Decent, Training Two Parameter Mini-Batch Gradient Decent] description: In this lab, you will review how to make a prediction in several different ways by using PyTorch.¶
In [1]:
Copied!
import pandas as pd
import dataidea_science as ds
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import pandas as pd import dataidea_science as ds import torch from torch.utils.data import Dataset from torch.utils.data import DataLoader
In [2]:
Copied!
boston_ = ds.loadDataset('boston')
boston_ = ds.loadDataset('boston')
Custom Dataset Class¶
In [3]:
Copied!
class BostonDataset(Dataset):
def __init__(self):
# define our dataset
self.data = boston_
self.x = torch.tensor(self.data.drop('MEDV', axis=1).values, dtype=torch.float32)
self.y = torch.tensor(self.data.MEDV.values, dtype=torch.float32)
self.samples = self.data.shape[0]
def __getitem__(self, index):
# access samples
return self.x[index], self.y[index]
def __len__(self):
# len(dataset)
return self.samples
class BostonDataset(Dataset): def __init__(self): # define our dataset self.data = boston_ self.x = torch.tensor(self.data.drop('MEDV', axis=1).values, dtype=torch.float32) self.y = torch.tensor(self.data.MEDV.values, dtype=torch.float32) self.samples = self.data.shape[0] def __getitem__(self, index): # access samples return self.x[index], self.y[index] def __len__(self): # len(dataset) return self.samples
In [4]:
Copied!
boston_dataset = BostonDataset()
row_1 = boston_dataset[1]
print('Row 1 Features:', row_1[0])
print('Row 1 Outcome:', row_1[1])
length_ = len(boston_dataset)
print('Total Samples: ', length_)
boston_dataset = BostonDataset() row_1 = boston_dataset[1] print('Row 1 Features:', row_1[0]) print('Row 1 Outcome:', row_1[1]) length_ = len(boston_dataset) print('Total Samples: ', length_)
Row 1 Features: tensor([2.7310e-02, 0.0000e+00, 7.0700e+00, 0.0000e+00, 4.6900e-01, 6.4210e+00, 7.8900e+01, 4.9671e+00, 2.0000e+00, 2.4200e+02, 1.7800e+01, 3.9690e+02, 9.1400e+00]) Row 1 Outcome: tensor(21.6000) Total Samples: 506