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
@@ -16,19 +16,46 @@ export class DecisionNode {
16
16
 
17
17
  children: Array<DecisionNode> = undefined
18
18
  private EPSILON: number = 0.0000000001;
19
- private data : InstanceList = undefined
20
19
  private classLabel : string = undefined
21
20
  leaf: boolean = false
22
21
  private condition: DecisionCondition = undefined
22
+ private classLabelsDistribution: DiscreteDistribution
23
23
 
24
+ /**
25
+ * The DecisionNode method takes {@link InstanceList} data as input and then it sets the class label parameter by finding
26
+ * the most occurred class label of given data, it then gets distinct class labels as class labels ArrayList. Later, it adds ordered
27
+ * indices to the indexList and shuffles them randomly. Then, it gets the class distribution of given data and finds the best entropy value
28
+ * of these class distribution.
29
+ * <p>
30
+ * If an attribute of given data is {@link DiscreteIndexedAttribute}, it creates a Distribution according to discrete indexed attribute class distribution
31
+ * and finds the entropy. If it is better than the last best entropy it reassigns the best entropy, best attribute and best split value according to
32
+ * the newly founded best entropy's index. At the end, it also add new distribution to the class distribution .
33
+ * <p>
34
+ * If an attribute of given data is {@link DiscreteAttribute}, it directly finds the entropy. If it is better than the last best entropy it
35
+ * reassigns the best entropy, best attribute and best split value according to the newly founded best entropy's index.
36
+ * <p>
37
+ * If an attribute of given data is {@link ContinuousAttribute}, it creates two distributions; left and right according to class distribution
38
+ * and discrete distribution respectively, and finds the entropy. If it is better than the last best entropy it reassigns the best entropy,
39
+ * best attribute and best split value according to the newly founded best entropy's index. At the end, it also add new distribution to
40
+ * the right distribution and removes from left distribution .
41
+ *
42
+ * @param data {@link InstanceList} input.
43
+ * @param condition {@link DecisionCondition} to check.
44
+ * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
45
+ * @param isStump Refers to decision trees with only 1 splitting rule.
46
+ */
24
47
  constructor1(data: InstanceList, condition?: DecisionCondition | number, parameter?: RandomForestParameter, isStump?: boolean){
25
48
  let bestAttribute = -1
26
49
  let bestSplitValue = 0
27
50
  if (condition instanceof DecisionCondition){
28
51
  this.condition = condition;
29
52
  }
30
- this.data = data;
31
- this.classLabel = Model.getMaximum(data.getClassLabels());
53
+ this.classLabelsDistribution = new DiscreteDistribution()
54
+ let labels = data.getClassLabels()
55
+ for (let label of labels){
56
+ this.classLabelsDistribution.addItem(label)
57
+ }
58
+ this.classLabel = Model.getMaximum(labels)
32
59
  this.leaf = true;
33
60
  let classLabels = data.getDistinctClassLabels();
34
61
  if (classLabels.length == 1) {
@@ -69,7 +96,7 @@ export class DecisionNode {
69
96
  }
70
97
  } else {
71
98
  if (data.get(0).getAttribute(index) instanceof DiscreteAttribute) {
72
- let entropy = this.entropyForDiscreteAttribute(index);
99
+ let entropy = this.entropyForDiscreteAttribute(data, index);
73
100
  if (entropy + this.EPSILON < bestEntropy) {
74
101
  bestEntropy = entropy;
75
102
  bestAttribute = index;
@@ -106,19 +133,23 @@ export class DecisionNode {
106
133
  if (bestAttribute != -1) {
107
134
  this.leaf = false;
108
135
  if (data.get(0).getAttribute(bestAttribute) instanceof DiscreteIndexedAttribute) {
109
- this.createChildrenForDiscreteIndexed(bestAttribute, bestSplitValue, parameter, isStump);
136
+ this.createChildrenForDiscreteIndexed(data, bestAttribute, bestSplitValue, parameter, isStump);
110
137
  } else {
111
138
  if (data.get(0).getAttribute(bestAttribute) instanceof DiscreteAttribute) {
112
- this.createChildrenForDiscrete(bestAttribute, parameter, isStump);
139
+ this.createChildrenForDiscrete(data, bestAttribute, parameter, isStump);
113
140
  } else {
114
141
  if (data.get(0).getAttribute(bestAttribute) instanceof ContinuousAttribute) {
115
- this.createChildrenForContinuous(bestAttribute, bestSplitValue, parameter, isStump);
142
+ this.createChildrenForContinuous(data, bestAttribute, bestSplitValue, parameter, isStump);
116
143
  }
117
144
  }
118
145
  }
119
146
  }
120
147
  }
121
148
 
149
+ /**
150
+ * Reads the decision node model (as one line) from model file.
151
+ * @param contents Model file
152
+ */
122
153
  constructor2(contents: FileContents){
123
154
  let items = contents.readLine().split(" ")
124
155
  if (items[0] != "-1"){
@@ -144,6 +175,7 @@ export class DecisionNode {
144
175
  } else {
145
176
  this.leaf = true
146
177
  this.classLabel = contents.readLine()
178
+ this.classLabelsDistribution = Model.loadDiscreteDistribution(contents);
147
179
  }
148
180
  }
149
181
 
@@ -184,14 +216,15 @@ export class DecisionNode {
184
216
  * The entropyForDiscreteAttribute method takes an attributeIndex and creates an ArrayList of DiscreteDistribution.
185
217
  * Then loops through the distributions and calculates the total entropy.
186
218
  *
219
+ * @param data Instance list.
187
220
  * @param attributeIndex Index of the attribute.
188
221
  * @return Total entropy for the discrete attribute.
189
222
  */
190
- private entropyForDiscreteAttribute(attributeIndex: number): number{
223
+ private entropyForDiscreteAttribute(data: InstanceList, attributeIndex: number): number{
191
224
  let sum = 0.0;
192
- let distributions = this.data.attributeClassDistribution(attributeIndex);
225
+ let distributions = data.attributeClassDistribution(attributeIndex);
193
226
  for (let distribution of distributions) {
194
- sum += (distribution.getSum() / this.data.size()) * distribution.entropy();
227
+ sum += (distribution.getSum() / data.size()) * distribution.entropy();
195
228
  }
196
229
  return sum;
197
230
  }
@@ -200,29 +233,31 @@ export class DecisionNode {
200
233
  * The createChildrenForDiscreteIndexed method creates an ArrayList of DecisionNodes as children and a partition with respect to
201
234
  * indexed attribute.
202
235
  *
236
+ * @param data Instance list.
203
237
  * @param attributeIndex Index of the attribute.
204
238
  * @param attributeValue Value of the attribute.
205
239
  * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
206
240
  * @param isStump Refers to decision trees with only 1 splitting rule.
207
241
  */
208
- private createChildrenForDiscreteIndexed(attributeIndex: number, attributeValue: number, parameter: RandomForestParameter, isStump: boolean){
209
- let childrenData = new Partition(this.data, attributeIndex, attributeValue);
242
+ private createChildrenForDiscreteIndexed(data: InstanceList, attributeIndex: number, attributeValue: number, parameter: RandomForestParameter, isStump: boolean){
243
+ let childrenData = new Partition(data, attributeIndex, attributeValue);
210
244
  this.children = new Array<DecisionNode>();
211
- this.children.push(new DecisionNode(childrenData.get(0), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", attributeValue, (<DiscreteIndexedAttribute> this.data.get(0).getAttribute(attributeIndex)).getMaxIndex())), parameter, isStump));
212
- this.children.push(new DecisionNode(childrenData.get(1), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", -1, (<DiscreteIndexedAttribute> this.data.get(0).getAttribute(attributeIndex)).getMaxIndex())), parameter, isStump));
245
+ this.children.push(new DecisionNode(childrenData.get(0), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", attributeValue, (<DiscreteIndexedAttribute> data.get(0).getAttribute(attributeIndex)).getMaxIndex())), parameter, isStump));
246
+ this.children.push(new DecisionNode(childrenData.get(1), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", -1, (<DiscreteIndexedAttribute> data.get(0).getAttribute(attributeIndex)).getMaxIndex())), parameter, isStump));
213
247
  }
214
248
 
215
249
  /**
216
250
  * The createChildrenForDiscrete method creates an ArrayList of values, a partition with respect to attributes and an ArrayList
217
251
  * of DecisionNodes as children.
218
252
  *
253
+ * @param data Instance list.
219
254
  * @param attributeIndex Index of the attribute.
220
255
  * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
221
256
  * @param isStump Refers to decision trees with only 1 splitting rule.
222
257
  */
223
- private createChildrenForDiscrete(attributeIndex: number, parameter: RandomForestParameter, isStump: boolean){
224
- let valueList = this.data.getAttributeValueList(attributeIndex);
225
- let childrenData = new Partition(this.data, attributeIndex);
258
+ private createChildrenForDiscrete(data: InstanceList, attributeIndex: number, parameter: RandomForestParameter, isStump: boolean){
259
+ let valueList = data.getAttributeValueList(attributeIndex);
260
+ let childrenData = new Partition(data, attributeIndex);
226
261
  this.children = new Array<DecisionNode>();
227
262
  for (let i = 0; i < valueList.length; i++) {
228
263
  this.children.push(new DecisionNode(childrenData.get(i), new DecisionCondition(attributeIndex, new DiscreteAttribute(valueList[i])), parameter, isStump));
@@ -233,13 +268,14 @@ export class DecisionNode {
233
268
  * The createChildrenForContinuous method creates an ArrayList of DecisionNodes as children and a partition with respect to
234
269
  * continuous attribute and the given split value.
235
270
  *
271
+ * @param data Instance list.
236
272
  * @param attributeIndex Index of the attribute.
237
273
  * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
238
274
  * @param isStump Refers to decision trees with only 1 splitting rule.
239
275
  * @param splitValue Split value is used for partitioning.
240
276
  */
241
- private createChildrenForContinuous(attributeIndex: number, splitValue: number, parameter: RandomForestParameter, isStump: boolean){
242
- let childrenData = new Partition(this.data, attributeIndex, splitValue + 0.0000001);
277
+ private createChildrenForContinuous(data: InstanceList, attributeIndex: number, splitValue: number, parameter: RandomForestParameter, isStump: boolean){
278
+ let childrenData = new Partition(data, attributeIndex, splitValue + 0.0000001);
243
279
  this.children = new Array<DecisionNode>();
244
280
  this.children.push(new DecisionNode(childrenData.get(0), new DecisionCondition(attributeIndex, new ContinuousAttribute(splitValue), "<"), parameter, isStump));
245
281
  this.children.push(new DecisionNode(childrenData.get(1), new DecisionCondition(attributeIndex, new ContinuousAttribute(splitValue), ">"), parameter, isStump));
@@ -255,7 +291,7 @@ export class DecisionNode {
255
291
  predict(instance: Instance): string{
256
292
  if (instance instanceof CompositeInstance) {
257
293
  let possibleClassLabels = (<CompositeInstance> instance).getPossibleClassLabels();
258
- let distribution = this.data.classDistribution();
294
+ let distribution = this.classLabelsDistribution;
259
295
  let predictedClass = distribution.getMaxItem(possibleClassLabels);
260
296
  if (this.leaf) {
261
297
  return predictedClass;
@@ -286,16 +322,23 @@ export class DecisionNode {
286
322
  }
287
323
  }
288
324
 
325
+ /**
326
+ * Recursive method that returns the posterior probability distribution of a given instance. If the node is a leaf
327
+ * node, it returns the class label distribution, otherwise it checks in which direction (child node) this instance
328
+ * is forwarded.
329
+ * @param instance Instance for which the posterior probability distribution is calculated.
330
+ * @return Posterior probability distribution for this instance.
331
+ */
289
332
  predictProbabilityDistribution(instance: Instance): Map<string, number>{
290
333
  if (this.leaf) {
291
- return this.data.classDistribution().getProbabilityDistribution();
334
+ return this.classLabelsDistribution.getProbabilityDistribution();
292
335
  } else {
293
336
  for (let node of this.children) {
294
337
  if (node.condition.satisfy(instance)) {
295
338
  return node.predictProbabilityDistribution(instance);
296
339
  }
297
340
  }
298
- return this.data.classDistribution().getProbabilityDistribution();
341
+ return this.classLabelsDistribution.getProbabilityDistribution();
299
342
  }
300
343
  }
301
344
  }
@@ -0,0 +1,26 @@
1
+ import {DecisionTree} from "./DecisionTree";
2
+ import {InstanceList} from "../../InstanceList/InstanceList";
3
+ import {Parameter} from "../../Parameter/Parameter";
4
+ import {DecisionNode} from "./DecisionNode";
5
+
6
+ export class DecisionStump extends DecisionTree {
7
+
8
+ /**
9
+ * Training algorithm for C4.5 Stump univariate decision tree classifier.
10
+ *
11
+ * @param trainSet Training data given to the algorithm.
12
+ * @param parameters -
13
+ */
14
+ train(trainSet: InstanceList, parameters: Parameter): void {
15
+ this.root = new DecisionNode(trainSet, undefined, undefined, true);
16
+ }
17
+
18
+ /**
19
+ * Loads the decision tree model from an input file.
20
+ * @param fileName File name of the decision tree model.
21
+ */
22
+ loadModel(fileName: string): void{
23
+ this.constructor2(fileName)
24
+ }
25
+
26
+ }
@@ -4,29 +4,27 @@ import {Instance} from "../../Instance/Instance";
4
4
  import {CompositeInstance} from "../../Instance/CompositeInstance";
5
5
  import {InstanceList} from "../../InstanceList/InstanceList";
6
6
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
7
+ import {Parameter} from "../../Parameter/Parameter";
8
+ import {C45Parameter} from "../../Parameter/C45Parameter";
9
+ import {Partition} from "../../InstanceList/Partition";
7
10
 
8
11
  export class DecisionTree extends ValidatedModel{
9
12
 
10
- private readonly root: DecisionNode
13
+ protected root: DecisionNode
11
14
 
12
- /**
13
- * Constructor that sets root node of the decision tree.
14
- *
15
- * @param rootOrFileName DecisionNode type input or fileName
16
- */
17
- constructor(rootOrFileName: DecisionNode | string) {
15
+ constructor2(fileName: string) {
16
+ let contents = new FileContents(fileName)
17
+ this.root = new DecisionNode(contents)
18
+ }
19
+
20
+ constructor(root?: DecisionNode) {
18
21
  super();
19
- if (rootOrFileName instanceof DecisionNode){
20
- this.root = rootOrFileName
21
- } else {
22
- let contents = new FileContents(rootOrFileName)
23
- this.root = new DecisionNode(contents)
24
- }
22
+ this.root = root;
25
23
  }
26
24
 
27
25
  /**
28
- * The predict method performs prediction on the root node of given instance, and if it is null, it returns the possible class labels.
29
- * Otherwise it returns the returned class labels.
26
+ * The predict method performs prediction on the root node of given instance, and if it is null, it returns the
27
+ * possible class labels. Otherwise, it returns the returned class labels.
30
28
  *
31
29
  * @param instance Instance make prediction.
32
30
  * @return Possible class labels.
@@ -39,6 +37,11 @@ export class DecisionTree extends ValidatedModel{
39
37
  return predictedClass;
40
38
  }
41
39
 
40
+ /**
41
+ * Calculates the posterior probability distribution for the given instance according to Decision tree model.
42
+ * @param instance Instance for which posterior probability distribution is calculated.
43
+ * @return Posterior probability distribution for the given instance.
44
+ */
42
45
  predictProbability(instance: Instance): Map<string, number> {
43
46
  return this.root.predictProbabilityDistribution(instance)
44
47
  }
@@ -77,4 +80,30 @@ export class DecisionTree extends ValidatedModel{
77
80
  prune(pruneSet: InstanceList){
78
81
  this.pruneNode(this.root, pruneSet)
79
82
  }
83
+
84
+ /**
85
+ * Training algorithm for C4.5 univariate decision tree classifier. 20 percent of the data are left aside for pruning
86
+ * 80 percent of the data is used for constructing the tree.
87
+ *
88
+ * @param trainSet Training data given to the algorithm.
89
+ * @param parameters -
90
+ */
91
+ train(trainSet: InstanceList, parameters: Parameter): void {
92
+ if ((<C45Parameter> parameters).isPrune()) {
93
+ let partition = new Partition(trainSet, (<C45Parameter> parameters).getCrossValidationRatio(), true);
94
+ this.root = new DecisionNode(partition.get(1), undefined, undefined, false);
95
+ this.prune(partition.get(0));
96
+ } else {
97
+ this.root = new DecisionNode(trainSet, undefined, undefined, false);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Loads the decision tree model from an input file.
103
+ * @param fileName File name of the decision tree model.
104
+ */
105
+ loadModel(fileName: string): void{
106
+ this.constructor2(fileName)
107
+ }
108
+
80
109
  }
@@ -4,33 +4,29 @@ import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
4
4
  import {InstanceList} from "../InstanceList/InstanceList";
5
5
  import {CompositeInstance} from "../Instance/CompositeInstance";
6
6
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
7
+ import {Parameter} from "../Parameter/Parameter";
7
8
 
8
9
  export class DummyModel extends Model{
9
10
 
10
11
  private distribution: DiscreteDistribution
11
12
 
12
13
  /**
13
- * Constructor which sets the distribution using the given {@link InstanceList}.
14
+ * Training algorithm for the dummy classifier. Actually dummy classifier returns the maximum occurring class in
15
+ * the training data, there is no training. Sets the distribution using the given {@link InstanceList}.
14
16
  *
15
- * @param trainSet {@link InstanceList} which is used to get the class distribution.
17
+ * @param trainSet Training data given to the algorithm.
16
18
  */
17
- constructor(trainSet: InstanceList | string) {
18
- super();
19
- if (trainSet instanceof InstanceList){
20
- this.distribution = trainSet.classDistribution();
21
- } else {
22
- let input = new FileContents(trainSet)
23
- this.distribution = new DiscreteDistribution()
24
- let size = parseInt(input.readLine())
25
- for (let i = 0; i < size; i++){
26
- let line = input.readLine()
27
- let items = line.split(" ")
28
- let count = parseInt(items[1])
29
- for (let j = 0; j < count; j++){
30
- this.distribution.addItem(items[0])
31
- }
32
- }
33
- }
19
+ constructor1(trainSet: InstanceList){
20
+ this.distribution = trainSet.classDistribution()
21
+ }
22
+
23
+ /**
24
+ * Loads the dummy model from an input file.
25
+ * @param fileName File name of the dummy model.
26
+ */
27
+ constructor2(fileName: string) {
28
+ let input = new FileContents(fileName)
29
+ this.distribution = Model.loadDiscreteDistribution(input)
34
30
  }
35
31
 
36
32
  /**
@@ -48,6 +44,11 @@ export class DummyModel extends Model{
48
44
  }
49
45
  }
50
46
 
47
+ /**
48
+ * Calculates the posterior probability distribution for the given instance according to dummy model.
49
+ * @param instance Instance for which posterior probability distribution is calculated.
50
+ * @return Posterior probability distribution for the given instance.
51
+ */
51
52
  predictProbability(instance: Instance): Map<string, number> {
52
53
  return this.distribution.getProbabilityDistribution();
53
54
  }
@@ -55,4 +56,24 @@ export class DummyModel extends Model{
55
56
  saveTxt(fileName: string){
56
57
  }
57
58
 
59
+ /**
60
+ * Training algorithm for the dummy classifier. Actually dummy classifier returns the maximum occurring class in
61
+ * the training data, there is no training.
62
+ *
63
+ * @param trainSet Training data given to the algorithm.
64
+ * @param parameters -
65
+ */
66
+ train(trainSet: InstanceList, parameters: Parameter): void {
67
+ this.constructor1(trainSet)
68
+ }
69
+
70
+ /**
71
+ * Loads the dummy model from an input file.
72
+ * @param fileName File name of the dummy model.
73
+ */
74
+ loadModel(fileName: string): void{
75
+ this.constructor2(fileName)
76
+ }
77
+
78
+
58
79
  }
@@ -1,12 +1,11 @@
1
- import {Classifier} from "./Classifier";
2
- import {InstanceList} from "../InstanceList/InstanceList";
3
- import {Parameter} from "../Parameter/Parameter";
4
- import {BaggingParameter} from "../Parameter/BaggingParameter";
5
- import {DecisionTree} from "../Model/DecisionTree/DecisionTree";
6
- import {DecisionNode} from "../Model/DecisionTree/DecisionNode";
7
- import {TreeEnsembleModel} from "../Model/TreeEnsembleModel";
1
+ import {TreeEnsembleModel} from "./TreeEnsembleModel";
2
+ import {InstanceList} from "../../InstanceList/InstanceList";
3
+ import {Parameter} from "../../Parameter/Parameter";
4
+ import {BaggingParameter} from "../../Parameter/BaggingParameter";
5
+ import {DecisionTree} from "../DecisionTree/DecisionTree";
6
+ import {DecisionNode} from "../DecisionTree/DecisionNode";
8
7
 
9
- export class Bagging extends Classifier{
8
+ export class BaggingModel extends TreeEnsembleModel{
10
9
 
11
10
  /**
12
11
  * Bagging bootstrap ensemble method that creates individuals for its ensemble by training each classifier on a random
@@ -20,16 +19,20 @@ export class Bagging extends Classifier{
20
19
  */
21
20
  train(trainSet: InstanceList, parameters: Parameter): void {
22
21
  let forestSize = (<BaggingParameter> parameters).getEnsembleSize();
23
- let forest = new Array<DecisionTree>();
22
+ this.forest = new Array<DecisionTree>();
24
23
  for (let i = 0; i < forestSize; i++) {
25
24
  let bootstrap = trainSet.bootstrap(i);
26
25
  let tree = new DecisionTree(new DecisionNode(new InstanceList(bootstrap.getSample()), undefined, undefined, false));
27
- forest.push(tree);
26
+ this.forest.push(tree);
28
27
  }
29
- this.model = new TreeEnsembleModel(forest);
30
28
  }
31
29
 
30
+ /**
31
+ * Loads the Bagging ensemble model from an input file.
32
+ * @param fileName File name of the decision tree model.
33
+ */
32
34
  loadModel(fileName: string): void{
33
- this.model = new TreeEnsembleModel(fileName)
35
+ this.constructor2(fileName)
34
36
  }
37
+
35
38
  }
@@ -1,12 +1,11 @@
1
- import {Classifier} from "./Classifier";
2
- import {InstanceList} from "../InstanceList/InstanceList";
3
- import {Parameter} from "../Parameter/Parameter";
4
- import {RandomForestParameter} from "../Parameter/RandomForestParameter";
5
- import {DecisionTree} from "../Model/DecisionTree/DecisionTree";
6
- import {DecisionNode} from "../Model/DecisionTree/DecisionNode";
7
- import {TreeEnsembleModel} from "../Model/TreeEnsembleModel";
1
+ import {TreeEnsembleModel} from "./TreeEnsembleModel";
2
+ import {InstanceList} from "../../InstanceList/InstanceList";
3
+ import {Parameter} from "../../Parameter/Parameter";
4
+ import {RandomForestParameter} from "../../Parameter/RandomForestParameter";
5
+ import {DecisionTree} from "../DecisionTree/DecisionTree";
6
+ import {DecisionNode} from "../DecisionTree/DecisionNode";
8
7
 
9
- export class RandomForest extends Classifier{
8
+ export class RandomForestModel extends TreeEnsembleModel{
10
9
 
11
10
  /**
12
11
  * Training algorithm for random forest classifier. Basically the algorithm creates K distinct decision trees from
@@ -17,17 +16,20 @@ export class RandomForest extends Classifier{
17
16
  */
18
17
  train(trainSet: InstanceList, parameters: Parameter): void {
19
18
  let forestSize = (<RandomForestParameter> parameters).getEnsembleSize();
20
- let forest = new Array<DecisionTree>();
19
+ this.forest = new Array<DecisionTree>();
21
20
  for (let i = 0; i < forestSize; i++){
22
21
  let bootstrap = trainSet.bootstrap(i);
23
22
  let tree = new DecisionTree(new DecisionNode(new InstanceList(bootstrap.getSample()), undefined, <RandomForestParameter> parameters, false));
24
- forest.push(tree);
23
+ this.forest.push(tree);
25
24
  }
26
- this.model = new TreeEnsembleModel(forest);
27
25
  }
28
26
 
27
+ /**
28
+ * Loads the random forest model from an input file.
29
+ * @param fileName File name of the random forest model.
30
+ */
29
31
  loadModel(fileName: string): void{
30
- this.model = new TreeEnsembleModel(fileName)
32
+ this.constructor2(fileName)
31
33
  }
32
34
 
33
35
  }
@@ -1,30 +1,33 @@
1
- import {Model} from "./Model";
2
- import {Instance} from "../Instance/Instance";
3
- import {DecisionTree} from "./DecisionTree/DecisionTree";
1
+ import {Model} from "../Model";
2
+ import {Instance} from "../../Instance/Instance";
3
+ import {DecisionTree} from "../DecisionTree/DecisionTree";
4
4
  import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
5
5
  import {FileContents} from "nlptoolkit-util/dist/FileContents";
6
- import {DecisionNode} from "./DecisionTree/DecisionNode";
6
+ import {DecisionNode} from "../DecisionTree/DecisionNode";
7
7
 
8
- export class TreeEnsembleModel extends Model{
8
+ export abstract class TreeEnsembleModel extends Model{
9
9
 
10
- private forest: Array<DecisionTree>
10
+ protected forest: Array<DecisionTree>
11
11
 
12
12
  /**
13
13
  * A constructor which sets the {@link Array} of {@link DecisionTree} with given input.
14
14
  *
15
- * @param forestOrFileName An {@link Array} of {@link DecisionTree}.
15
+ * @param forest An {@link Array} of {@link DecisionTree}.
16
16
  */
17
- constructor(forestOrFileName: Array<DecisionTree> | string) {
18
- super();
19
- if (forestOrFileName instanceof Array){
20
- this.forest = forestOrFileName
21
- } else {
22
- let input = new FileContents(forestOrFileName)
23
- let numberOfTrees = parseInt(input.readLine())
24
- this.forest = new Array<DecisionTree>()
25
- for (let i = 0; i < numberOfTrees; i++){
26
- this.forest.push(new DecisionTree(new DecisionNode(input)))
27
- }
17
+ constructor1(forest: Array<DecisionTree>) {
18
+ this.forest = forest
19
+ }
20
+
21
+ /**
22
+ * Loads a tree ensemble model such as Random Forest model or Bagging model from an input model file.
23
+ * @param fileName Model file name.
24
+ */
25
+ constructor2(fileName: string) {
26
+ let input = new FileContents(fileName)
27
+ let numberOfTrees = parseInt(input.readLine())
28
+ this.forest = new Array<DecisionTree>()
29
+ for (let i = 0; i < numberOfTrees; i++){
30
+ this.forest.push(new DecisionTree(new DecisionNode(input)))
28
31
  }
29
32
  }
30
33
 
@@ -43,6 +46,11 @@ export class TreeEnsembleModel extends Model{
43
46
  return distribution.getMaxItem();
44
47
  }
45
48
 
49
+ /**
50
+ * Calculates the posterior probability distribution for the given instance according to ensemble tree model.
51
+ * @param instance Instance for which posterior probability distribution is calculated.
52
+ * @return Posterior probability distribution for the given instance.
53
+ */
46
54
  predictProbability(instance: Instance): Map<string, number> {
47
55
  let distribution = new DiscreteDistribution();
48
56
  for (let tree of this.forest) {