plot1dExampleClassification.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = true;
  20. b_ils_verbose = false;
  21. %
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23. %% integer
  24. i_nrOfEigenvaluesToConsiderForVarApprox ...
  25. = 1;
  26. i_num_bins = 1000; % 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: '1d-aequi-0-1' , '1d-aequi-0-max' or 'nd-aequi-0-1'
  43. s_quantType = '1d-aequi-0-1';
  44. % options: 'identity', 'exp', 'absexp'
  45. % with settings above, this equals 'identity'
  46. s_transform = 'identity';
  47. % options: 'exact', 'approximate_fine', 'approximate_rough', and 'none'
  48. s_varianceApproximation = 'approximate_fine';
  49. % init new GPHIKClassifier object
  50. myGPHIKClassifier = ...
  51. GPHIKClassifier ( ...
  52. 'verboseTime', b_verboseTime, ...
  53. 'verbose', b_verbose, ...
  54. 'debug', b_debug, ...
  55. 'uncertaintyPredictionForClassification', b_uncertaintyPredictionForClassification, ...
  56. 'optimize_noise', b_optimize_noise, ...
  57. 'use_quantization', b_use_quantization, ...
  58. 'ils_verbose', b_ils_verbose, ...
  59. ...
  60. 'nrOfEigenvaluesToConsiderForVarApprox', i_nrOfEigenvaluesToConsiderForVarApprox, ...
  61. 'num_bins', i_num_bins, ...
  62. 'ils_max_iterations', i_ils_max_iterations, ...
  63. ...
  64. 'ils_min_delta', d_ils_min_delta, ...
  65. 'ils_min_residual', d_ils_min_residual, ...
  66. 'noise', d_noise, ...
  67. 'parameter_lower_bound', d_parameter_lower_bound, ...
  68. 'parameter_upper_bound', d_parameter_upper_bound, ...
  69. ...
  70. 'ils_method', s_ils_method, ...
  71. 'optimization_method', s_optimization_method, ...
  72. 's_quantType', s_quantType, ...
  73. 'transform', s_transform, ...
  74. 'varianceApproximation', s_varianceApproximation ...
  75. );
  76. %% run train method
  77. myGPHIKClassifier.train( myData, myLabels );
  78. %% evaluate model on test data
  79. myDataTest = 0:0.001:1;
  80. % create l1-normalized 'histograms'
  81. myDataTest = cat(1, myDataTest, 1-myDataTest)';
  82. scores = zeros(size(myDataTest,1),1);
  83. uncertainties = zeros(size(myDataTest,1),1);
  84. for i=1:size(myDataTest,1)
  85. example = myDataTest(i,:);
  86. % [ classNoEst, score, uncertainties(i)] = myGPHIKClassifier.classify( sparse(example) );
  87. [ classNoEst, score, uncertainties(i)] = myGPHIKClassifier.classify( example );
  88. % [ classNoEst, score] = myGPHIKClassifier.classify( example );
  89. % [ classNoEst, score] = myGPHIKClassifier.classify( sparse( example ) );
  90. scores(i) = score(1);
  91. end
  92. %% plot results
  93. % create figure and set title
  94. classificationFig = figure;
  95. set ( classificationFig, 'name', 'Classification with GPHIK');
  96. hold on;
  97. %#initialize x array
  98. x=myDataTest(:,1)';
  99. %#create first curve
  100. uncLower=scores-uncertainties;
  101. %#create second curve
  102. uncUpper=scores+uncertainties;
  103. %#create polygon-like x values for plotting
  104. X=[x,fliplr(x)];
  105. %# concatenate y-values accordingly
  106. Y=[uncLower',fliplr(uncUpper')];
  107. %#plot filled area
  108. fill(X,Y,'y');
  109. % plot mean values
  110. plot ( x,scores, ...
  111. 'LineStyle', '--', ...
  112. 'LineWidth', 2, ...
  113. 'Color', 'r', ...
  114. 'Marker','none', ...
  115. 'MarkerSize',1, ...
  116. 'MarkerEdgeColor','r', ...
  117. 'MarkerFaceColor',[0.5,0.5,0.5] ...
  118. );
  119. % plot training data
  120. plot ( myData(:,1), 2*(myLabels==1)-1, ...
  121. 'LineStyle', 'none', ...
  122. 'LineWidth', 3, ...
  123. 'Marker','o', ...
  124. 'MarkerSize',6, ...
  125. 'MarkerEdgeColor','b', ...
  126. 'MarkerFaceColor',[0.5,0.5,0.5] ...
  127. );
  128. xlabel('1st Input dimension');
  129. ylabel('Classification score');
  130. i_fontSizeAxis = 16;
  131. set(get(gca,'XLabel'), 'FontSize', i_fontSizeAxis);
  132. set(get(gca,'YLabel'), 'FontSize', i_fontSizeAxis);
  133. %% clean up and delete object
  134. myGPHIKClassifier.delete();
  135. clear ( 'myGPHIKClassifier' );