Custom Datasets Practice

In this lab, you will review how to make a prediction in several different ways by using PyTorch.
Author

Juma Shafara

Published

September 4, 2024

Keywords

Training Two Parameter, Mini-Batch Gradient Decent, Training Two Parameter Mini-Batch Gradient Decent

Photo by DATAIDEA
import pandas as pd
import dataidea_science as ds
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
boston_ = ds.loadDataset('boston')

Custom Dataset Class

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
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
Back to top