nlptoolkit-classification 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (310) hide show
  1. package/README.md +1 -0
  2. package/dist/Attribute/DiscreteAttribute.d.ts +1 -1
  3. package/dist/Attribute/DiscreteAttribute.js.map +1 -1
  4. package/dist/Attribute/DiscreteIndexedAttribute.d.ts +2 -2
  5. package/dist/DataSet/DataDefinition.d.ts +11 -0
  6. package/dist/DataSet/DataDefinition.js +11 -0
  7. package/dist/DataSet/DataDefinition.js.map +1 -1
  8. package/dist/Experiment/BootstrapRun.js +2 -2
  9. package/dist/Experiment/BootstrapRun.js.map +1 -1
  10. package/dist/Experiment/Experiment.d.ts +7 -7
  11. package/dist/Experiment/Experiment.js +6 -6
  12. package/dist/Experiment/Experiment.js.map +1 -1
  13. package/dist/Experiment/KFoldRun.d.ts +17 -2
  14. package/dist/Experiment/KFoldRun.js +19 -4
  15. package/dist/Experiment/KFoldRun.js.map +1 -1
  16. package/dist/Experiment/KFoldRunSeparateTest.d.ts +13 -2
  17. package/dist/Experiment/KFoldRunSeparateTest.js +15 -4
  18. package/dist/Experiment/KFoldRunSeparateTest.js.map +1 -1
  19. package/dist/Experiment/MxKFoldRun.js +1 -1
  20. package/dist/Experiment/MxKFoldRun.js.map +1 -1
  21. package/dist/Experiment/MxKFoldRunSeparateTest.js +1 -1
  22. package/dist/Experiment/MxKFoldRunSeparateTest.js.map +1 -1
  23. package/dist/Experiment/SingleRunWithK.d.ts +18 -3
  24. package/dist/Experiment/SingleRunWithK.js +18 -3
  25. package/dist/Experiment/SingleRunWithK.js.map +1 -1
  26. package/dist/Experiment/StratifiedKFoldRun.js +1 -1
  27. package/dist/Experiment/StratifiedKFoldRun.js.map +1 -1
  28. package/dist/Experiment/StratifiedKFoldRunSeparateTest.js +1 -1
  29. package/dist/Experiment/StratifiedKFoldRunSeparateTest.js.map +1 -1
  30. package/dist/Experiment/StratifiedMxKFoldRun.js +1 -1
  31. package/dist/Experiment/StratifiedMxKFoldRun.js.map +1 -1
  32. package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js +3 -3
  33. package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js.map +1 -1
  34. package/dist/Experiment/StratifiedSingleRunWithK.js +1 -1
  35. package/dist/Experiment/StratifiedSingleRunWithK.js.map +1 -1
  36. package/dist/Filter/LaryFilter.d.ts +1 -1
  37. package/dist/Filter/LaryFilter.js +1 -1
  38. package/dist/Filter/TrainedFeatureFilter.d.ts +1 -1
  39. package/dist/Filter/TrainedFeatureFilter.js +1 -1
  40. package/dist/InstanceList/Partition.d.ts +45 -0
  41. package/dist/InstanceList/Partition.js +125 -70
  42. package/dist/InstanceList/Partition.js.map +1 -1
  43. package/dist/Model/DecisionTree/DecisionCondition.d.ts +8 -0
  44. package/dist/Model/DecisionTree/DecisionCondition.js +8 -0
  45. package/dist/Model/DecisionTree/DecisionCondition.js.map +1 -1
  46. package/dist/Model/DecisionTree/DecisionNode.d.ts +39 -1
  47. package/dist/Model/DecisionTree/DecisionNode.js +64 -22
  48. package/dist/Model/DecisionTree/DecisionNode.js.map +1 -1
  49. package/dist/Model/DecisionTree/DecisionStump.d.ts +17 -0
  50. package/dist/{Classifier/C45Stump.js → Model/DecisionTree/DecisionStump.js} +13 -10
  51. package/dist/Model/DecisionTree/DecisionStump.js.map +1 -0
  52. package/dist/Model/DecisionTree/DecisionTree.d.ts +24 -9
  53. package/dist/Model/DecisionTree/DecisionTree.js +39 -16
  54. package/dist/Model/DecisionTree/DecisionTree.js.map +1 -1
  55. package/dist/Model/DummyModel.d.ts +28 -3
  56. package/dist/Model/DummyModel.js +36 -23
  57. package/dist/Model/DummyModel.js.map +1 -1
  58. package/dist/{Classifier/Bagging.d.ts → Model/Ensemble/BaggingModel.d.ts} +8 -4
  59. package/dist/{Classifier/Bagging.js → Model/Ensemble/BaggingModel.js} +16 -14
  60. package/dist/Model/Ensemble/BaggingModel.js.map +1 -0
  61. package/dist/{Classifier/RandomForest.d.ts → Model/Ensemble/RandomForestModel.d.ts} +8 -4
  62. package/dist/{Classifier/RandomForest.js → Model/Ensemble/RandomForestModel.js} +16 -14
  63. package/dist/Model/Ensemble/RandomForestModel.js.map +1 -0
  64. package/dist/Model/Ensemble/TreeEnsembleModel.d.ts +32 -0
  65. package/dist/Model/{TreeEnsembleModel.js → Ensemble/TreeEnsembleModel.js} +23 -17
  66. package/dist/Model/Ensemble/TreeEnsembleModel.js.map +1 -0
  67. package/dist/Model/Model.d.ts +52 -0
  68. package/dist/Model/Model.js +84 -1
  69. package/dist/Model/Model.js.map +1 -1
  70. package/dist/Model/NeuralNetwork/DeepNetworkModel.d.ts +47 -0
  71. package/dist/Model/{DeepNetworkModel.js → NeuralNetwork/DeepNetworkModel.js} +55 -55
  72. package/dist/Model/NeuralNetwork/DeepNetworkModel.js.map +1 -0
  73. package/dist/Model/NeuralNetwork/LinearPerceptronModel.d.ts +31 -0
  74. package/dist/Model/NeuralNetwork/LinearPerceptronModel.js +84 -0
  75. package/dist/Model/NeuralNetwork/LinearPerceptronModel.js.map +1 -0
  76. package/dist/Model/NeuralNetwork/MultiLayerPerceptronModel.d.ts +39 -0
  77. package/dist/Model/{MultiLayerPerceptronModel.js → NeuralNetwork/MultiLayerPerceptronModel.js} +41 -37
  78. package/dist/Model/NeuralNetwork/MultiLayerPerceptronModel.js.map +1 -0
  79. package/dist/Model/{NeuralNetworkModel.d.ts → NeuralNetwork/NeuralNetworkModel.d.ts} +19 -5
  80. package/dist/Model/{NeuralNetworkModel.js → NeuralNetwork/NeuralNetworkModel.js} +19 -6
  81. package/dist/Model/NeuralNetwork/NeuralNetworkModel.js.map +1 -0
  82. package/dist/Model/{KnnInstance.d.ts → NonParametric/KnnInstance.d.ts} +1 -1
  83. package/dist/Model/NonParametric/KnnInstance.js.map +1 -0
  84. package/dist/Model/{KnnModel.d.ts → NonParametric/KnnModel.d.ts} +30 -6
  85. package/dist/Model/{KnnModel.js → NonParametric/KnnModel.js} +42 -19
  86. package/dist/Model/NonParametric/KnnModel.js.map +1 -0
  87. package/dist/Model/{GaussianModel.d.ts → Parametric/GaussianModel.d.ts} +20 -2
  88. package/dist/Model/{GaussianModel.js → Parametric/GaussianModel.js} +21 -3
  89. package/dist/Model/Parametric/GaussianModel.js.map +1 -0
  90. package/dist/Model/Parametric/KMeansModel.d.ts +36 -0
  91. package/dist/Model/Parametric/KMeansModel.js +73 -0
  92. package/dist/Model/Parametric/KMeansModel.js.map +1 -0
  93. package/dist/Model/Parametric/LdaModel.d.ts +44 -0
  94. package/dist/Model/Parametric/LdaModel.js +98 -0
  95. package/dist/Model/Parametric/LdaModel.js.map +1 -0
  96. package/dist/Model/{NaiveBayesModel.d.ts → Parametric/NaiveBayesModel.d.ts} +30 -10
  97. package/dist/Model/{NaiveBayesModel.js → Parametric/NaiveBayesModel.js} +64 -27
  98. package/dist/Model/Parametric/NaiveBayesModel.js.map +1 -0
  99. package/dist/Model/Parametric/QdaModel.d.ts +30 -0
  100. package/dist/Model/Parametric/QdaModel.js +84 -0
  101. package/dist/Model/Parametric/QdaModel.js.map +1 -0
  102. package/dist/Model/RandomModel.d.ts +32 -2
  103. package/dist/Model/RandomModel.js +48 -16
  104. package/dist/Model/RandomModel.js.map +1 -1
  105. package/dist/Parameter/C45Parameter.d.ts +2 -2
  106. package/dist/Parameter/DeepNetworkParameter.d.ts +2 -2
  107. package/dist/Parameter/KnnParameter.d.ts +1 -1
  108. package/dist/Parameter/LinearPerceptronParameter.d.ts +4 -4
  109. package/dist/Parameter/MultiLayerPerceptronParameter.d.ts +2 -2
  110. package/dist/Parameter/Parameter.d.ts +1 -1
  111. package/dist/Performance/ConfusionMatrix.d.ts +1 -1
  112. package/dist/StatisticalTest/Combined5x2F.d.ts +12 -0
  113. package/dist/StatisticalTest/Combined5x2F.js +12 -0
  114. package/dist/StatisticalTest/Combined5x2F.js.map +1 -1
  115. package/dist/StatisticalTest/Combined5x2t.d.ts +13 -0
  116. package/dist/StatisticalTest/Combined5x2t.js +13 -0
  117. package/dist/StatisticalTest/Combined5x2t.js.map +1 -1
  118. package/dist/StatisticalTest/Paired5x2t.d.ts +12 -0
  119. package/dist/StatisticalTest/Paired5x2t.js +12 -0
  120. package/dist/StatisticalTest/Paired5x2t.js.map +1 -1
  121. package/dist/StatisticalTest/PairedTest.d.ts +12 -0
  122. package/dist/StatisticalTest/PairedTest.js +12 -0
  123. package/dist/StatisticalTest/PairedTest.js.map +1 -1
  124. package/dist/StatisticalTest/Pairedt.d.ts +12 -0
  125. package/dist/StatisticalTest/Pairedt.js +12 -0
  126. package/dist/StatisticalTest/Pairedt.js.map +1 -1
  127. package/dist/StatisticalTest/Sign.d.ts +17 -0
  128. package/dist/StatisticalTest/Sign.js +17 -0
  129. package/dist/StatisticalTest/Sign.js.map +1 -1
  130. package/dist/StatisticalTest/StatisticalTestResult.d.ts +30 -0
  131. package/dist/StatisticalTest/StatisticalTestResult.js +30 -0
  132. package/dist/StatisticalTest/StatisticalTestResult.js.map +1 -1
  133. package/dist/index.d.ts +88 -0
  134. package/dist/index.js +111 -0
  135. package/dist/index.js.map +1 -0
  136. package/models/bagging-bupa.txt +10346 -0
  137. package/models/bagging-car.txt +40752 -0
  138. package/models/bagging-dermatology.txt +2990 -0
  139. package/models/bagging-iris.txt +1332 -0
  140. package/models/bagging-tictactoe.txt +29442 -0
  141. package/models/c45-bupa.txt +3 -0
  142. package/models/c45-car.txt +212 -0
  143. package/models/c45-carIndexed.txt +92 -0
  144. package/models/c45-dermatology.txt +22 -0
  145. package/models/c45-iris.txt +7 -0
  146. package/models/c45-tictactoe.txt +147 -0
  147. package/models/c45-tictactoeIndexed.txt +79 -0
  148. package/models/c45stump-bupa.txt +6 -0
  149. package/models/c45stump-car.txt +11 -0
  150. package/models/c45stump-chess.txt +35 -0
  151. package/models/c45stump-dermatology.txt +11 -0
  152. package/models/c45stump-iris.txt +5 -0
  153. package/models/c45stump-nursery.txt +10 -0
  154. package/models/c45stump-tictactoe.txt +9 -0
  155. package/models/randomforest-bupa.txt +10346 -0
  156. package/models/randomforest-car.txt +40752 -0
  157. package/models/randomforest-carIndexed.txt +15594 -0
  158. package/models/randomforest-dermatology.txt +2990 -0
  159. package/models/randomforest-iris.txt +1332 -0
  160. package/models/randomforest-tictactoe.txt +29442 -0
  161. package/package.json +7 -7
  162. package/source/Attribute/DiscreteAttribute.ts +1 -1
  163. package/source/Attribute/DiscreteIndexedAttribute.ts +2 -2
  164. package/source/DataSet/DataDefinition.ts +11 -0
  165. package/source/Experiment/BootstrapRun.ts +2 -2
  166. package/source/Experiment/Experiment.ts +10 -10
  167. package/source/Experiment/KFoldRun.ts +20 -5
  168. package/source/Experiment/KFoldRunSeparateTest.ts +16 -5
  169. package/source/Experiment/MxKFoldRun.ts +1 -1
  170. package/source/Experiment/MxKFoldRunSeparateTest.ts +1 -1
  171. package/source/Experiment/SingleRunWithK.ts +20 -5
  172. package/source/Experiment/StratifiedKFoldRun.ts +1 -1
  173. package/source/Experiment/StratifiedKFoldRunSeparateTest.ts +1 -1
  174. package/source/Experiment/StratifiedMxKFoldRun.ts +1 -1
  175. package/source/Experiment/StratifiedMxKFoldRunSeparateTest.ts +3 -3
  176. package/source/Experiment/StratifiedSingleRunWithK.ts +1 -1
  177. package/source/Filter/LaryFilter.ts +1 -1
  178. package/source/Filter/TrainedFeatureFilter.ts +1 -1
  179. package/source/InstanceList/Partition.ts +125 -65
  180. package/source/Model/DecisionTree/DecisionCondition.ts +8 -0
  181. package/source/Model/DecisionTree/DecisionNode.ts +65 -22
  182. package/source/Model/DecisionTree/DecisionStump.ts +26 -0
  183. package/source/Model/DecisionTree/DecisionTree.ts +44 -15
  184. package/source/Model/DummyModel.ts +40 -19
  185. package/source/{Classifier/Bagging.ts → Model/Ensemble/BaggingModel.ts} +15 -12
  186. package/source/{Classifier/RandomForest.ts → Model/Ensemble/RandomForestModel.ts} +14 -12
  187. package/source/Model/{TreeEnsembleModel.ts → Ensemble/TreeEnsembleModel.ts} +26 -18
  188. package/source/Model/Model.ts +92 -0
  189. package/source/Model/{DeepNetworkModel.ts → NeuralNetwork/DeepNetworkModel.ts} +59 -56
  190. package/source/Model/NeuralNetwork/LinearPerceptronModel.ts +81 -0
  191. package/source/Model/{MultiLayerPerceptronModel.ts → NeuralNetwork/MultiLayerPerceptronModel.ts} +46 -39
  192. package/source/Model/{NeuralNetworkModel.ts → NeuralNetwork/NeuralNetworkModel.ts} +20 -7
  193. package/source/Model/{KnnInstance.ts → NonParametric/KnnInstance.ts} +1 -1
  194. package/source/Model/{KnnModel.ts → NonParametric/KnnModel.ts} +48 -19
  195. package/source/Model/{GaussianModel.ts → Parametric/GaussianModel.ts} +21 -3
  196. package/source/Model/Parametric/KMeansModel.ts +72 -0
  197. package/source/Model/Parametric/LdaModel.ts +98 -0
  198. package/source/Model/{NaiveBayesModel.ts → Parametric/NaiveBayesModel.ts} +68 -25
  199. package/source/Model/Parametric/QdaModel.ts +83 -0
  200. package/source/Model/RandomModel.ts +54 -16
  201. package/source/Parameter/C45Parameter.ts +2 -2
  202. package/source/Parameter/DeepNetworkParameter.ts +2 -2
  203. package/source/Parameter/KnnParameter.ts +1 -1
  204. package/source/Parameter/LinearPerceptronParameter.ts +4 -4
  205. package/source/Parameter/MultiLayerPerceptronParameter.ts +2 -2
  206. package/source/Parameter/Parameter.ts +1 -1
  207. package/source/Performance/ConfusionMatrix.ts +1 -1
  208. package/source/StatisticalTest/Combined5x2F.ts +12 -0
  209. package/source/StatisticalTest/Combined5x2t.ts +13 -0
  210. package/source/StatisticalTest/Paired5x2t.ts +12 -0
  211. package/source/StatisticalTest/PairedTest.ts +12 -0
  212. package/source/StatisticalTest/Pairedt.ts +12 -0
  213. package/source/StatisticalTest/Sign.ts +17 -0
  214. package/source/StatisticalTest/StatisticalTestResult.ts +30 -0
  215. package/source/index.ts +88 -0
  216. package/source/tsconfig.json +1 -1
  217. package/tests/Classifier/C45Test.ts +2 -2
  218. package/tests/Classifier/DeepNetworkTest.ts +3 -5
  219. package/tests/Classifier/DummyTest.ts +2 -2
  220. package/tests/Classifier/KMeansTest.ts +2 -2
  221. package/tests/Classifier/KnnTest.ts +2 -2
  222. package/tests/Classifier/LdaTest.ts +2 -2
  223. package/tests/Classifier/LinearPerceptronTest.ts +2 -2
  224. package/tests/Classifier/MultiLayerPerceptronTest.ts +2 -3
  225. package/tests/Classifier/NaiveBayesTest.ts +2 -2
  226. package/tests/Classifier/QdaTest.ts +2 -2
  227. package/tests/Classifier/RandomForestTest.ts +2 -2
  228. package/tsconfig.json +1 -2
  229. package/dist/Classifier/Bagging.js.map +0 -1
  230. package/dist/Classifier/C45.d.ts +0 -14
  231. package/dist/Classifier/C45.js +0 -43
  232. package/dist/Classifier/C45.js.map +0 -1
  233. package/dist/Classifier/C45Stump.d.ts +0 -13
  234. package/dist/Classifier/C45Stump.js.map +0 -1
  235. package/dist/Classifier/Classifier.d.ts +0 -40
  236. package/dist/Classifier/Classifier.js +0 -72
  237. package/dist/Classifier/Classifier.js.map +0 -1
  238. package/dist/Classifier/DeepNetwork.d.ts +0 -14
  239. package/dist/Classifier/DeepNetwork.js +0 -34
  240. package/dist/Classifier/DeepNetwork.js.map +0 -1
  241. package/dist/Classifier/Dummy.d.ts +0 -14
  242. package/dist/Classifier/Dummy.js +0 -32
  243. package/dist/Classifier/Dummy.js.map +0 -1
  244. package/dist/Classifier/KMeans.d.ts +0 -13
  245. package/dist/Classifier/KMeans.js +0 -39
  246. package/dist/Classifier/KMeans.js.map +0 -1
  247. package/dist/Classifier/Knn.d.ts +0 -14
  248. package/dist/Classifier/Knn.js +0 -32
  249. package/dist/Classifier/Knn.js.map +0 -1
  250. package/dist/Classifier/Lda.d.ts +0 -13
  251. package/dist/Classifier/Lda.js +0 -55
  252. package/dist/Classifier/Lda.js.map +0 -1
  253. package/dist/Classifier/LinearPerceptron.d.ts +0 -15
  254. package/dist/Classifier/LinearPerceptron.js +0 -35
  255. package/dist/Classifier/LinearPerceptron.js.map +0 -1
  256. package/dist/Classifier/MultiLayerPerceptron.d.ts +0 -15
  257. package/dist/Classifier/MultiLayerPerceptron.js +0 -35
  258. package/dist/Classifier/MultiLayerPerceptron.js.map +0 -1
  259. package/dist/Classifier/NaiveBayes.d.ts +0 -26
  260. package/dist/Classifier/NaiveBayes.js +0 -70
  261. package/dist/Classifier/NaiveBayes.js.map +0 -1
  262. package/dist/Classifier/Qda.d.ts +0 -13
  263. package/dist/Classifier/Qda.js +0 -53
  264. package/dist/Classifier/Qda.js.map +0 -1
  265. package/dist/Classifier/RandomClassifier.d.ts +0 -13
  266. package/dist/Classifier/RandomClassifier.js +0 -35
  267. package/dist/Classifier/RandomClassifier.js.map +0 -1
  268. package/dist/Classifier/RandomForest.js.map +0 -1
  269. package/dist/Model/DeepNetworkModel.d.ts +0 -45
  270. package/dist/Model/DeepNetworkModel.js.map +0 -1
  271. package/dist/Model/GaussianModel.js.map +0 -1
  272. package/dist/Model/KMeansModel.d.ts +0 -28
  273. package/dist/Model/KMeansModel.js +0 -61
  274. package/dist/Model/KMeansModel.js.map +0 -1
  275. package/dist/Model/KnnInstance.js.map +0 -1
  276. package/dist/Model/KnnModel.js.map +0 -1
  277. package/dist/Model/LdaModel.d.ts +0 -28
  278. package/dist/Model/LdaModel.js +0 -67
  279. package/dist/Model/LdaModel.js.map +0 -1
  280. package/dist/Model/LinearPerceptronModel.d.ts +0 -24
  281. package/dist/Model/LinearPerceptronModel.js +0 -91
  282. package/dist/Model/LinearPerceptronModel.js.map +0 -1
  283. package/dist/Model/MultiLayerPerceptronModel.d.ts +0 -33
  284. package/dist/Model/MultiLayerPerceptronModel.js.map +0 -1
  285. package/dist/Model/NaiveBayesModel.js.map +0 -1
  286. package/dist/Model/NeuralNetworkModel.js.map +0 -1
  287. package/dist/Model/QdaModel.d.ts +0 -27
  288. package/dist/Model/QdaModel.js +0 -63
  289. package/dist/Model/QdaModel.js.map +0 -1
  290. package/dist/Model/TreeEnsembleModel.d.ts +0 -22
  291. package/dist/Model/TreeEnsembleModel.js.map +0 -1
  292. package/index.js +0 -100
  293. package/source/Classifier/C45.ts +0 -34
  294. package/source/Classifier/C45Stump.ts +0 -23
  295. package/source/Classifier/Classifier.ts +0 -72
  296. package/source/Classifier/DeepNetwork.ts +0 -26
  297. package/source/Classifier/Dummy.ts +0 -23
  298. package/source/Classifier/KMeans.ts +0 -30
  299. package/source/Classifier/Knn.ts +0 -25
  300. package/source/Classifier/Lda.ts +0 -47
  301. package/source/Classifier/LinearPerceptron.ts +0 -27
  302. package/source/Classifier/MultiLayerPerceptron.ts +0 -27
  303. package/source/Classifier/NaiveBayes.ts +0 -66
  304. package/source/Classifier/Qda.ts +0 -46
  305. package/source/Classifier/RandomClassifier.ts +0 -26
  306. package/source/Model/KMeansModel.ts +0 -56
  307. package/source/Model/LdaModel.ts +0 -62
  308. package/source/Model/LinearPerceptronModel.ts +0 -83
  309. package/source/Model/QdaModel.ts +0 -57
  310. /package/dist/Model/{KnnInstance.js → NonParametric/KnnInstance.js} +0 -0
