KNN.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #ifndef _KNN_h_
  2. #define _KNN_h_
  3. /* KNN.h
  4. *
  5. * Copyright (C) 2007-2008 Ola Söder, 2017 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * os 20080529 Initial release
  22. * pb 2011/03/08 C++
  23. */
  24. /////////////////////////////////////////////////////
  25. // Praat datatypes //
  26. /////////////////////////////////////////////////////
  27. #include "Data.h"
  28. #include "PatternList.h"
  29. #include "Categories.h"
  30. #include "TableOfReal.h"
  31. #include "Permutation.h"
  32. #include "MDS.h"
  33. /////////////////////////////////////////////////////
  34. // KNN miscs //
  35. /////////////////////////////////////////////////////
  36. #include "OlaP.h"
  37. #include "FeatureWeights.h"
  38. #include "gsl_siman.h"
  39. /////////////////////////////////////////////////////
  40. // Praat specifics //
  41. /////////////////////////////////////////////////////
  42. #include "KNN_def.h"
  43. /////////////////////////////////////////////////////
  44. // Private definitions and macros //
  45. /////////////////////////////////////////////////////
  46. #define kOla_TEN_FOLD_CROSS_VALIDATION 1
  47. #define kOla_LEAVE_ONE_OUT 2
  48. #define kOla_SQUARED_DISTANCE_WEIGHTED_VOTING 8
  49. #define kOla_DISTANCE_WEIGHTED_VOTING 16
  50. #define kOla_FLAT_VOTING 32
  51. #define kOla_ERROR 0
  52. #define kOla_SUCCESS 303
  53. #define kOla_APPEND 1
  54. #define kOla_REPLACE 2
  55. #define kOla_SHUFFLE 1
  56. #define kOla_SEQUENTIAL 2
  57. #define kOla_PATTERN_CATEGORIES_MISMATCH 111
  58. #define kOla_DIMENSIONALITY_MISMATCH 222
  59. #define kOla_FWEIGHTS_MISMATCH 333
  60. /////////////////////////////////////////////////////
  61. // Prototypes //
  62. /////////////////////////////////////////////////////
  63. // a near-dummy function
  64. autoKNN KNN_create ();
  65. // Learning
  66. int KNN_learn
  67. (
  68. KNN me, // the classifier to be trained
  69. PatternList p, // source pattern
  70. Categories c, // target categories
  71. int method, // method <- REPLACE or APPEND
  72. int ordering // ordering <- SHUFFLE?
  73. );
  74. // Classification - To Categories
  75. autoCategories KNN_classifyToCategories
  76. (
  77. KNN me, // the classifier being used
  78. PatternList ps, // target pattern (where neighbours are sought for)
  79. FeatureWeights fws, // feature weights
  80. integer k, // the number of sought after neighbours
  81. int dist // distance weighting
  82. );
  83. // Classification - To Categories, threading aux
  84. void * KNN_classifyToCategoriesAux
  85. (
  86. void * input
  87. );
  88. // Classification - To TableOfReal
  89. autoTableOfReal KNN_classifyToTableOfReal
  90. (
  91. KNN me, PatternList ps, FeatureWeights fws, integer k, int dist
  92. );
  93. // Classification - To TableOfReal, threading aux
  94. void * KNN_classifyToTableOfRealAux
  95. (
  96. void * input
  97. );
  98. // Classification - To TableOfReal, all candidates
  99. autoTableOfReal KNN_classifyToTableOfRealAll
  100. (
  101. KNN me, // the classifier being used
  102. PatternList ps, // target pattern (where neighbours are sought for)
  103. FeatureWeights fws, // feature weights
  104. integer k, // the number of sought after neighbours
  105. int dist // distance weighting
  106. );
  107. // Classification - Folding
  108. autoCategories KNN_classifyFold
  109. (
  110. KNN me, // the classifier being used
  111. PatternList ps, // target pattern (where neighbours are sought for)
  112. FeatureWeights fws, // feature weights
  113. integer k, // the number of sought after neighbours
  114. int dist, // distance weighting
  115. integer begin, // fold start, inclusive [...
  116. integer end // fold end, inclusive ...]
  117. );
  118. // Evaluation
  119. double KNN_evaluate
  120. (
  121. KNN me, // the classifier being used
  122. FeatureWeights fws, // feature weights
  123. integer k, // the number of sought after neighbours
  124. int dist, // distance weighting
  125. int mode // TEN_FOLD_CROSS_VALIDATION / LEAVE_ONE_OUT
  126. );
  127. // Evaluation using a separate test set
  128. double KNN_evaluateWithTestSet
  129. (
  130. KNN me, // the classifier being used
  131. PatternList p, // the vectors of the test set
  132. Categories c, // the categories of the test set
  133. FeatureWeights fws, // feature weights
  134. integer k, // the number of sought after neighbours
  135. int dist // distance weighting
  136. );
  137. // Model search
  138. double KNN_modelSearch
  139. (
  140. KNN me, // the classifier being used
  141. FeatureWeights fws, // feature weights
  142. integer *k, // valid integer *, to hold the output value of k
  143. int *dist, // valid int *, to hold the output value dist_weight
  144. int mode, // evaluation mode
  145. double rate, // learning rate
  146. integer nseeds // the number of seeds to be used
  147. );
  148. // Euclidean distance
  149. double KNN_distanceEuclidean
  150. (
  151. PatternList ps, PatternList pt, FeatureWeights fws, integer rows, integer rowt
  152. );
  153. // Manhattan distance
  154. double KNN_distanceManhattan
  155. (
  156. PatternList ps, PatternList pt, integer rows, integer rowt
  157. );
  158. // Find longest distance
  159. integer KNN_max
  160. (
  161. double * distances, // an array of distances containing ...
  162. integer ndistances // ndistances distances
  163. );
  164. // Locate k neighbours, skip one + disposal of distance
  165. integer KNN_kNeighboursSkip
  166. (
  167. PatternList j, PatternList p, FeatureWeights fws, integer jy, integer k, integer *indices, integer skipper
  168. );
  169. // Locate the k nearest neighbours, exclude instances within the range defined
  170. // by [begin ... end]
  171. integer KNN_kNeighboursSkipRange
  172. (
  173. PatternList j, PatternList p, FeatureWeights fws, integer jy, integer k, integer *indices, double *distances, integer begin, integer end
  174. // range of excluded instances in the target
  175. // pattern
  176. );
  177. // Locate k neighbours
  178. integer KNN_kNeighbours
  179. (
  180. PatternList j, // source-pattern (where the unknown is located)
  181. PatternList p, // target pattern (where neighbours are sought for)
  182. FeatureWeights fws, // feature weights
  183. integer jy, // the index of the unknown instance in the source pattern
  184. integer k, // the number of sought after neighbours
  185. integer * indices, // a pointer to a memory-space big enough for k integers
  186. // representing indices to the k neighbours in the
  187. // target pattern
  188. double * distances // a pointer to a memory-space big enough for k
  189. // doubles representing the distances to the k
  190. // neighbours
  191. );
  192. // Locating k (nearest) friends
  193. integer KNN_kFriends
  194. (
  195. PatternList j, PatternList p, Categories c, integer jy, integer k, integer *indices
  196. // representing indices to the k friends in the
  197. // target pattern
  198. );
  199. // Computing the distance to the nearest enemy
  200. double KNN_nearestEnemy
  201. (
  202. PatternList j, PatternList p, Categories c, integer jy
  203. );
  204. // Computing the number of friends among k neighbours
  205. integer KNN_friendsAmongkNeighbours
  206. (
  207. PatternList j, PatternList p, Categories c, integer jy, integer k
  208. );
  209. // Locating k unique (nearest) enemies
  210. integer KNN_kUniqueEnemies
  211. (
  212. PatternList j, PatternList p, Categories c, integer jy, integer k, integer *indices
  213. // located enemies
  214. );
  215. // Compute dissimilarity matrix
  216. autoDissimilarity KNN_patternToDissimilarity
  217. (
  218. PatternList p, // PatternList
  219. FeatureWeights fws // Feature weights
  220. );
  221. // Compute frequencies
  222. integer KNN_kIndicesToFrequenciesAndDistances
  223. (
  224. Categories c, // Source categories
  225. integer k, // k (!)
  226. integer *indices, // In: indices
  227. double *distances, // Out: distances
  228. double *freqs, // Out: and frequencies (double, sic!)
  229. integer *freqindices // Out: and indices -> freqs.
  230. );
  231. // Normalize array
  232. void KNN_normalizeFloatArray
  233. (
  234. double * array, // Array to be normalized
  235. integer n // The number of elements
  236. // in the array
  237. );
  238. // Remove instance
  239. void KNN_removeInstance
  240. (
  241. KNN me, // Classifier
  242. integer y // Index of the instance to be purged
  243. //
  244. );
  245. // Shuffle instances
  246. void KNN_shuffleInstances
  247. (
  248. KNN me // Classifier whose instance
  249. // base is to be shuffled
  250. );
  251. // Experimental code
  252. autoPermutation KNN_SA_ToPermutation
  253. (
  254. KNN me, // the classifier being used
  255. integer tries, //
  256. integer iterations, //
  257. double step_size, //
  258. double boltzmann_c, //
  259. double temp_start, //
  260. double damping_f, //
  261. double temp_stop //
  262. //
  263. );
  264. // Experimental code
  265. typedef struct
  266. {
  267. PatternList p;
  268. integer *indices;
  269. } KNN_SA_t;
  270. // Experimental code
  271. double KNN_SA_t_energy
  272. (
  273. void * istruct
  274. );
  275. // Experimental code
  276. double KNN_SA_t_metric
  277. (
  278. void * istruct1,
  279. void * istruct2
  280. );
  281. // Experimental code
  282. void KNN_SA_t_print
  283. (
  284. void * istruct
  285. );
  286. // Experimental code
  287. void KNN_SA_t_step
  288. (
  289. const gsl_rng * r,
  290. void * istruct,
  291. double step_size
  292. );
  293. // Experimental code
  294. void KNN_SA_t_copy
  295. (
  296. void * istruct_src,
  297. void * istruct_dest
  298. );
  299. // Experimental code
  300. void * KNN_SA_t_copy_construct
  301. (
  302. void * istruct
  303. );
  304. // Experimental code
  305. KNN_SA_t * KNN_SA_t_create
  306. (
  307. PatternList p
  308. );
  309. // Experimental code
  310. void KNN_SA_t_destroy
  311. (
  312. void * istruct
  313. );
  314. // Experimental code
  315. void KNN_SA_partition
  316. (
  317. PatternList p,
  318. integer i1,
  319. integer i2,
  320. integer *result
  321. );
  322. // Compute feature weights (wrapper), evaluate using folding
  323. autoFeatureWeights FeatureWeights_computeWrapperInt
  324. (
  325. KNN me, // Classifier
  326. integer k, // k(!)
  327. int d, // distance weighting
  328. integer nseeds, // the number of seeds
  329. double alfa, // shrinkage factor
  330. double stop, // stop at
  331. int mode, // mode (co/serial)
  332. int emode // evaluation mode (10-fold/L1O)
  333. );
  334. // Compute feature weights (wrapper), evaluate using separate test set
  335. autoFeatureWeights FeatureWeights_computeWrapperExt
  336. (
  337. KNN nn, // Classifier
  338. PatternList pp, // test pattern
  339. Categories c, // test categories
  340. integer k, // k(!)
  341. int d, // distance weighting
  342. integer nseeds, // the number of seeds
  343. double alfa, // shrinkage factor
  344. double stop, // stop at
  345. int mode // mode (co/serial)
  346. );
  347. // Evaluate feature weights, wrapper aux.
  348. double FeatureWeights_evaluate
  349. (
  350. FeatureWeights fws, // Weights to evaluate
  351. KNN nn, // Classifier
  352. PatternList pp, // test pattern
  353. Categories c, // test categories
  354. integer k, // k(!)
  355. int d // distance weighting
  356. );
  357. /* End of file KNN.h */
  358. #endif