Created
January 26, 2018 19:40
-
-
Save belljustin/5beda4fe7d3c2473b6332d11ceb75794 to your computer and use it in GitHub Desktop.
Script to split a dataset into an 80/20 training/test set by sampling from a binomial distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys | |
| import numpy.random as rand | |
| ''' | |
| Splits a dataset into an 80/20 train/test set by sampling from a binomial | |
| distribution with each datapoint deciding which set it belongs to. | |
| ''' | |
| if __name__ == '__main__': | |
| if len(sys.argv) != 4: | |
| print("Usage: splitdata inputFname trainFname testFname\n" + | |
| " Creates a random 80/20 train/test set") | |
| sys.exit(1) | |
| (fname, train_fname, test_fname) = sys.argv[1:] | |
| try: | |
| train = open(train_fname, 'w') | |
| test = open(test_fname, 'w') | |
| f = open(fname, 'r') | |
| for l in f: | |
| if rand.binomial(1, 0.8): | |
| train.write(l) | |
| else: | |
| test.write(l) | |
| finally: | |
| train.close() | |
| test.close() | |
| f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment