Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import sklearn
  2. import numpy as np
  3. import math
  4. import pickle
  5. import collections
  6. class DGA:
  7. def __init__(self):
  8. self.model = { 'clf': pickle.loads(open('./dga_model_random_forest.model','rb').read())
  9. , 'alexa_vc': pickle.loads(open('./dga_model_alexa_vectorizor.model','rb').read())
  10. , 'alexa_counts': pickle.loads(open('./dga_model_alexa_counts.model','rb').read())
  11. , 'dict_vc': pickle.loads(open('./dga_model_dict_vectorizor.model','rb').read())
  12. , 'dict_counts': pickle.loads(open('./dga_model_dict_counts.model','rb').read()) }
  13.  
  14. def evaluate_domain(self, domain):
  15. alexa_match = self.model['alexa_counts'] * self.model['alexa_vc'].transform([domain]).T
  16. dict_match = self.model['dict_counts'] * self.model['dict_vc'].transform([domain]).T
  17.  
  18. # Assemble feature matrix (for just one domain)
  19. X = [len(domain), self.entropy(domain), alexa_match, dict_match]
  20. y_pred = self.model['clf'].predict([ X ])[0]
  21. return y_pred
  22.  
  23. def entropy(self, s):
  24. p, lns = collections.Counter(s), float(len(s))
  25. return -sum( count/lns * math.log(count/lns, 2) for count in p.values())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement