plot1dExampleClassification.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. % BRIEF: Small visualization script using GPHIKClassifier
  2. % author: Alexander Freytag
  3. % date: 20-01-2014 (dd-mm-yyyy)
  4. myData = [ 0.2; 0.6; 0.9];
  5. % create l1-normalized 'histograms'
  6. myData = cat(2,myData , 1-myData);
  7. myLabels = [1; 2; 2];
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. %% boolean
  10. %interested in time measurements?
  11. b_verboseTime = false;
  12. %interested in outputs?
  13. b_verbose = false;
  14. b_debug = false;
  15. % important for plotting!
  16. b_uncertaintyPredictionForClassification ...
  17. = true;
  18. b_optimize_noise = false;
  19. b_use_quantization = false;
  20. b_ils_verbose = false;
  21. %
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23. %% integer
  24. i_nrOfEigenvaluesToConsiderForVarApprox ...
  25. = 1;
  26. i_num_bins = 100; % default
  27. i_ils_max_iterations = 1000; % default
  28. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  29. %% double
  30. d_ils_min_delta = 1e-7; % default
  31. d_ils_min_residual = 1e-7; % default
  32. % model regularization
  33. d_noise = 0.000001;
  34. % adapt parameter bounds if you are interested in optimization
  35. d_parameter_lower_bound = 1.0;
  36. d_parameter_upper_bound = 1.0;
  37. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38. %% string
  39. s_ils_method = 'CG'; % default
  40. % options: 'none', 'greedy', 'downhillsimplex'
  41. s_optimization_method = 'downhillsimplex';
  42. % options: 'identity', 'exp', 'absexp'
  43. % with settings above, this equals 'identity'
  44. s_transform = 'identity';
  45. % options: 'exact', 'approximate_fine', 'approximate_rough', and 'none'
  46. s_varianceApproximation = 'approximate_fine';
  47. % init new GPHIKClassifier object
  48. myGPHIKClassifier = ...
  49. GPHIKClassifier ( ...
  50. 'verboseTime', b_verboseTime, ...
  51. 'verbose', b_verbose, ...
  52. 'debug', b_debug, ...
  53. 'uncertaintyPredictionForClassification', b_uncertaintyPredictionForClassification, ...
  54. 'optimize_noise', b_optimize_noise, ...
  55. 'use_quantization', b_use_quantization, ...
  56. 'ils_verbose', b_ils_verbose, ...
  57. ...
  58. 'nrOfEigenvaluesToConsiderForVarApprox', i_nrOfEigenvaluesToConsiderForVarApprox, ...
  59. 'num_bins', i_num_bins, ...
  60. 'ils_max_iterations', i_ils_max_iterations, ...
  61. ...
  62. 'ils_min_delta', d_ils_min_delta, ...
  63. 'ils_min_residual', d_ils_min_residual, ...
  64. 'noise', d_noise, ...
  65. 'parameter_lower_bound', d_parameter_lower_bound, ...
  66. 'parameter_upper_bound', d_parameter_upper_bound, ...
  67. ...
  68. 'ils_method', s_ils_method, ...
  69. 'optimization_method', s_optimization_method, ...
  70. 'transform', s_transform, ...
  71. 'varianceApproximation', s_varianceApproximation ...
  72. );
  73. %% run train method
  74. myGPHIKClassifier.train( myData, myLabels );
  75. %% evaluate model on test data
  76. myDataTest = 0:0.01:1;
  77. % create l1-normalized 'histograms'
  78. myDataTest = cat(1, myDataTest, 1-myDataTest)';
  79. scores = zeros(size(myDataTest,1),1);
  80. uncertainties = zeros(size(myDataTest,1),1);
  81. for i=1:size(myDataTest,1)
  82. example = myDataTest(i,:);
  83. % [ classNoEst, score, uncertainties(i)] = myGPHIKClassifier.classify( sparse(example) );
  84. [ classNoEst, score, uncertainties(i)] = myGPHIKClassifier.classify( example );
  85. % [ classNoEst, score] = myGPHIKClassifier.classify( example );
  86. % [ classNoEst, score] = myGPHIKClassifier.classify( sparse( example ) );
  87. scores(i) = score(1);
  88. end
  89. %% plot results
  90. % create figure and set title
  91. classificationFig = figure;
  92. set ( classificationFig, 'name', 'Classification with GPHIK');
  93. hold on;
  94. %#initialize x array
  95. x=myDataTest(:,1)';
  96. %#create first curve
  97. uncLower=scores-uncertainties;
  98. %#create second curve
  99. uncUpper=scores+uncertainties;
  100. %#create polygon-like x values for plotting
  101. X=[x,fliplr(x)];
  102. %# concatenate y-values accordingly
  103. Y=[uncLower',fliplr(uncUpper')];
  104. %#plot filled area
  105. fill(X,Y,'y');
  106. % plot mean values
  107. plot ( x,scores, ...
  108. 'LineStyle', '--', ...
  109. 'LineWidth', 2, ...
  110. 'Color', 'r', ...
  111. 'Marker','none', ...
  112. 'MarkerSize',1, ...
  113. 'MarkerEdgeColor','r', ...
  114. 'MarkerFaceColor',[0.5,0.5,0.5] ...
  115. );
  116. % plot training data
  117. plot ( myData(:,1), 2*(myLabels==1)-1, ...
  118. 'LineStyle', 'none', ...
  119. 'LineWidth', 3, ...
  120. 'Marker','o', ...
  121. 'MarkerSize',6, ...
  122. 'MarkerEdgeColor','b', ...
  123. 'MarkerFaceColor',[0.5,0.5,0.5] ...
  124. );
  125. xlabel('1st Input dimension');
  126. ylabel('Classification score');
  127. i_fontSizeAxis = 16;
  128. set(get(gca,'XLabel'), 'FontSize', i_fontSizeAxis);
  129. set(get(gca,'YLabel'), 'FontSize', i_fontSizeAxis);
  130. %% clean up and delete object
  131. myGPHIKClassifier.delete();
  132. clear ( 'myGPHIKClassifier' );