Advertisement
Guest User

Untitled

a guest
May 20th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. import cPickle as pickle
  2. import random
  3. import hashlib
  4.  
  5. def ParamsID(a):
  6.     '''
  7.     Using hash function to build an unique identifier for each dictionary
  8.     '''
  9.     ID = 1
  10.     for x in a.keys():
  11.         ID = ID + int(hashlib.sha256(str(a[x]).encode('utf-8')).hexdigest(), 16)
  12.         ID = ID + int(hashlib.sha256(x.encode('utf-8')).hexdigest(), 16)
  13.  
  14.     return (ID % 10**10)
  15.  
  16. def getRandomParams():
  17.     '''
  18.     Returns a dictionary containing all the parameters for a BDT classifier
  19.     '''
  20.     p_lr = [1.0,0.1,0.01,0.001,0.0001]
  21.     p_n = [25,50,100,150,200,250,300,400,500]
  22.     p_max = [2,3,4,5,6,3]
  23.     p_leaves = [1,10,50,100,0.001,0.0001,1,1]
  24.            
  25.     mydic = {}
  26.     mydic['lr'] = random.choice(p_lr)
  27.     mydic['n'] = random.choice(p_n)
  28.     mydic['max'] = random.choice(p_max)
  29.     mydic['leaves'] = random.choice(p_leaves)
  30.  
  31.     return mydic
  32.  
  33. def _run():
  34.    
  35.     params = getRandomParams()
  36.     ID = ParamsID(params)                                       # Get an unique ID associated to the parameters
  37.     with open('models/params_' + str(ID) + '.pick','w') as f:   # Save the parameters into a file determined by unique ID
  38.         pickle.dump(params,f)
  39.    
  40.     results = run_algorithm(params)
  41.     save_results(params,ID)
  42.  
  43. if __name__ == '__main__':
  44.     for x in range(5):          # Runs the algorithm on 5 random sets of parameters
  45.         _run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement