Advertisement
Guest User

xgboost higgs-numpy.py

a guest
Jul 5th, 2014
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. #!/usr/bin/python                                                                                                                                
  2. # this is the example script to use xgboost to train                                                                                            
  3. import inspect
  4. import os
  5. import sys
  6. import numpy as np
  7. # add path of xgboost python module                                                                                                              
  8. code_path = os.path.join(
  9.     os.path.split(inspect.getfile(inspect.currentframe()))[0], "../../python")
  10.  
  11. sys.path.append(code_path)
  12.  
  13. import xgboost as xgb
  14.  
  15. test_size = 550000
  16.  
  17. # path to where the data lies                                                                                                                    
  18. dpath = 'data'
  19.  
  20. # load in training data, directly use numpy                                                                                                      
  21. dtrain = np.loadtxt( dpath+'/training.csv', delimiter=',', skiprows=1, converters={32: lambda x:int(x=='s'.encode('utf-8')) } )
  22. print ('finish loading from csv ')
  23.  
  24. label  = dtrain[:,32]
  25. data   = dtrain[:,1:31]
  26. # rescale weight to make it same as test set                                                                                                    
  27. weight = dtrain[:,31] * float(test_size) / len(label)
  28.  
  29. sum_wpos = sum( weight[i] for i in range(len(label)) if label[i] == 1.0  )
  30. sum_wneg = sum( weight[i] for i in range(len(label)) if label[i] == 0.0  )
  31.  
  32. # print weight statistics                                                                                                                        
  33. print ('weight statistics: wpos=%g, wneg=%g, ratio=%g' % ( sum_wpos, sum_wneg, sum_wneg/sum_wpos ))
  34.  
  35. # construct xgboost.DMatrix from numpy array, treat -999.0 as missing value                                                                      
  36. xgmat = xgb.DMatrix( data, label=label, missing = -999.0, weight=weight )
  37.  
  38. # setup parameters for xgboost      
  39. param = {}
  40. # use logistic regression loss, use raw prediction before logistic transformation                                                                
  41. # since we only need the rank                                                                                                                    
  42. param['objective'] = 'binary:logitraw'
  43. # scale weight of positive examples                                                                                                              
  44. param['scale_pos_weight'] = sum_wneg/sum_wpos
  45. param['bst:eta'] = 0.025
  46. param['bst:max_depth'] = 6
  47. param['eval_metric'] = 'auc'
  48. param['silent'] = 1
  49. param['nthread'] = 32
  50.  
  51. # you can directly throw param in, though we want to watch multiple metrics here                                                                
  52. plst = list(param.items())+[('eval_metric', 'ams@0.15')]
  53.  
  54. watchlist = [ (xgmat,'train') ]
  55. # boost 120 tres                                                                                                                                
  56. num_round = 1100
  57. print ('loading data end, start to boost trees')
  58. bst = xgb.train( plst, xgmat, num_round, watchlist );
  59. # save out model                                                                                                                                
  60. bst.save_model('higgs.model')
  61.  
  62. print ('finish training')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement