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
@@ -0,0 +1,39 @@
1
+ import { LinearPerceptronModel } from "./LinearPerceptronModel";
2
+ import { ActivationFunction } from "../../Parameter/ActivationFunction";
3
+ import { InstanceList } from "../../InstanceList/InstanceList";
4
+ import { Parameter } from "../../Parameter/Parameter";
5
+ export declare class MultiLayerPerceptronModel extends LinearPerceptronModel {
6
+ private V;
7
+ protected activationFunction: ActivationFunction;
8
+ /**
9
+ * The allocateWeights method allocates layers' weights of Matrix W and V.
10
+ *
11
+ * @param H Integer value for weights.
12
+ * @param random Random function to set weights.
13
+ */
14
+ private allocateWeights;
15
+ /**
16
+ * Loads a multi-layer perceptron model from an input model file.
17
+ * @param fileName Model file name.
18
+ */
19
+ constructor2(fileName: string): void;
20
+ /**
21
+ * The calculateOutput method calculates the forward single hidden layer by using Matrices W and V.
22
+ */
23
+ protected calculateOutput(): void;
24
+ saveTxt(fileName: string): void;
25
+ /**
26
+ * Training algorithm for the multilayer perceptron algorithm. 20 percent of the data is separated as cross-validation
27
+ * data used for selecting the best weights. 80 percent of the data is used for training the multilayer perceptron with
28
+ * gradient descent.
29
+ *
30
+ * @param train Training data given to the algorithm
31
+ * @param params Parameters of the multilayer perceptron.
32
+ */
33
+ train(train: InstanceList, params: Parameter): void;
34
+ /**
35
+ * Loads the multi-layer perceptron model from an input file.
36
+ * @param fileName File name of the multi-layer perceptron model.
37
+ */
38
+ loadModel(fileName: string): void;
39
+ }
@@ -4,7 +4,7 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./LinearPerceptronModel", "nlptoolkit-math/dist/Matrix", "../Parameter/ActivationFunction", "../InstanceList/InstanceList", "../Performance/ClassificationPerformance", "nlptoolkit-math/dist/Vector", "nlptoolkit-util/dist/Random", "nlptoolkit-util/dist/FileContents"], factory);
7
+ define(["require", "exports", "./LinearPerceptronModel", "nlptoolkit-math/dist/Matrix", "../../Parameter/ActivationFunction", "../../Performance/ClassificationPerformance", "nlptoolkit-math/dist/Vector", "nlptoolkit-util/dist/Random", "nlptoolkit-util/dist/FileContents", "../../InstanceList/Partition"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -12,33 +12,13 @@
12
12
  exports.MultiLayerPerceptronModel = void 0;
13
13
  const LinearPerceptronModel_1 = require("./LinearPerceptronModel");
14
14
  const Matrix_1 = require("nlptoolkit-math/dist/Matrix");
15
- const ActivationFunction_1 = require("../Parameter/ActivationFunction");
16
- const InstanceList_1 = require("../InstanceList/InstanceList");
17
- const ClassificationPerformance_1 = require("../Performance/ClassificationPerformance");
15
+ const ActivationFunction_1 = require("../../Parameter/ActivationFunction");
16
+ const ClassificationPerformance_1 = require("../../Performance/ClassificationPerformance");
18
17
  const Vector_1 = require("nlptoolkit-math/dist/Vector");
19
18
  const Random_1 = require("nlptoolkit-util/dist/Random");
20
19
  const FileContents_1 = require("nlptoolkit-util/dist/FileContents");
20
+ const Partition_1 = require("../../InstanceList/Partition");
21
21
  class MultiLayerPerceptronModel extends LinearPerceptronModel_1.LinearPerceptronModel {
22
- /**
23
- * A constructor that takes {@link InstanceList}s as trainsSet and validationSet. It sets the {@link NeuralNetworkModel}
24
- * nodes with given {@link InstanceList} then creates an input vector by using given trainSet and finds error.
25
- * Via the validationSet it finds the classification performance and reassigns the allocated weight Matrix with the matrix
26
- * that has the best accuracy and the Matrix V with the best Vector input.
27
- *
28
- * @param trainSetOrFileName InstanceList that is used to train.
29
- * @param validationSet InstanceList that is used to validate.
30
- * @param parameters Multi layer perceptron parameters; seed, learningRate, etaDecrease, crossValidationRatio, epoch, hiddenNodes.
31
- */
32
- constructor(trainSetOrFileName, validationSet, parameters) {
33
- if (trainSetOrFileName instanceof InstanceList_1.InstanceList) {
34
- super(trainSetOrFileName);
35
- this.constructor1(trainSetOrFileName, validationSet, parameters);
36
- }
37
- else {
38
- super();
39
- this.constructor2(trainSetOrFileName);
40
- }
41
- }
42
22
  /**
43
23
  * The allocateWeights method allocates layers' weights of Matrix W and V.
44
24
  *
@@ -49,7 +29,39 @@
49
29
  this.W = this.allocateLayerWeights(H, this.d + 1, random);
50
30
  this.V = this.allocateLayerWeights(this.K, H + 1, random);
51
31
  }
52
- constructor1(trainSet, validationSet, parameters) {
32
+ /**
33
+ * Loads a multi-layer perceptron model from an input model file.
34
+ * @param fileName Model file name.
35
+ */
36
+ constructor2(fileName) {
37
+ let input = new FileContents_1.FileContents(fileName);
38
+ this.loadClassLabels(input);
39
+ this.W = this.loadMatrix(input);
40
+ this.V = this.loadMatrix(input);
41
+ this.activationFunction = this.loadActivationFunction(input);
42
+ }
43
+ /**
44
+ * The calculateOutput method calculates the forward single hidden layer by using Matrices W and V.
45
+ */
46
+ calculateOutput() {
47
+ this.calculateForwardSingleHiddenLayer(this.W, this.V, this.activationFunction);
48
+ }
49
+ saveTxt(fileName) {
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, params) {
60
+ this.initialize(train);
61
+ let parameters = params;
62
+ let partition = new Partition_1.Partition(train, parameters.getCrossValidationRatio(), true);
63
+ let trainSet = partition.get(1);
64
+ let validationSet = partition.get(1);
53
65
  this.activationFunction = parameters.getActivationFunction();
54
66
  this.allocateWeights(parameters.getHiddenNodes(), new Random_1.Random(parameters.getSeed()));
55
67
  let bestW = this.W.clone();
@@ -102,20 +114,12 @@
102
114
  this.W = bestW;
103
115
  this.V = bestV;
104
116
  }
105
- constructor2(fileName) {
106
- let input = new FileContents_1.FileContents(fileName);
107
- this.loadClassLabels(input);
108
- this.W = this.loadMatrix(input);
109
- this.V = this.loadMatrix(input);
110
- this.activationFunction = this.loadActivationFunction(input);
111
- }
112
117
  /**
113
- * The calculateOutput method calculates the forward single hidden layer by using Matrices W and V.
118
+ * Loads the multi-layer perceptron model from an input file.
119
+ * @param fileName File name of the multi-layer perceptron model.
114
120
  */
115
- calculateOutput() {
116
- this.calculateForwardSingleHiddenLayer(this.W, this.V, this.activationFunction);
117
- }
118
- saveTxt(fileName) {
121
+ loadModel(fileName) {
122
+ this.constructor2(fileName);
119
123
  }
120
124
  }
121
125
  exports.MultiLayerPerceptronModel = MultiLayerPerceptronModel;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultiLayerPerceptronModel.js","sourceRoot":"","sources":["../../../source/Model/NeuralNetwork/MultiLayerPerceptronModel.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,mEAA8D;IAC9D,wDAAmD;IACnD,2EAAsE;IAGtE,2FAAsF;IACtF,wDAAmD;IACnD,wDAAmD;IACnD,oEAA+D;IAE/D,4DAAuD;IAEvD,MAAa,yBAA0B,SAAQ,6CAAqB;QAKhE;;;;;WAKG;QACK,eAAe,CAAC,CAAS,EAAE,MAAc;YAC7C,IA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,CAAC,CAAC;gBAC1E,IAAI,gCAAgC,CAAC,WAAW,EAAE,GAAG,6BAA6B,CAAC,WAAW,EAAE,EAAE;oBAC9F,6BAA6B,GAAG,gCAAgC,CAAC;oBACjE,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;oBACvB,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;iBAC1B;gBACD,YAAY,IAAI,UAAU,CAAC,cAAc,EAAE,CAAC;aAC/C;YACD,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC;YACf,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC;QACnB,CAAC;QAED;;;WAGG;QACH,SAAS,CAAC,QAAgB;YACtB,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAA;QAC/B,CAAC;KAEJ;IAjHD,8DAiHC"}
@@ -1,9 +1,9 @@
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";
5
+ import { Instance } from "../../Instance/Instance";
6
+ import { ActivationFunction } from "../../Parameter/ActivationFunction";
7
7
  import { Random } from "nlptoolkit-util/dist/Random";
8
8
  import { FileContents } from "nlptoolkit-util/dist/FileContents";
9
9
  export declare abstract class NeuralNetworkModel extends ValidatedModel {
@@ -19,7 +19,7 @@ export declare abstract class NeuralNetworkModel extends ValidatedModel {
19
19
  *
20
20
  * @param trainSet {@link InstanceList} to use as train set.
21
21
  */
22
- protected constructor(trainSet?: InstanceList);
22
+ protected initialize(trainSet: InstanceList): void;
23
23
  /**
24
24
  * The allocateLayerWeights method returns a new {@link Matrix} with random weights.
25
25
  *
@@ -98,7 +98,21 @@ export declare abstract class NeuralNetworkModel extends ValidatedModel {
98
98
  * @return The class label which has the maximum y.
99
99
  */
100
100
  predict(instance: Instance): string;
101
+ /**
102
+ * Calculates the posterior probability distribution for the given instance according to neural network model.
103
+ * @param instance Instance for which posterior probability distribution is calculated.
104
+ * @return Posterior probability distribution for the given instance.
105
+ */
101
106
  predictProbability(instance: Instance): Map<string, number>;
107
+ /**
108
+ * Loads the class labels from input model file.
109
+ * @param input Input model file.
110
+ */
102
111
  loadClassLabels(input: FileContents): void;
112
+ /**
113
+ * Loads the activation function from an input model file.
114
+ * @param input Input model file.
115
+ * @return Activation function read.
116
+ */
103
117
  loadActivationFunction(input: FileContents): ActivationFunction;
104
118
  }
@@ -4,25 +4,24 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./ValidatedModel", "nlptoolkit-math/dist/Vector", "nlptoolkit-math/dist/Matrix", "../Parameter/ActivationFunction", "../Instance/CompositeInstance"], factory);
7
+ define(["require", "exports", "../ValidatedModel", "nlptoolkit-math/dist/Vector", "nlptoolkit-math/dist/Matrix", "../../Parameter/ActivationFunction", "../../Instance/CompositeInstance"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.NeuralNetworkModel = void 0;
13
- const ValidatedModel_1 = require("./ValidatedModel");
13
+ const ValidatedModel_1 = require("../ValidatedModel");
14
14
  const Vector_1 = require("nlptoolkit-math/dist/Vector");
15
15
  const Matrix_1 = require("nlptoolkit-math/dist/Matrix");
16
- const ActivationFunction_1 = require("../Parameter/ActivationFunction");
17
- const CompositeInstance_1 = require("../Instance/CompositeInstance");
16
+ const ActivationFunction_1 = require("../../Parameter/ActivationFunction");
17
+ const CompositeInstance_1 = require("../../Instance/CompositeInstance");
18
18
  class NeuralNetworkModel extends ValidatedModel_1.ValidatedModel {
19
19
  /**
20
20
  * Constructor that sets the class labels, their sizes as K and the size of the continuous attributes as d.
21
21
  *
22
22
  * @param trainSet {@link InstanceList} to use as train set.
23
23
  */
24
- constructor(trainSet) {
25
- super();
24
+ initialize(trainSet) {
26
25
  if (trainSet != undefined) {
27
26
  this.classLabels = trainSet.getDistinctClassLabels();
28
27
  this.K = this.classLabels.length;
@@ -168,6 +167,11 @@
168
167
  return this.classLabels[this.y.maxIndex()];
169
168
  }
170
169
  }
170
+ /**
171
+ * Calculates the posterior probability distribution for the given instance according to neural network model.
172
+ * @param instance Instance for which posterior probability distribution is calculated.
173
+ * @return Posterior probability distribution for the given instance.
174
+ */
171
175
  predictProbability(instance) {
172
176
  this.createInputVector(instance);
173
177
  this.calculateOutput();
@@ -177,6 +181,10 @@
177
181
  }
178
182
  return result;
179
183
  }
184
+ /**
185
+ * Loads the class labels from input model file.
186
+ * @param input Input model file.
187
+ */
180
188
  loadClassLabels(input) {
181
189
  let items = input.readLine().split(" ");
182
190
  this.K = parseInt(items[0]);
@@ -186,6 +194,11 @@
186
194
  this.classLabels.push(input.readLine());
187
195
  }
188
196
  }
197
+ /**
198
+ * Loads the activation function from an input model file.
199
+ * @param input Input model file.
200
+ * @return Activation function read.
201
+ */
189
202
  loadActivationFunction(input) {
190
203
  switch (input.readLine()) {
191
204
  case "TANH":
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NeuralNetworkModel.js","sourceRoot":"","sources":["../../../source/Model/NeuralNetwork/NeuralNetworkModel.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,sDAAiD;IACjD,wDAAmD;IAEnD,wDAAmD;IAEnD,2EAAsE;IACtE,wEAAmE;IAInE,MAAsB,kBAAmB,SAAQ,+BAAc;QAW3D;;;;WAIG;QACO,UAAU,CAAC,QAAsB;YACvC,IAAI,QAAQ,IAAI,SAAS,EAAC;gBACtB,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC,sBAAsB,EAAE,CAAA;gBACpD,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,WAAW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IAAI,GAAG,EAAkB,CAAC;YACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAC;gBAC7C,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;aACvD;YACD,OAAO,MAAM,CAAC;QAClB,CAAC;QAED;;;WAGG;QACH,eAAe,CAAC,KAAmB;YAC/B,IAAI,KAAK,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACvC,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;YAC3B,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;YAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,KAAK,EAAU,CAAA;YACtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,EAAC;gBAC5B,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAA;aAC1C;QACL,CAAC;QAED;;;;WAIG;QACH,sBAAsB,CAAC,KAAmB;YACtC,QAAQ,KAAK,CAAC,QAAQ,EAAE,EAAC;gBACrB,KAAK,MAAM;oBACP,OAAO,uCAAkB,CAAC,IAAI,CAAA;gBAClC,KAAK,MAAM;oBACP,OAAO,uCAAkB,CAAC,IAAI,CAAA;gBAClC;oBACI,OAAO,uCAAkB,CAAC,OAAO,CAAA;aACxC;QACL,CAAC;KACJ;IAvND,gDAuNC"}
@@ -1,4 +1,4 @@
1
- import { Instance } from "../Instance/Instance";
1
+ import { Instance } from "../../Instance/Instance";
2
2
  export declare class KnnInstance {
3
3
  private readonly distance;
4
4
  private readonly instance;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KnnInstance.js","sourceRoot":"","sources":["../../../source/Model/NonParametric/KnnInstance.ts"],"names":[],"mappings":";;;;;;;;;;;;IAEA,MAAa,WAAW;QAKpB;;;;;WAKG;QACH,YAAY,QAAkB,EAAE,QAAgB;YAC5C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;YACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAC7B,CAAC;QAED,WAAW;YACP,OAAO,IAAI,CAAC,QAAQ,CAAA;QACxB,CAAC;QAED,WAAW;YACP,OAAO,IAAI,CAAC,QAAQ,CAAA;QACxB,CAAC;KAEJ;IAxBD,kCAwBC"}
@@ -1,7 +1,8 @@
1
- import { Model } from "./Model";
2
- import { InstanceList } from "../InstanceList/InstanceList";
3
- import { Instance } from "../Instance/Instance";
4
- import { DistanceMetric } from "../DistanceMetric/DistanceMetric";
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 { Parameter } from "../../Parameter/Parameter";
5
6
  export declare class KnnModel extends Model {
6
7
  private data;
7
8
  private k;
@@ -9,11 +10,16 @@ export declare class KnnModel extends Model {
9
10
  /**
10
11
  * Constructor that sets the data {@link InstanceList}, k value and the {@link DistanceMetric}.
11
12
  *
12
- * @param dataOrFileName {@link InstanceList} input.
13
+ * @param data {@link InstanceList} input.
13
14
  * @param k K value.
14
15
  * @param distanceMetric {@link DistanceMetric} input.
15
16
  */
16
- constructor(dataOrFileName: InstanceList | string, k?: number, distanceMetric?: DistanceMetric);
17
+ constructor1(data: InstanceList, k: number, distanceMetric: DistanceMetric): void;
18
+ /**
19
+ * Loads a K-nearest neighbor model from an input model file.
20
+ * @param fileName Model file name.
21
+ */
22
+ constructor2(fileName: string): void;
17
23
  /**
18
24
  * The predict method takes an {@link Instance} as an input and finds the nearest neighbors of given instance. Then
19
25
  * it returns the first possible class label as the predicted class.
@@ -22,6 +28,11 @@ export declare class KnnModel extends Model {
22
28
  * @return The first possible class label as the predicted class.
23
29
  */
24
30
  predict(instance: Instance): string;
31
+ /**
32
+ * Calculates the posterior probability distribution for the given instance according to K-means model.
33
+ * @param instance Instance for which posterior probability distribution is calculated.
34
+ * @return Posterior probability distribution for the given instance.
35
+ */
25
36
  predictProbability(instance: Instance): Map<string, number>;
26
37
  /**
27
38
  * The nearestNeighbors method takes an {@link Instance} as an input. First it gets the possible class labels, then loops
@@ -34,4 +45,17 @@ export declare class KnnModel extends Model {
34
45
  */
35
46
  nearestNeighbors(instance: Instance): InstanceList;
36
47
  saveTxt(fileName: string): void;
48
+ /**
49
+ * Training algorithm for K-nearest neighbor classifier.
50
+ *
51
+ * @param trainSet Training data given to the algorithm.
52
+ * @param parameters K: k parameter of the K-nearest neighbor algorithm
53
+ * distanceMetric: distance metric used to calculate the distance between two instances.
54
+ */
55
+ train(trainSet: InstanceList, parameters: Parameter): void;
56
+ /**
57
+ * Loads the K-nearest neighbor model from an input file.
58
+ * @param fileName File name of the K-nearest neighbor model.
59
+ */
60
+ loadModel(fileName: string): void;
37
61
  }
@@ -4,39 +4,40 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./Model", "../InstanceList/InstanceList", "../Instance/CompositeInstance", "./KnnInstance", "../DistanceMetric/EuclidianDistance", "nlptoolkit-util/dist/FileContents"], factory);
7
+ define(["require", "exports", "../Model", "../../InstanceList/InstanceList", "../../Instance/CompositeInstance", "./KnnInstance", "../../DistanceMetric/EuclidianDistance", "nlptoolkit-util/dist/FileContents"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.KnnModel = void 0;
13
- const Model_1 = require("./Model");
14
- const InstanceList_1 = require("../InstanceList/InstanceList");
15
- const CompositeInstance_1 = require("../Instance/CompositeInstance");
13
+ const Model_1 = require("../Model");
14
+ const InstanceList_1 = require("../../InstanceList/InstanceList");
15
+ const CompositeInstance_1 = require("../../Instance/CompositeInstance");
16
16
  const KnnInstance_1 = require("./KnnInstance");
17
- const EuclidianDistance_1 = require("../DistanceMetric/EuclidianDistance");
17
+ const EuclidianDistance_1 = require("../../DistanceMetric/EuclidianDistance");
18
18
  const FileContents_1 = require("nlptoolkit-util/dist/FileContents");
19
19
  class KnnModel extends Model_1.Model {
20
20
  /**
21
21
  * Constructor that sets the data {@link InstanceList}, k value and the {@link DistanceMetric}.
22
22
  *
23
- * @param dataOrFileName {@link InstanceList} input.
23
+ * @param data {@link InstanceList} input.
24
24
  * @param k K value.
25
25
  * @param distanceMetric {@link DistanceMetric} input.
26
26
  */
27
- constructor(dataOrFileName, k, distanceMetric) {
28
- super();
29
- if (dataOrFileName instanceof InstanceList_1.InstanceList) {
30
- this.data = dataOrFileName;
31
- this.k = k;
32
- this.distanceMetric = distanceMetric;
33
- }
34
- else {
35
- this.distanceMetric = new EuclidianDistance_1.EuclidianDistance();
36
- let input = new FileContents_1.FileContents(dataOrFileName);
37
- this.k = parseInt(input.readLine());
38
- this.data = this.loadInstanceList(input);
39
- }
27
+ constructor1(data, k, distanceMetric) {
28
+ this.data = data;
29
+ this.k = k;
30
+ this.distanceMetric = distanceMetric;
31
+ }
32
+ /**
33
+ * Loads a K-nearest neighbor model from an input model file.
34
+ * @param fileName Model file name.
35
+ */
36
+ constructor2(fileName) {
37
+ this.distanceMetric = new EuclidianDistance_1.EuclidianDistance();
38
+ let input = new FileContents_1.FileContents(fileName);
39
+ this.k = parseInt(input.readLine());
40
+ this.data = this.loadInstanceList(input);
40
41
  }
41
42
  /**
42
43
  * The predict method takes an {@link Instance} as an input and finds the nearest neighbors of given instance. Then
@@ -56,6 +57,11 @@
56
57
  }
57
58
  return predictedClass;
58
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
+ */
59
65
  predictProbability(instance) {
60
66
  let nearestNeighbors = this.nearestNeighbors(instance);
61
67
  return nearestNeighbors.classDistribution().getProbabilityDistribution();
@@ -89,6 +95,23 @@
89
95
  }
90
96
  saveTxt(fileName) {
91
97
  }
98
+ /**
99
+ * Training algorithm for K-nearest neighbor classifier.
100
+ *
101
+ * @param trainSet Training data given to the algorithm.
102
+ * @param parameters K: k parameter of the K-nearest neighbor algorithm
103
+ * distanceMetric: distance metric used to calculate the distance between two instances.
104
+ */
105
+ train(trainSet, parameters) {
106
+ this.constructor1(trainSet, parameters.getK(), parameters.getDistanceMetric());
107
+ }
108
+ /**
109
+ * Loads the K-nearest neighbor model from an input file.
110
+ * @param fileName File name of the K-nearest neighbor model.
111
+ */
112
+ loadModel(fileName) {
113
+ this.constructor2(fileName);
114
+ }
92
115
  }
93
116
  exports.KnnModel = KnnModel;
94
117
  });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KnnModel.js","sourceRoot":"","sources":["../../../source/Model/NonParametric/KnnModel.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,oCAA+B;IAC/B,kEAA6D;IAG7D,wEAAmE;IACnE,+CAA0C;IAC1C,8EAAyE;IACzE,oEAA+D;IAI/D,MAAa,QAAS,SAAQ,aAAK;QAM/B;;;;;;WAMG;QACH,YAAY,CAAC,IAAkB,EAAE,CAAS,EAAE,cAA8B;YACtE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;YAChB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;YACV,IAAI,CAAC,cAAc,GAAG,cAAc,CAAA;QACxC,CAAC;QAED;;;WAGG;QACH,YAAY,CAAC,QAAgB;YACzB,IAAI,CAAC,cAAc,GAAG,IAAI,qCAAiB,EAAE,CAAA;YAC7C,IAAI,KAAK,GAAG,IAAI,2BAAY,CAAC,QAAQ,CAAC,CAAA;YACtC,IA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,CAAC;KAEJ;IA5GD,4BA4GC"}
@@ -1,6 +1,6 @@
1
1
  import { DiscreteDistribution } from "nlptoolkit-math/dist/DiscreteDistribution";
2
- import { Instance } from "../Instance/Instance";
3
- import { ValidatedModel } from "./ValidatedModel";
2
+ import { Instance } from "../../Instance/Instance";
3
+ import { ValidatedModel } from "../ValidatedModel";
4
4
  import { FileContents } from "nlptoolkit-util/dist/FileContents";
5
5
  import { Vector } from "nlptoolkit-math/dist/Vector";
6
6
  export declare abstract class GaussianModel extends ValidatedModel {
@@ -22,7 +22,25 @@ export declare abstract class GaussianModel extends ValidatedModel {
22
22
  * @return The class which has the maximum value of metric.
23
23
  */
24
24
  predict(instance: Instance): string;
25
+ /**
26
+ * Loads the prior probability distribution from an input model file.
27
+ * @param input Input model file.
28
+ * @return Prior probability distribution.
29
+ * @throws IOException If the input file can not be read, the method throws IOException.
30
+ */
25
31
  loadPriorDistribution(input: FileContents): number;
32
+ /**
33
+ * Loads hash map of vectors from input model file.
34
+ * @param input Input model file.
35
+ * @param size Number of vectors to be read from input model file.
36
+ * @return Hash map of vectors.
37
+ * @throws IOException If the input file can not be read, the method throws IOException.
38
+ */
26
39
  loadVectors(input: FileContents, size: number): Map<string, Vector>;
40
+ /**
41
+ * Calculates the posterior probability distribution for the given instance according to Gaussian model.
42
+ * @param instance Instance for which posterior probability distribution is calculated.
43
+ * @return Posterior probability distribution for the given instance.
44
+ */
27
45
  predictProbability(instance: Instance): Map<string, number>;
28
46
  }
@@ -4,15 +4,15 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "nlptoolkit-math/dist/DiscreteDistribution", "./ValidatedModel", "../Instance/CompositeInstance", "nlptoolkit-math/dist/Vector"], factory);
7
+ define(["require", "exports", "nlptoolkit-math/dist/DiscreteDistribution", "../ValidatedModel", "../../Instance/CompositeInstance", "nlptoolkit-math/dist/Vector"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.GaussianModel = void 0;
13
13
  const DiscreteDistribution_1 = require("nlptoolkit-math/dist/DiscreteDistribution");
14
- const ValidatedModel_1 = require("./ValidatedModel");
15
- const CompositeInstance_1 = require("../Instance/CompositeInstance");
14
+ const ValidatedModel_1 = require("../ValidatedModel");
15
+ const CompositeInstance_1 = require("../../Instance/CompositeInstance");
16
16
  const Vector_1 = require("nlptoolkit-math/dist/Vector");
17
17
  class GaussianModel extends ValidatedModel_1.ValidatedModel {
18
18
  /**
@@ -52,6 +52,12 @@
52
52
  }
53
53
  return predictedClass;
54
54
  }
55
+ /**
56
+ * Loads the prior probability distribution from an input model file.
57
+ * @param input Input model file.
58
+ * @return Prior probability distribution.
59
+ * @throws IOException If the input file can not be read, the method throws IOException.
60
+ */
55
61
  loadPriorDistribution(input) {
56
62
  let size = parseInt(input.readLine());
57
63
  this.priorDistribution = new DiscreteDistribution_1.DiscreteDistribution();
@@ -64,6 +70,13 @@
64
70
  }
65
71
  return size;
66
72
  }
73
+ /**
74
+ * Loads hash map of vectors from input model file.
75
+ * @param input Input model file.
76
+ * @param size Number of vectors to be read from input model file.
77
+ * @return Hash map of vectors.
78
+ * @throws IOException If the input file can not be read, the method throws IOException.
79
+ */
67
80
  loadVectors(input, size) {
68
81
  let map = new Map();
69
82
  for (let i = 0; i < size; i++) {
@@ -77,6 +90,11 @@
77
90
  }
78
91
  return map;
79
92
  }
93
+ /**
94
+ * Calculates the posterior probability distribution for the given instance according to Gaussian model.
95
+ * @param instance Instance for which posterior probability distribution is calculated.
96
+ * @return Posterior probability distribution for the given instance.
97
+ */
80
98
  predictProbability(instance) {
81
99
  return undefined;
82
100
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GaussianModel.js","sourceRoot":"","sources":["../../../source/Model/Parametric/GaussianModel.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,oFAA+E;IAE/E,sDAAiD;IACjD,wEAAmE;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AAM,CAAC,CAAA;aAC5B;YACD,OAAO,GAAG,CAAA;QACd,CAAC;QAED;;;;WAIG;QACH,kBAAkB,CAAC,QAAkB;YACjC,OAAO,SAAS,CAAC;QACrB,CAAC;KACJ;IAjGD,sCAiGC"}
@@ -0,0 +1,36 @@
1
+ import { GaussianModel } from "./GaussianModel";
2
+ import { InstanceList } from "../../InstanceList/InstanceList";
3
+ import { Instance } from "../../Instance/Instance";
4
+ import { Parameter } from "../../Parameter/Parameter";
5
+ export declare class KMeansModel extends GaussianModel {
6
+ private classMeans;
7
+ private distanceMetric;
8
+ /**
9
+ * Loads a K-means model from an input model file.
10
+ * @param fileName Model file name.
11
+ */
12
+ constructor2(fileName: string): void;
13
+ /**
14
+ * The calculateMetric method takes an {@link Instance} and a String as inputs. It loops through the class means, if
15
+ * the corresponding class label is same as the given String it returns the negated distance between given instance and the
16
+ * current item of class means. Otherwise it returns the smallest negative number.
17
+ *
18
+ * @param instance {@link Instance} input.
19
+ * @param Ci String input.
20
+ * @return The negated distance between given instance and the current item of class means.
21
+ */
22
+ calculateMetric(instance: Instance, Ci: string): number;
23
+ saveTxt(fileName: string): void;
24
+ /**
25
+ * Training algorithm for K-Means classifier. K-Means finds the mean of each class for training.
26
+ *
27
+ * @param trainSet Training data given to the algorithm.
28
+ * @param parameters distanceMetric: distance metric used to calculate the distance between two instances.
29
+ */
30
+ train(trainSet: InstanceList, parameters: Parameter): void;
31
+ /**
32
+ * Loads the K-means model from an input file.
33
+ * @param fileName File name of the K-means model.
34
+ */
35
+ loadModel(fileName: string): void;
36
+ }