Advertisement
Guest User

split

a guest
Nov 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. #! /usr/bin/python3
  2.  
  3. from random import randint
  4.  
  5. f = open("leukemia.dat", "r")
  6.  
  7. lines = sum(1 for line in open('leukemia.dat'))
  8. # get 20% of toal lines
  9. tot = int((int(lines)*20)/100)
  10. rand_lines = []
  11.  
  12. for _ in range(tot):
  13.     #generate random value
  14.     n =  randint(1, lines)
  15.     while n in rand_lines:
  16.         n =  randint(1, lines)
  17.     # add to the list
  18.     rand_lines.append(n)
  19.  
  20. test = open("test.dat","w+")
  21. train = open("train.dat","w+")
  22.  
  23. i=0
  24. for x in f:
  25.     if i == 0:
  26.         test.write(x)
  27.         train.write(x)
  28.     elif i in rand_lines:
  29.         test.write(x)
  30.     else:
  31.         train.write(x)
  32.  
  33.     i += 1
  34.  
  35. print("Done!")
  36. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement