pkldataset
PKLDataset
¶
Bases: Dataset
A PyTorch Dataset Class for handling pickle (*.pkl) data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The filename of the pkl file. Defaults to “data.pkl”. |
'data.pkl'
|
directory |
str
|
The directory where the pkl file is located. Defaults to None. |
None
|
feature_type |
dtype
|
The data type of the features. Defaults to torch.float. |
float
|
target_column |
str
|
The name of the target column. Defaults to “y”. |
'y'
|
target_type |
dtype
|
The data type of the targets. Defaults to torch.long. |
float
|
train |
bool
|
Whether the dataset is for training or not. Defaults to True. |
True
|
rmNA |
bool
|
Whether to remove rows with NA values or not. Defaults to True. |
True
|
**desc |
Any
|
Additional arguments to be passed to the base class. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
filename |
str
|
The filename of the pkl file. |
directory |
str
|
The directory where the pkl file is located. |
feature_type |
dtype
|
The data type of the features. Defaults to torch.float. |
target_column |
str
|
The name of the target column. |
target_type |
dtype
|
The data type of the targets. Defaults to torch.float. |
train |
bool
|
Whether the dataset is for training or not. |
rmNA |
bool
|
Whether to remove rows with NA values or not. |
data |
Tensor
|
The features. |
targets |
Tensor
|
The targets. |
Notes
spotpython
comes with a sample pkl file, which is located atspotpython/data/pkldataset.pkl
.
Examples:
>>> from spotpython.data.pkldataset import PKLDataset
import torch
from torch.utils.data import DataLoader
dataset = PKLDataset(target_column='prognosis', feature_type=torch.long)
# Set batch size for DataLoader
batch_size = 5
# Create DataLoader
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
# Iterate over the data in the DataLoader
for batch in dataloader:
inputs, targets = batch
print(f"Batch Size: {inputs.size(0)}")
print("---------------")
print(f"Inputs: {inputs}")
print(f"Targets: {targets}")
break
Batch Size: 5
---------------
Inputs: tensor([[1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Targets: tensor([ 0, 1, 6, 9, 10])
>>> # Load the data from a different directory:
>>> # Similar to the above example, but with a different target column, full path, and different data type
>>> from spotpython.data.pkldataset import PKLDataset
import torch
from torch.utils.data import DataLoader
dataset = PKLDataset(directory="/Users/bartz/workspace/spotpython/notebooks/data/spotpython/",
filename="data_sensitive.pkl",
target_column='N',
feature_type=torch.float32,
target_type=torch.float32,
rmNA=True)
Source code in spotpython/data/pkldataset.py
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
__getitem__(idx)
¶
Returns the feature and target at the given index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx |
int
|
The index. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
A tuple containing the feature and target at the given index. |
Examples:
>>> from spotpython.data.pkldataset import PKLDataset
import torch
from torch.utils.data import DataLoader
dataset = PKLDataset(target_column='prognosis', feature_type=torch.long)
print(dataset.data.shape)
print(dataset.targets.shape)
torch.Size([11, 64])
torch.Size([11])
Source code in spotpython/data/pkldataset.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
__len__()
¶
Returns the length of the dataset.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The length of the dataset. |
Examples:
>>> from spotpython.data.pkldataset import PKLDataset
import torch
from torch.utils.data import DataLoader
dataset = PKLDataset(target_column='prognosis', feature_type=torch.long)
print(len(dataset))
11
Source code in spotpython/data/pkldataset.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
__ncols__()
¶
Returns the number of columns in the dataset.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of columns in the dataset. |
Examples:
>>> from spotpython.data.pkldataset import PKLDataset
import torch
from torch.utils.data import DataLoader
dataset = PKLDataset(target_column='prognosis', feature_type=torch.long)
print(dataset.__ncols__())
64
Source code in spotpython/data/pkldataset.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
extra_repr()
¶
Returns a string with the filename and directory of the dataset.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string with the filename and directory of the dataset. |
Examples:
>>> from spotpython.data.pkldataset import PKLDataset
import torch
from torch.utils.data import DataLoader
dataset = PKLDataset(target_column='prognosis', feature_type=torch.long)
print(dataset)
Source code in spotpython/data/pkldataset.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|