记录PaddlePaddle继承Dataset类实现自定义回归预测数据集的相关代码(Pytorch类似)
import paddle
from paddle.io import Dataset
from sklearn.model_selection import train_test_split
class MyDataset(Dataset):
def __init__(self, data,mode='train'):
super().__init__()
train_ratio = 0.8
test_ratio = 0.2
self.data = data
self.features = list(self.data.columns)[2:-1]
self.label = list(self.data.columns)[-1]
self.data = self.data.dropna()
self.train_data,self.test_data = train_test_split(self.data,
train_size=train_ratio,
test_size=test_ratio,
shuffle=False)
if mode == 'train':
self.data = self.train_data
if mode == 'test':
self.data = self.test_data
def __getitem__(self, index):
if index >= len(self):
raise ValueError("Index out of range")
record = self.data.iloc[index] #根据索引值指定行
features = [record[feature]for feature in self.features]
label = record[self.label]
features = paddle.to_tensor(features,dtype='float32')
label = paddle.to_tensor(label,dtype='float32')
return features,label
def __len__(self):
return len(self.data)
if __name__ == '__main__':
# 测试定义的数据集
data = pd.read_excel('./dataset/result.xlsx')
train_dataset = MyDataset(data, mode='train')
test_dataset = MyDataset(data, mode='test')
train_loader = paddle.io.DataLoader(train_dataset,batch_size=16,shuffle=False)
test_loader = paddle.io.DataLoader(test_dataset,batch_size=16,shuffle=False)
所测试数据集如下图所示:
此处需要指定特征及标签列