From 5055b6204ab3d724003aefab5dc6a871a96a3b85 Mon Sep 17 00:00:00 2001 From: dshahid380 <36432967+dshahid380@users.noreply.github.com> Date: Fri, 12 Apr 2019 21:01:26 +0530 Subject: [PATCH] Improvement in train_test_split function Improvement in train_test_split function Shuffling has been removed --- learning.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/learning.py b/learning.py index c30fa9b6e..e40d919fc 100644 --- a/learning.py +++ b/learning.py @@ -1049,13 +1049,25 @@ def grade_learner(predict, tests): return mean(int(predict(X) == y) for X, y in tests) -def train_test_split(dataset, start, end): - """Reserve dataset.examples[start:end] for test; train on the remainder.""" - start = int(start) - end = int(end) +def train_test_split(dataset, start = None, end = None, test_split = None): + """If you are giving 'start' and 'end' as parameters, + then it will return the testing set from index 'start' to 'end' + and the rest for training. + If you give 'test_split' as a parameter then it will return + test_split * 100% as the testing set and the rest as + training set. + """ examples = dataset.examples - train = examples[:start] + examples[end:] - val = examples[start:end] + if test_split == None: + train = examples[:start] + examples[end:] + val = examples[start:end] + else: + total_size = len(examples) + val_size = int(total_size * test_split) + train_size = total_size - val_size + train = examples[:train_size] + val = examples[train_size:total_size] + return train, val