from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
import torch
import torchvision
from torch.utils.data import Dataset
import numpy as np


import requests
import pandas as pd
import io


# url = "https://raw.githubusercontent.com/python-engineer/pytorchTutorial/master/data/wine/wine.csv"
# download = requests.get(url).content

# df = pd.read_csv(io.StringIO(download.decode('utf-8')))
# df.to_csv("wine.csv")
df = pd.read_csv("/content/drive/MyDrive/Study/data/wine.csv")
df
Unnamed: 0 Wine Alcohol Malic.acid Ash Acl Mg Phenols Flavanoids Nonflavanoid.phenols Proanth Color.int Hue OD Proline
0 0 1 14.23 1.71 2.43 15.6 127 2.80 3.06 0.28 2.29 5.64 1.04 3.92 1065
1 1 1 13.20 1.78 2.14 11.2 100 2.65 2.76 0.26 1.28 4.38 1.05 3.40 1050
2 2 1 13.16 2.36 2.67 18.6 101 2.80 3.24 0.30 2.81 5.68 1.03 3.17 1185
3 3 1 14.37 1.95 2.50 16.8 113 3.85 3.49 0.24 2.18 7.80 0.86 3.45 1480
4 4 1 13.24 2.59 2.87 21.0 118 2.80 2.69 0.39 1.82 4.32 1.04 2.93 735
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
173 173 3 13.71 5.65 2.45 20.5 95 1.68 0.61 0.52 1.06 7.70 0.64 1.74 740
174 174 3 13.40 3.91 2.48 23.0 102 1.80 0.75 0.43 1.41 7.30 0.70 1.56 750
175 175 3 13.27 4.28 2.26 20.0 120 1.59 0.69 0.43 1.35 10.20 0.59 1.56 835
176 176 3 13.17 2.59 2.37 20.0 120 1.65 0.68 0.53 1.46 9.30 0.60 1.62 840
177 177 3 14.13 4.10 2.74 24.5 96 2.05 0.76 0.56 1.35 9.20 0.61 1.60 560

178 rows × 15 columns

class WineDataset(Dataset):
  def __init__(self, transform=None):
    xy = np.loadtxt("/content/drive/MyDrive/Study/data/wine.csv", delimiter=',', dtype=np.float32, skiprows=1)
    self.n_samples = xy.shape[0]

    self.x = xy[:, 1:]
    self.y = xy[:, [0]]

    self.transform = transform

  def __getitem__(self, index):
    sample = self.x[index], self.y[index]

    if self.transform:
      sample = self.transform(sample)
    
    return sample

  def __len__(self):
    return self.n_samples

class ToTensor:
  def __call__(self, sample):
    inputs, targets = sample
    return torch.from_numpy(inputs), torch.from_numpy(targets)


class MulTransform:
  def __init__(self, factor):
    self.factor = factor
  
  def __call__(self, sample):
    inputs, target = sample
    inputs *= self.factor
    return inputs, target
  
# dataset = WineDataset(transform=ToTensor())          # tensor로 바꿔줘야한다.
dataset = WineDataset()
first_data = dataset[0]
features, labels = first_data
print(features)
print(type(features), type(labels))
[1.000e+00 1.423e+01 1.710e+00 2.430e+00 1.560e+01 1.270e+02 2.800e+00
 3.060e+00 2.800e-01 2.290e+00 5.640e+00 1.040e+00 3.920e+00 1.065e+03]
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
composed = torchvision.transforms.Compose([ToTensor(), MulTransform(2)])
dataset = WineDataset(transform=composed)
first_data = dataset[0]
features, labels = first_data
print(features)
print(type(features), type(labels))
tensor([2.0000e+00, 2.8460e+01, 3.4200e+00, 4.8600e+00, 3.1200e+01, 2.5400e+02,
        5.6000e+00, 6.1200e+00, 5.6000e-01, 4.5800e+00, 1.1280e+01, 2.0800e+00,
        7.8400e+00, 2.1300e+03])
<class 'torch.Tensor'> <class 'torch.Tensor'>

댓글남기기