@@ -3,12 +3,21 @@ import {CounterHashMap} from "nlptoolkit-datastructure/dist/CounterHashMap";
3
3
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
4
4
  import {InstanceList} from "../InstanceList/InstanceList";
5
5
  import {Matrix} from "nlptoolkit-math/dist/Matrix";
6
+ import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
7
+ import {Parameter} from "../Parameter/Parameter";
8
+ import {DiscreteAttribute} from "../Attribute/DiscreteAttribute";
9
+ import {DiscreteIndexedAttribute} from "../Attribute/DiscreteIndexedAttribute";
10
+ import {Performance} from "../Performance/Performance";
11
+ import {ConfusionMatrix} from "../Performance/ConfusionMatrix";
12
+ import {DetailedClassificationPerformance} from "../Performance/DetailedClassificationPerformance";
6
13
 
7
14
  export abstract class Model {
8
15
 
9
16
  abstract predict(instance: Instance): string
10
17
  abstract predictProbability(instance: Instance): Map<string, number>
11
18
  abstract saveTxt(fileName: String): void
19
+ abstract train(trainSet: InstanceList, parameters: Parameter):void
20
+ abstract loadModel(fileName: string): void
12
21
 
13
22
  /**
14
23
  * Given an array of class labels, returns the maximum occurred one.
@@ -24,6 +33,13 @@ export abstract class Model {
24
33
  return frequencies.max();
25
34
  }
26
35
 
36
+ /**
37
+ * Loads a single instance from a single line.
38
+ * @param line Line containing the instance.
39
+ * @param attributeTypes Type of the attributes of the instance. If th attribute is discrete, it is "DISCRETE",
40
+ * otherwise it is "CONTINUOUS".
41
+ * @return Instance read from the line.
42
+ */
27
43
  loadInstance(line: string, attributeTypes: string[]): Instance{
28
44
  let items = line.split(",")
29
45
  let instance = new Instance(items[items.length - 1])
@@ -40,6 +56,30 @@ export abstract class Model {
40
56
  return instance
41
57
  }
42
58
 
59
+ /**
60
+ * Loads a discrete distribution from an input model file
61
+ * @param input Input model file.
62
+ * @return Discrete distribution read from an input model file.
63
+ */
64
+ static loadDiscreteDistribution(input: FileContents): DiscreteDistribution{
65
+ let distribution = new DiscreteDistribution()
66
+ let size = parseInt(input.readLine())
67
+ for (let i = 0; i < size; i++){
68
+ let line = input.readLine()
69
+ let items = line.split(" ")
70
+ let count = parseInt(items[1])
71
+ for (let j = 0; j < count; j++){
72
+ distribution.addItem(items[0])
73
+ }
74
+ }
75
+ return distribution
76
+ }
77
+
78
+ /**
79
+ * Loads an instance list from an input model file.
80
+ * @param input Input model file.
81
+ * @return Instance list read from an input model file.
82
+ */
43
83
  loadInstanceList(input: FileContents): InstanceList{
44
84
  let types = input.readLine().split(" ")
45
85
  let instanceCount = parseInt(input.readLine())
@@ -50,6 +90,11 @@ export abstract class Model {
50
90
  return instanceList;
51
91
  }
52
92
 
93
+ /**
94
+ * Loads a matrix from an input model file.
95
+ * @param input Input model file.
96
+ * @return Matrix read from the input model file.
97
+ */
53
98
  loadMatrix(input: FileContents): Matrix{
54
99
  let items = input.readLine().split(" ")
55
100
  let matrix = new Matrix(parseInt(items[0]), parseInt(items[1]))
@@ -62,4 +107,51 @@ export abstract class Model {
62
107
  }
63
108
  return matrix
64
109
  }
110
+
111
+ /**
112
+ * Checks given instance's attribute and returns true if it is a discrete indexed attribute, false otherwise.
113
+ *
114
+ * @param instance Instance to check.
115
+ * @return True if instance is a discrete indexed attribute, false otherwise.
116
+ */
117
+ discreteCheck(instance: Instance): boolean{
118
+ for (let i = 0; i < instance.attributeSize(); i++) {
119
+ if (instance.getAttribute(i) instanceof DiscreteAttribute &&
120
+ !(instance.getAttribute(i) instanceof DiscreteIndexedAttribute)) {
121
+ return false;
122
+ }
123
+ }
124
+ return true;
125
+ }
126
+
127
+ /**
128
+ * TestClassification an instance list with the current model.
129
+ *
130
+ * @param testSet Test data (list of instances) to be tested.
131
+ * @return The accuracy (and error) of the model as an instance of Performance class.
132
+ */
133
+ test(testSet: InstanceList): Performance{
134
+ let classLabels = testSet.getUnionOfPossibleClassLabels();
135
+ let confusion = new ConfusionMatrix(classLabels);
136
+ for (let i = 0; i < testSet.size(); i++) {
137
+ let instance = testSet.get(i);
138
+ confusion.classify(instance.getClassLabel(), this.predict(instance));
139
+ }
140
+ return new DetailedClassificationPerformance(confusion);
141
+ }
142
+
143
+ /**
144
+ * Runs current classifier with the given train and test data.
145
+ *
146
+ * @param parameter Parameter of the classifier to be trained.
147
+ * @param trainSet Training data to be used in training the classifier.
148
+ * @param testSet Test data to be tested after training the model.
149
+ * @return The accuracy (and error) of the trained model as an instance of Performance class.
150
+ * @throws DiscreteFeaturesNotAllowed Exception for discrete features.
151
+ */
152
+ singleRun(parameter: Parameter, trainSet: InstanceList, testSet: InstanceList): Performance{
153
+ this.train(trainSet, parameter);
154
+ return this.test(testSet);
155
+ }
156
+
65
157
  }
@@ -1,12 +1,14 @@
1
1
  import {NeuralNetworkModel} from "./NeuralNetworkModel";
2
2
  import {Matrix} from "nlptoolkit-math/dist/Matrix";
3
- import {ActivationFunction} from "../Parameter/ActivationFunction";
4
- import {DeepNetworkParameter} from "../Parameter/DeepNetworkParameter";
5
- import {InstanceList} from "../InstanceList/InstanceList";
3
+ import {ActivationFunction} from "../../Parameter/ActivationFunction";
4
+ import {DeepNetworkParameter} from "../../Parameter/DeepNetworkParameter";
5
+ import {InstanceList} from "../../InstanceList/InstanceList";
6
6
  import {Vector} from "nlptoolkit-math/dist/Vector";
7
- import {ClassificationPerformance} from "../Performance/ClassificationPerformance";
7
+ import {ClassificationPerformance} from "../../Performance/ClassificationPerformance";
8
8
  import {Random} from "nlptoolkit-util/dist/Random";
9
9
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
10
+ import {Parameter} from "../../Parameter/Parameter";
11
+ import {Partition} from "../../InstanceList/Partition";
10
12
 
11
13
  export class DeepNetworkModel extends NeuralNetworkModel{
12
14
 
@@ -45,7 +47,55 @@ export class DeepNetworkModel extends NeuralNetworkModel{
45
47
  return bestWeights;
46
48
  }
47
49
 
48
- constructor1(trainSet: InstanceList, validationSet: InstanceList, parameters: DeepNetworkParameter){
50
+ /**
51
+ * Loads a deep network model from an input model file.
52
+ * @param fileName Model file name.
53
+ */
54
+ constructor2(fileName: string){
55
+ let input = new FileContents(fileName)
56
+ this.loadClassLabels(input)
57
+ this.hiddenLayerSize = parseInt(input.readLine())
58
+ this.weights = new Array<Matrix>()
59
+ for (let i = 0; i < this.hiddenLayerSize + 1; i++){
60
+ this.weights.push(this.loadMatrix(input))
61
+ }
62
+ this.activationFunction = this.loadActivationFunction(input)
63
+ }
64
+
65
+ /**
66
+ * The calculateOutput method loops size of the weights times and calculate one hidden layer at a time and adds bias term.
67
+ * At the end it updates the output y value.
68
+ */
69
+ protected calculateOutput(): void {
70
+ let hiddenBiased
71
+ for (let i = 0; i < this.weights.length - 1; i++) {
72
+ let hidden
73
+ if (i == 0) {
74
+ hidden = this.calculateHidden(this.x, this.weights[i], this.activationFunction);
75
+ } else {
76
+ hidden = this.calculateHidden(hiddenBiased, this.weights[i], this.activationFunction);
77
+ }
78
+ hiddenBiased = hidden.biased();
79
+ }
80
+ this.y = this.weights[this.weights.length - 1].multiplyWithVectorFromRight(hiddenBiased);
81
+ }
82
+
83
+ saveTxt(fileName: string){
84
+ }
85
+
86
+ /**
87
+ * Training algorithm for deep network classifier.
88
+ *
89
+ * @param train Training data given to the algorithm.
90
+ * @param params Parameters of the deep network algorithm. crossValidationRatio and seed are used as parameters.
91
+ * @throws DiscreteFeaturesNotAllowed Exception for discrete features.
92
+ */
93
+ train(train: InstanceList, params: Parameter): void {
94
+ this.initialize(train)
95
+ let parameters = <DeepNetworkParameter> params
96
+ let partition = new Partition(train, parameters.getCrossValidationRatio(), true);
97
+ let trainSet = partition.get(1)
98
+ let validationSet = partition.get(0)
49
99
  let tmpHidden = new Vector(0, 0);
50
100
  let deltaWeights = new Array<Matrix>();
51
101
  let hidden = new Array<Vector>();
@@ -123,59 +173,12 @@ export class DeepNetworkModel extends NeuralNetworkModel{
123
173
  }
124
174
  }
125
175
 
126
- constructor2(fileName: string){
127
- let input = new FileContents(fileName)
128
- this.loadClassLabels(input)
129
- this.hiddenLayerSize = parseInt(input.readLine())
130
- this.weights = new Array<Matrix>()
131
- for (let i = 0; i < this.hiddenLayerSize + 1; i++){
132
- this.weights.push(this.loadMatrix(input))
133
- }
134
- this.activationFunction = this.loadActivationFunction(input)
135
- }
136
-
137
176
  /**
138
- * Constructor that takes two {@link InstanceList} train set and validation set and {@link DeepNetworkParameter} as inputs.
139
- * First it sets the class labels, their sizes as K and the size of the continuous attributes as d of given train set and
140
- * allocates weights and sets the best weights. At each epoch, it shuffles the train set and loops through the each item of that train set,
141
- * it multiplies the weights Matrix with input Vector than applies the sigmoid function and stores the result as hidden and add bias.
142
- * Then updates weights and at the end it compares the performance of these weights with validation set. It updates the bestClassificationPerformance and
143
- * bestWeights according to the current situation. At the end it updates the learning rate via etaDecrease value and finishes
144
- * with clearing the weights.
145
- *
146
- * @param trainSetOrFileName {@link InstanceList} to be used as trainSet.
147
- * @param validationSet {@link InstanceList} to be used as validationSet.
148
- * @param parameters {@link DeepNetworkParameter} input.
177
+ * Loads the deep network model from an input file.
178
+ * @param fileName File name of the deep network model.
149
179
  */
150
- constructor(trainSetOrFileName: InstanceList | string, validationSet?: InstanceList, parameters?: DeepNetworkParameter) {
151
- if (trainSetOrFileName instanceof InstanceList){
152
- super(trainSetOrFileName)
153
- this.constructor1(trainSetOrFileName, validationSet, parameters)
154
- } else {
155
- super()
156
- this.constructor2(trainSetOrFileName)
157
- }
158
- }
159
-
160
- /**
161
- * The calculateOutput method loops size of the weights times and calculate one hidden layer at a time and adds bias term.
162
- * At the end it updates the output y value.
163
- */
164
- protected calculateOutput(): void {
165
- let hiddenBiased
166
- for (let i = 0; i < this.weights.length - 1; i++) {
167
- let hidden
168
- if (i == 0) {
169
- hidden = this.calculateHidden(this.x, this.weights[i], this.activationFunction);
170
- } else {
171
- hidden = this.calculateHidden(hiddenBiased, this.weights[i], this.activationFunction);
172
- }
173
- hiddenBiased = hidden.biased();
174
- }
175
- this.y = this.weights[this.weights.length - 1].multiplyWithVectorFromRight(hiddenBiased);
176
- }
177
-
178
- saveTxt(fileName: string){
180
+ loadModel(fileName: string): void{
181
+ this.constructor2(fileName)
179
182
  }
180
183
 
181
184
  }
@@ -0,0 +1,81 @@
1
+ import {NeuralNetworkModel} from "./NeuralNetworkModel";
2
+ import {Matrix} from "nlptoolkit-math/dist/Matrix";
3
+ import {InstanceList} from "../../InstanceList/InstanceList";
4
+ import {LinearPerceptronParameter} from "../../Parameter/LinearPerceptronParameter";
5
+ import {ClassificationPerformance} from "../../Performance/ClassificationPerformance";
6
+ import {Random} from "nlptoolkit-util/dist/Random";
7
+ import {FileContents} from "nlptoolkit-util/dist/FileContents";
8
+ import {Parameter} from "../../Parameter/Parameter";
9
+ import {Partition} from "../../InstanceList/Partition";
10
+
11
+ export class LinearPerceptronModel extends NeuralNetworkModel{
12
+
13
+ protected W: Matrix
14
+
15
+ /**
16
+ * Loads a linear perceptron model from an input model file.
17
+ * @param fileName Model file name.
18
+ */
19
+ constructor2(fileName: string){
20
+ let input = new FileContents(fileName)
21
+ this.loadClassLabels(input)
22
+ this.W = this.loadMatrix(input)
23
+ }
24
+
25
+ /**
26
+ * The calculateOutput method calculates the {@link Matrix} y by multiplying Matrix W with {@link Vector} x.
27
+ */
28
+ protected calculateOutput(): void {
29
+ this.y = this.W.multiplyWithVectorFromRight(this.x);
30
+ }
31
+
32
+ saveTxt(fileName: string){
33
+ }
34
+
35
+ /**
36
+ * Training algorithm for the linear perceptron algorithm. 20 percent of the data is separated as cross-validation
37
+ * data used for selecting the best weights. 80 percent of the data is used for training the linear perceptron with
38
+ * gradient descent.
39
+ *
40
+ * @param train Training data given to the algorithm
41
+ * @param params Parameters of the linear perceptron.
42
+ */
43
+ train(train: InstanceList, params: Parameter): void {
44
+ this.initialize(train)
45
+ let parameters = <LinearPerceptronParameter> params
46
+ let partition = new Partition(train, parameters.getCrossValidationRatio(), true);
47
+ let trainSet = partition.get(1)
48
+ let validationSet = partition.get(0)
49
+ this.W = this.allocateLayerWeights(this.K, this.d + 1, new Random(parameters.getSeed()));
50
+ let bestW = this.W.clone();
51
+ let bestClassificationPerformance = new ClassificationPerformance(0.0)
52
+ let epoch = parameters.getEpoch()
53
+ let learningRate = parameters.getLearningRate()
54
+ for (let i = 0; i < epoch; i++) {
55
+ trainSet.shuffle(new Random(parameters.getSeed()))
56
+ for (let j = 0; j < trainSet.size(); j++) {
57
+ this.createInputVector(trainSet.get(j))
58
+ let rMinusY = this.calculateRMinusY(trainSet.get(j), this.x, this.W)
59
+ let deltaW = new Matrix(rMinusY, this.x);
60
+ deltaW.multiplyWithConstant(learningRate);
61
+ this.W.add(deltaW)
62
+ }
63
+ let currentClassificationPerformance = this.testClassifier(validationSet)
64
+ if (currentClassificationPerformance.getAccuracy() > bestClassificationPerformance.getAccuracy()) {
65
+ bestClassificationPerformance = currentClassificationPerformance
66
+ bestW = this.W.clone()
67
+ }
68
+ learningRate *= parameters.getEtaDecrease()
69
+ }
70
+ this.W = bestW
71
+ }
72
+
73
+ /**
74
+ * Loads the linear perceptron model from an input file.
75
+ * @param fileName File name of the linear perceptron model.
76
+ */
77
+ loadModel(fileName: string): void{
78
+ this.constructor2(fileName)
79
+ }
80
+
81
+ }
@@ -1,12 +1,14 @@
1
1
  import {LinearPerceptronModel} from "./LinearPerceptronModel";
2
2
  import {Matrix} from "nlptoolkit-math/dist/Matrix";
3
- import {ActivationFunction} from "../Parameter/ActivationFunction";
4
- import {InstanceList} from "../InstanceList/InstanceList";
5
- import {MultiLayerPerceptronParameter} from "../Parameter/MultiLayerPerceptronParameter";
6
- import {ClassificationPerformance} from "../Performance/ClassificationPerformance";
3
+ import {ActivationFunction} from "../../Parameter/ActivationFunction";
4
+ import {InstanceList} from "../../InstanceList/InstanceList";
5
+ import {MultiLayerPerceptronParameter} from "../../Parameter/MultiLayerPerceptronParameter";
6
+ import {ClassificationPerformance} from "../../Performance/ClassificationPerformance";
7
7
  import {Vector} from "nlptoolkit-math/dist/Vector";
8
8
  import {Random} from "nlptoolkit-util/dist/Random";
9
9
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
10
+ import {Parameter} from "../../Parameter/Parameter";
11
+ import {Partition} from "../../InstanceList/Partition";
10
12
 
11
13
  export class MultiLayerPerceptronModel extends LinearPerceptronModel{
12
14
 
@@ -24,7 +26,42 @@ export class MultiLayerPerceptronModel extends LinearPerceptronModel{
24
26
  this.V = this.allocateLayerWeights(this.K, H + 1, random);
25
27
  }
26
28
 
27
- constructor1(trainSet: InstanceList, validationSet?: InstanceList, parameters?: MultiLayerPerceptronParameter){
29
+ /**
30
+ * Loads a multi-layer perceptron model from an input model file.
31
+ * @param fileName Model file name.
32
+ */
33
+ constructor2(fileName: string){
34
+ let input = new FileContents(fileName)
35
+ this.loadClassLabels(input)
36
+ this.W = this.loadMatrix(input)
37
+ this.V = this.loadMatrix(input)
38
+ this.activationFunction = this.loadActivationFunction(input)
39
+ }
40
+
41
+ /**
42
+ * The calculateOutput method calculates the forward single hidden layer by using Matrices W and V.
43
+ */
44
+ protected calculateOutput() {
45
+ this.calculateForwardSingleHiddenLayer(this.W, this.V, this.activationFunction);
46
+ }
47
+
48
+ saveTxt(fileName: string){
49
+ }
50
+
51
+ /**
52
+ * Training algorithm for the multilayer perceptron algorithm. 20 percent of the data is separated as cross-validation
53
+ * data used for selecting the best weights. 80 percent of the data is used for training the multilayer perceptron with
54
+ * gradient descent.
55
+ *
56
+ * @param train Training data given to the algorithm
57
+ * @param params Parameters of the multilayer perceptron.
58
+ */
59
+ train(train: InstanceList, params: Parameter): void {
60
+ this.initialize(train)
61
+ let parameters = (<MultiLayerPerceptronParameter> params)
62
+ let partition = new Partition(train, parameters.getCrossValidationRatio(), true);
63
+ let trainSet = partition.get(1)
64
+ let validationSet = partition.get(1)
28
65
  this.activationFunction = parameters.getActivationFunction();
29
66
  this.allocateWeights(parameters.getHiddenNodes(), new Random(parameters.getSeed()));
30
67
  let bestW = this.W.clone();
@@ -78,42 +115,12 @@ export class MultiLayerPerceptronModel extends LinearPerceptronModel{
78
115
  this.V = bestV;
79
116
  }
80
117
 
81
- constructor2(fileName: string){
82
- let input = new FileContents(fileName)
83
- this.loadClassLabels(input)
84
- this.W = this.loadMatrix(input)
85
- this.V = this.loadMatrix(input)
86
- this.activationFunction = this.loadActivationFunction(input)
87
- }
88
-
89
118
  /**
90
- * A constructor that takes {@link InstanceList}s as trainsSet and validationSet. It sets the {@link NeuralNetworkModel}
91
- * nodes with given {@link InstanceList} then creates an input vector by using given trainSet and finds error.
92
- * Via the validationSet it finds the classification performance and reassigns the allocated weight Matrix with the matrix
93
- * that has the best accuracy and the Matrix V with the best Vector input.
94
- *
95
- * @param trainSetOrFileName InstanceList that is used to train.
96
- * @param validationSet InstanceList that is used to validate.
97
- * @param parameters Multi layer perceptron parameters; seed, learningRate, etaDecrease, crossValidationRatio, epoch, hiddenNodes.
119
+ * Loads the multi-layer perceptron model from an input file.
120
+ * @param fileName File name of the multi-layer perceptron model.
98
121
  */
99
- constructor(trainSetOrFileName: InstanceList | string, validationSet?: InstanceList, parameters?: MultiLayerPerceptronParameter) {
100
- if (trainSetOrFileName instanceof InstanceList){
101
- super(trainSetOrFileName)
102
- this.constructor1(trainSetOrFileName, validationSet, parameters)
103
- } else {
104
- super()
105
- this.constructor2(trainSetOrFileName)
106
- }
107
- }
108
-
109
- /**
110
- * The calculateOutput method calculates the forward single hidden layer by using Matrices W and V.
111
- */
112
- protected calculateOutput() {
113
- this.calculateForwardSingleHiddenLayer(this.W, this.V, this.activationFunction);
114
- }
115
-
116
- saveTxt(fileName: string){
122
+ loadModel(fileName: string): void{
123
+ this.constructor2(fileName)
117
124
  }
118
125
 
119
126
  }
@@ -1,10 +1,10 @@
1
- import {ValidatedModel} from "./ValidatedModel";
1
+ import {ValidatedModel} from "../ValidatedModel";
2
2
  import {Vector} from "nlptoolkit-math/dist/Vector";
3
- import {InstanceList} from "../InstanceList/InstanceList";
3
+ import {InstanceList} from "../../InstanceList/InstanceList";
4
4
  import {Matrix} from "nlptoolkit-math/dist/Matrix";
5
- import {Instance} from "../Instance/Instance";
6
- import {ActivationFunction} from "../Parameter/ActivationFunction";
7
- import {CompositeInstance} from "../Instance/CompositeInstance";
5
+ import {Instance} from "../../Instance/Instance";
6
+ import {ActivationFunction} from "../../Parameter/ActivationFunction";
7
+ import {CompositeInstance} from "../../Instance/CompositeInstance";
8
8
  import {Random} from "nlptoolkit-util/dist/Random";
9
9
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
10
10
 
@@ -24,8 +24,7 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
24
24
  *
25
25
  * @param trainSet {@link InstanceList} to use as train set.
26
26
  */
27
- protected constructor(trainSet?: InstanceList) {
28
- super();
27
+ protected initialize(trainSet: InstanceList) {
29
28
  if (trainSet != undefined){
30
29
  this.classLabels = trainSet.getDistinctClassLabels()
31
30
  this.K = this.classLabels.length
@@ -180,6 +179,11 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
180
179
  }
181
180
  }
182
181
 
182
+ /**
183
+ * Calculates the posterior probability distribution for the given instance according to neural network model.
184
+ * @param instance Instance for which posterior probability distribution is calculated.
185
+ * @return Posterior probability distribution for the given instance.
186
+ */
183
187
  predictProbability(instance: Instance): Map<string, number> {
184
188
  this.createInputVector(instance);
185
189
  this.calculateOutput();
@@ -190,6 +194,10 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
190
194
  return result;
191
195
  }
192
196
 
197
+ /**
198
+ * Loads the class labels from input model file.
199
+ * @param input Input model file.
200
+ */
193
201
  loadClassLabels(input: FileContents){
194
202
  let items = input.readLine().split(" ")
195
203
  this.K = parseInt(items[0])
@@ -200,6 +208,11 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
200
208
  }
201
209
  }
202
210
 
211
+ /**
212
+ * Loads the activation function from an input model file.
213
+ * @param input Input model file.
214
+ * @return Activation function read.
215
+ */
203
216
  loadActivationFunction(input: FileContents): ActivationFunction{
204
217
  switch (input.readLine()){
205
218
  case "TANH":
@@ -1,4 +1,4 @@
1
- import {Instance} from "../Instance/Instance";
1
+ import {Instance} from "../../Instance/Instance";
2
2
 
3
3
  export class KnnInstance {
4
4
 
@@ -1,11 +1,13 @@
1
- import {Model} from "./Model";
2
- import {InstanceList} from "../InstanceList/InstanceList";
3
- import {Instance} from "../Instance/Instance";
4
- import {DistanceMetric} from "../DistanceMetric/DistanceMetric";
5
- import {CompositeInstance} from "../Instance/CompositeInstance";
1
+ import {Model} from "../Model";
2
+ import {InstanceList} from "../../InstanceList/InstanceList";
3
+ import {Instance} from "../../Instance/Instance";
4
+ import {DistanceMetric} from "../../DistanceMetric/DistanceMetric";
5
+ import {CompositeInstance} from "../../Instance/CompositeInstance";
6
6
  import {KnnInstance} from "./KnnInstance";
7
- import {EuclidianDistance} from "../DistanceMetric/EuclidianDistance";
7
+ import {EuclidianDistance} from "../../DistanceMetric/EuclidianDistance";
8
8
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
9
+ import {Parameter} from "../../Parameter/Parameter";
10
+ import {KnnParameter} from "../../Parameter/KnnParameter";
9
11
 
10
12
  export class KnnModel extends Model{
11
13
 
@@ -16,22 +18,25 @@ export class KnnModel extends Model{
16
18
  /**
17
19
  * Constructor that sets the data {@link InstanceList}, k value and the {@link DistanceMetric}.
18
20
  *
19
- * @param dataOrFileName {@link InstanceList} input.
21
+ * @param data {@link InstanceList} input.
20
22
  * @param k K value.
21
23
  * @param distanceMetric {@link DistanceMetric} input.
22
24
  */
23
- constructor(dataOrFileName: InstanceList | string, k?: number, distanceMetric?: DistanceMetric) {
24
- super()
25
- if (dataOrFileName instanceof InstanceList){
26
- this.data = dataOrFileName
27
- this.k = k
28
- this.distanceMetric = distanceMetric
29
- } else {
30
- this.distanceMetric = new EuclidianDistance()
31
- let input = new FileContents(dataOrFileName)
32
- this.k = parseInt(input.readLine())
33
- this.data = this.loadInstanceList(input)
34
- }
25
+ constructor1(data: InstanceList, k: number, distanceMetric: DistanceMetric) {
26
+ this.data = data
27
+ this.k = k
28
+ this.distanceMetric = distanceMetric
29
+ }
30
+
31
+ /**
32
+ * Loads a K-nearest neighbor model from an input model file.
33
+ * @param fileName Model file name.
34
+ */
35
+ constructor2(fileName: string) {
36
+ this.distanceMetric = new EuclidianDistance()
37
+ let input = new FileContents(fileName)
38
+ this.k = parseInt(input.readLine())
39
+ this.data = this.loadInstanceList(input)
35
40
  }
36
41
 
37
42
  /**
@@ -52,6 +57,11 @@ export class KnnModel extends Model{
52
57
  return predictedClass;
53
58
  }
54
59
 
60
+ /**
61
+ * Calculates the posterior probability distribution for the given instance according to K-means model.
62
+ * @param instance Instance for which posterior probability distribution is calculated.
63
+ * @return Posterior probability distribution for the given instance.
64
+ */
55
65
  predictProbability(instance: Instance): Map<string, number> {
56
66
  let nearestNeighbors = this.nearestNeighbors(instance);
57
67
  return nearestNeighbors.classDistribution().getProbabilityDistribution();
@@ -88,4 +98,23 @@ export class KnnModel extends Model{
88
98
  saveTxt(fileName: string){
89
99
  }
90
100
 
101
+ /**
102
+ * Training algorithm for K-nearest neighbor classifier.
103
+ *
104
+ * @param trainSet Training data given to the algorithm.
105
+ * @param parameters K: k parameter of the K-nearest neighbor algorithm
106
+ * distanceMetric: distance metric used to calculate the distance between two instances.
107
+ */
108
+ train(trainSet: InstanceList, parameters: Parameter): void {
109
+ this.constructor1(trainSet, (<KnnParameter> parameters).getK(), (<KnnParameter> parameters).getDistanceMetric());
110
+ }
111
+
112
+ /**
113
+ * Loads the K-nearest neighbor model from an input file.
114
+ * @param fileName File name of the K-nearest neighbor model.
115
+ */
116
+ loadModel(fileName: string): void{
117
+ this.constructor2(fileName)
118
+ }
119
+
91
120
  }
@@ -1,7 +1,7 @@
1
1
  import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
2
- import {Instance} from "../Instance/Instance";
3
- import {ValidatedModel} from "./ValidatedModel";
4
- import {CompositeInstance} from "../Instance/CompositeInstance";
2
+ import {Instance} from "../../Instance/Instance";
3
+ import {ValidatedModel} from "../ValidatedModel";
4
+ import {CompositeInstance} from "../../Instance/CompositeInstance";
5
5
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
6
6
  import {Vector} from "nlptoolkit-math/dist/Vector";
7
7
 
@@ -54,6 +54,12 @@ export abstract class GaussianModel extends ValidatedModel{
54
54
  return predictedClass;
55
55
  }
56
56
 
57
+ /**
58
+ * Loads the prior probability distribution from an input model file.
59
+ * @param input Input model file.
60
+ * @return Prior probability distribution.
61
+ * @throws IOException If the input file can not be read, the method throws IOException.
62
+ */
57
63
  loadPriorDistribution(input: FileContents): number{
58
64
  let size = parseInt(input.readLine())
59
65
  this.priorDistribution = new DiscreteDistribution()
@@ -67,6 +73,13 @@ export abstract class GaussianModel extends ValidatedModel{
67
73
  return size
68
74
  }
69
75
 
76
+ /**
77
+ * Loads hash map of vectors from input model file.
78
+ * @param input Input model file.
79
+ * @param size Number of vectors to be read from input model file.
80
+ * @return Hash map of vectors.
81
+ * @throws IOException If the input file can not be read, the method throws IOException.
82
+ */
70
83
  loadVectors(input: FileContents, size: number): Map<string, Vector>{
71
84
  let map = new Map<string, Vector>()
72
85
  for (let i = 0; i < size; i++){
@@ -81,6 +94,11 @@ export abstract class GaussianModel extends ValidatedModel{
81
94
  return map
82
95
  }
83
96
 
97
+ /**
98
+ * Calculates the posterior probability distribution for the given instance according to Gaussian model.
99
+ * @param instance Instance for which posterior probability distribution is calculated.
100
+ * @return Posterior probability distribution for the given instance.
101
+ */
84
102
  predictProbability(instance: Instance): Map<string, number> {
85
103
  return undefined;
86
104
  }