find_svm_params.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/python
  2. from cPickle import load
  3. from Classifier import Classifier
  4. C = [float(2 ** p) for p in xrange(-5, 16, 2)]
  5. Y = [float(2 ** p) for p in xrange(-15, 4, 2)]
  6. best_classifier = None
  7. print 'Loading learning set...'
  8. learning_set = load(file('learning_set.dat', 'r'))
  9. print 'Learning set:', [c.value for c in learning_set]
  10. print 'Loading test set...'
  11. test_set = load(file('test_set.dat', 'r'))
  12. print 'Test set:', [c.value for c in test_set]
  13. # Perform a grid-search on different combinations of soft margin and gamma
  14. results = []
  15. best = (0,)
  16. i = 0
  17. for c in C:
  18. for y in Y:
  19. classifier = Classifier(c=c, gamma=y)
  20. classifier.train(learning_set)
  21. result = classifier.test(test_set)
  22. if result > best[0]:
  23. best = (result, c, y, classifier)
  24. results.append(result)
  25. i += 1
  26. print '%d of %d, c = %f, gamma = %f, result = %d%%' \
  27. % (i, len(C) * len(Y), c, y, int(round(result * 100)))
  28. i = 0
  29. print '\n c\y',
  30. for y in Y:
  31. print '| %f' % y,
  32. print
  33. for c in C:
  34. print ' %7s' % c,
  35. for y in Y:
  36. print '| %8d' % int(round(results[i] * 100)),
  37. i += 1
  38. print
  39. print '\nBest result: %.3f%% for C = %f and gamma = %f' % best[:3]
  40. best[3].save('classifier.dat')