lolamontes69

Ch9 Ex3- Programming Collective Intelligence

Sep 14th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. """ Chapter 9 Exercise 3: Choosing the best kernel parameters.
  2.  
  3.    "Write a function that loops over different values for gamma and determines what the best value for a given dataset is."
  4.  
  5.    Gamma is the radius of RBF.
  6.    I wrote the function then learned how to use the LIBSVM included grid.py to optimize C and gamma. *see end of file
  7. """
  8.  
  9. import svm
  10. from svm import *
  11. import advancedclassify1 as advancedclassify
  12. numericalset=advancedclassify.loadnumerical()
  13. scaledset,scalef=advancedclassify.scaledata(numericalset) # libsvm needs scaled data
  14.  
  15. def optimize_gamma(scaledset):
  16.     answers,inputs=[r.match for r in scaledset],[r.data for r in scaledset]
  17.     cval = 0
  18.     low = 9999
  19.     for count in range(1,301,1):
  20.         valg = count/100.0
  21.         prob = svm_problem(answers, inputs)
  22.         param = svm_parameter(kernel_type = RBF, shrinking = 0, gamma = valg)
  23.         list1=[]
  24.         for a in range(10):
  25.             guesses = cross_validation(prob, param, 10)
  26.             b=sum([abs(answers[i]-guesses[i]) for i in range(len(guesses))])
  27.             list1.append(b)
  28.         total, count = 0, 0
  29.         for a in list1:
  30.             count+=1
  31.             total+=a
  32.         av =float(total)/count
  33.         if av<low:
  34.             low = av
  35.             gval = valg
  36.     print "Cost =",av
  37.     return gval
  38.  
  39. optimize_gamma(scaledset)
  40.  
  41. # Use the optimized gamma to train with
  42. answers,inputs=[r.match for r in scaledset],[r.data for r in scaledset]
  43. param = svm_parameter(kernel_type = RBF, gamma = 2.4)
  44. prob = svm_problem(answers, inputs)
  45. m = svm_model(prob,param)
  46.  
  47. newrow=[28.0,-1,-1,26.0,-1,1,2,0.8] # Man doesnt want kids woman does (??Gender stereotyping??)
  48. m.predict(scalef(newrow))
  49. newrow=[28.0,-1,1,26.0,-1,1,2,0.8]  # Both want children
  50. m.predict(scalef(newrow))
  51.  
  52. """
  53. Using grid.py
  54. *************
  55. First the data needs to be saved in the correct format, (easy enough to write a program to do this :3 .)
  56.  
  57.    0 1:0.65625 2:1 3:0 4:0.78125 5:0 6:1 7:0.0 8:0.0570524628059
  58.    1 1:0.15625 2:0 3:0 4:0.375 5:0 6:0 7:0.0 8:0.419948934927 .....
  59.  
  60. The first number is .match and the rest is indexed .data
  61.  
  62.    python grid.py -log2c -4,4,1 -log2g -3,0,1 -v 20 -m 300 answers_inputs_for_grid.txt
  63.  
  64. If -log2c, -log2g, or -v is not specified, default values are used. *run python grid.py for usage
  65.  
  66.  
  67.  
  68. What are gamma and C?
  69. *********************
  70. Gamma is the radius of RBF.
  71. -------------------------------------------------------------------------------
  72. C is the penalty parameter of the C-SVC model engine. If left empty, its default is 1.
  73. C is a trade-off between training error and the flatness of the solution.
  74.  
  75. The larger C is the less the final training error will be. But if you increase C too much you risk losing the generalization properties of the classifier, because it will try to fit as best as possible all the training points (including the possible errors of your dataset). In addition a large C, usually increases the time needed for training.
  76.  
  77. If C is small, then the classifier is flat (meaning that its derivatives are small - close to zero, at least for the gaussian rbf kernel this is substantiated theoretically). You have to find a C that keeps the training error small, but also generalizes well (i.e., it doesn't have large fluctuations). There are several methods to find the best possible C automatically, but you must keep in mind that this depends on the application you are interested in.
  78.  
  79. C = infinity will allow no misclassification
  80.  
  81.        (Copypasted)
  82.  
  83. """
Advertisement
Add Comment
Please, Sign In to add comment