Advertisement
Guest User

higgs-pred.py

a guest
Jul 5th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. #!/usr/bin/python                                                                                                                                
  2. # make prediction                                                                                                                                
  3. import sys
  4. import numpy as np
  5. # add path of xgboost python module                                                                                                              
  6. sys.path.append('../../python/')
  7. import xgboost as xgb
  8.  
  9. # path to where the data lies                                                                                                                    
  10. dpath = 'data'
  11.  
  12. modelfile = 'higgs.model'
  13. outfile = 'higgs.pred.csv'
  14. # make top 15% as positive                                                                                                                      
  15. threshold_ratio = 0.148
  16.  
  17. # load in training data, directly use numpy                                                                                                      
  18. dtest = np.loadtxt( dpath+'/test.csv', delimiter=',', skiprows=1 )
  19. data   = dtest[:,1:31]
  20. idx = dtest[:,0]
  21.  
  22. print ('finish loading from csv ')
  23. xgmat = xgb.DMatrix( data, missing = -999.0 )
  24. bst = xgb.Booster({'nthread':32})
  25. bst.load_model( modelfile )
  26. ypred = bst.predict( xgmat )
  27.  
  28. res  = [ ( int(idx[i]), ypred[i] ) for i in range(len(ypred)) ]
  29.  
  30. rorder = {}
  31. for k, v in sorted( res, key = lambda x:-x[1] ):
  32.     rorder[ k ] = len(rorder) + 1
  33.  
  34. # write out predictions                                                                                                                          
  35. ntop = int( threshold_ratio * len(rorder ) )
  36. fo = open(outfile, 'w')
  37. nhit = 0
  38. ntot = 0
  39. fo.write('EventId,RankOrder,Class\n')
  40. for k, v in res:
  41.     if rorder[k] <= ntop:
  42.         lb = 's'
  43.         nhit += 1
  44.     else:
  45.         lb = 'b'
  46.     # change output rank order to follow Kaggle convention                                                                                      
  47.     fo.write('%s,%d,%s\n' % ( k,  len(rorder)+1-rorder[k], lb ) )
  48.     ntot += 1
  49. fo.close()
  50.  
  51. print ('finished writing into prediction file')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement