在Python(Jupyter Notebook)中,我有一个数据集,其中包含5列,我已将其分为训练和测试数据集。我必须列出数据集的头部(前10行)和尾部(后10行)。我不允许使用NumPy,Pandas,SciPy和SciKit-Learn。我必须按如下方式列出格式化数据(column1,column2和column3是整数,column4和column5是浮点数):
column1 column 2 column3 column4 column5
1 0 5.0 male 200.4578
0 1 24.67 female 300.06
1 0 0.2 female 120.43
这是用于获取训练和测试数据集的代码:
random.seed(900001)
n = len(data['coumn1'])
train_flag = [random.random() < 2/3 for i in range(n)]
train = defaultdict(list)
test = defaultdict(list)
for i, flag in enumerate(train_flag):
if flag:
train['column1'].append(data['column1'][i])
train['column2'].append(data['column2'][i])
train['column3'].append(data['column3'][i])
train['column4'].append(data['column4'][i])
train['column5'].append(data['column5'][i])
else:
test['column1'].append(data['column1'][i])
test['column2'].append(data['column2'][i])
test['column3'].append(data['column3'][i])
test['column4'].append(data['column4'][i])
test['column5'].append(data['column5'][i])
非常感谢任何帮助。先谢谢你了