nlptoolkit-classification 1.0.0

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 (412) hide show
  1. package/README.md +161 -0
  2. package/datasets/bupa.data +345 -0
  3. package/datasets/car.data +1728 -0
  4. package/datasets/chess.data +28056 -0
  5. package/datasets/dermatology.data +366 -0
  6. package/datasets/iris.data +150 -0
  7. package/datasets/nursery.data +12960 -0
  8. package/datasets/tictactoe.data +958 -0
  9. package/dist/Attribute/Attribute.d.ts +4 -0
  10. package/dist/Attribute/Attribute.js +17 -0
  11. package/dist/Attribute/Attribute.js.map +1 -0
  12. package/dist/Attribute/AttributeType.d.ts +18 -0
  13. package/dist/Attribute/AttributeType.js +33 -0
  14. package/dist/Attribute/AttributeType.js.map +1 -0
  15. package/dist/Attribute/BinaryAttribute.d.ts +9 -0
  16. package/dist/Attribute/BinaryAttribute.js +26 -0
  17. package/dist/Attribute/BinaryAttribute.js.map +1 -0
  18. package/dist/Attribute/ContinuousAttribute.d.ts +30 -0
  19. package/dist/Attribute/ContinuousAttribute.js +59 -0
  20. package/dist/Attribute/ContinuousAttribute.js.map +1 -0
  21. package/dist/Attribute/DiscreteAttribute.d.ts +24 -0
  22. package/dist/Attribute/DiscreteAttribute.js +53 -0
  23. package/dist/Attribute/DiscreteAttribute.js.map +1 -0
  24. package/dist/Attribute/DiscreteIndexedAttribute.d.ts +27 -0
  25. package/dist/Attribute/DiscreteIndexedAttribute.js +61 -0
  26. package/dist/Attribute/DiscreteIndexedAttribute.js.map +1 -0
  27. package/dist/Classifier/Bagging.d.ts +16 -0
  28. package/dist/Classifier/Bagging.js +42 -0
  29. package/dist/Classifier/Bagging.js.map +1 -0
  30. package/dist/Classifier/C45.d.ts +13 -0
  31. package/dist/Classifier/C45.js +40 -0
  32. package/dist/Classifier/C45.js.map +1 -0
  33. package/dist/Classifier/C45Stump.d.ts +12 -0
  34. package/dist/Classifier/C45Stump.js +29 -0
  35. package/dist/Classifier/C45Stump.js.map +1 -0
  36. package/dist/Classifier/Classifier.d.ts +39 -0
  37. package/dist/Classifier/Classifier.js +72 -0
  38. package/dist/Classifier/Classifier.js.map +1 -0
  39. package/dist/Classifier/DeepNetwork.d.ts +13 -0
  40. package/dist/Classifier/DeepNetwork.js +31 -0
  41. package/dist/Classifier/DeepNetwork.js.map +1 -0
  42. package/dist/Classifier/Dummy.d.ts +13 -0
  43. package/dist/Classifier/Dummy.js +29 -0
  44. package/dist/Classifier/Dummy.js.map +1 -0
  45. package/dist/Classifier/KMeans.d.ts +12 -0
  46. package/dist/Classifier/KMeans.js +36 -0
  47. package/dist/Classifier/KMeans.js.map +1 -0
  48. package/dist/Classifier/Knn.d.ts +13 -0
  49. package/dist/Classifier/Knn.js +29 -0
  50. package/dist/Classifier/Knn.js.map +1 -0
  51. package/dist/Classifier/Lda.d.ts +12 -0
  52. package/dist/Classifier/Lda.js +52 -0
  53. package/dist/Classifier/Lda.js.map +1 -0
  54. package/dist/Classifier/LinearPerceptron.d.ts +14 -0
  55. package/dist/Classifier/LinearPerceptron.js +32 -0
  56. package/dist/Classifier/LinearPerceptron.js.map +1 -0
  57. package/dist/Classifier/MultiLayerPerceptron.d.ts +14 -0
  58. package/dist/Classifier/MultiLayerPerceptron.js +32 -0
  59. package/dist/Classifier/MultiLayerPerceptron.js.map +1 -0
  60. package/dist/Classifier/NaiveBayes.d.ts +25 -0
  61. package/dist/Classifier/NaiveBayes.js +67 -0
  62. package/dist/Classifier/NaiveBayes.js.map +1 -0
  63. package/dist/Classifier/Qda.d.ts +12 -0
  64. package/dist/Classifier/Qda.js +50 -0
  65. package/dist/Classifier/Qda.js.map +1 -0
  66. package/dist/Classifier/RandomClassifier.d.ts +12 -0
  67. package/dist/Classifier/RandomClassifier.js +32 -0
  68. package/dist/Classifier/RandomClassifier.js.map +1 -0
  69. package/dist/Classifier/RandomForest.d.ts +13 -0
  70. package/dist/Classifier/RandomForest.js +39 -0
  71. package/dist/Classifier/RandomForest.js.map +1 -0
  72. package/dist/DataSet/DataDefinition.d.ts +59 -0
  73. package/dist/DataSet/DataDefinition.js +108 -0
  74. package/dist/DataSet/DataDefinition.js.map +1 -0
  75. package/dist/DataSet/DataSet.d.ts +111 -0
  76. package/dist/DataSet/DataSet.js +286 -0
  77. package/dist/DataSet/DataSet.js.map +1 -0
  78. package/dist/DistanceMetric/DistanceMetric.d.ts +4 -0
  79. package/dist/DistanceMetric/DistanceMetric.js +13 -0
  80. package/dist/DistanceMetric/DistanceMetric.js.map +1 -0
  81. package/dist/DistanceMetric/EuclidianDistance.d.ts +13 -0
  82. package/dist/DistanceMetric/EuclidianDistance.js +43 -0
  83. package/dist/DistanceMetric/EuclidianDistance.js.map +1 -0
  84. package/dist/DistanceMetric/MahalanobisDistance.d.ts +20 -0
  85. package/dist/DistanceMetric/MahalanobisDistance.js +39 -0
  86. package/dist/DistanceMetric/MahalanobisDistance.js.map +1 -0
  87. package/dist/Experiment/BootstrapRun.d.ts +19 -0
  88. package/dist/Experiment/BootstrapRun.js +44 -0
  89. package/dist/Experiment/BootstrapRun.js.map +1 -0
  90. package/dist/Experiment/Experiment.d.ts +37 -0
  91. package/dist/Experiment/Experiment.js +57 -0
  92. package/dist/Experiment/Experiment.js.map +1 -0
  93. package/dist/Experiment/KFoldRun.d.ts +19 -0
  94. package/dist/Experiment/KFoldRun.js +43 -0
  95. package/dist/Experiment/KFoldRun.js.map +1 -0
  96. package/dist/Experiment/KFoldRunSeparateTest.d.ts +24 -0
  97. package/dist/Experiment/KFoldRunSeparateTest.js +51 -0
  98. package/dist/Experiment/KFoldRunSeparateTest.js.map +1 -0
  99. package/dist/Experiment/MultipleRun.d.ts +5 -0
  100. package/dist/Experiment/MultipleRun.js +13 -0
  101. package/dist/Experiment/MultipleRun.js.map +1 -0
  102. package/dist/Experiment/MxKFoldRun.d.ts +20 -0
  103. package/dist/Experiment/MxKFoldRun.js +44 -0
  104. package/dist/Experiment/MxKFoldRun.js.map +1 -0
  105. package/dist/Experiment/MxKFoldRunSeparateTest.d.ts +21 -0
  106. package/dist/Experiment/MxKFoldRunSeparateTest.js +48 -0
  107. package/dist/Experiment/MxKFoldRunSeparateTest.js.map +1 -0
  108. package/dist/Experiment/SingleRun.d.ts +5 -0
  109. package/dist/Experiment/SingleRun.js +13 -0
  110. package/dist/Experiment/SingleRun.js.map +1 -0
  111. package/dist/Experiment/SingleRunWithK.d.ts +19 -0
  112. package/dist/Experiment/SingleRunWithK.js +37 -0
  113. package/dist/Experiment/SingleRunWithK.js.map +1 -0
  114. package/dist/Experiment/StratifiedKFoldRun.d.ts +17 -0
  115. package/dist/Experiment/StratifiedKFoldRun.js +39 -0
  116. package/dist/Experiment/StratifiedKFoldRun.js.map +1 -0
  117. package/dist/Experiment/StratifiedKFoldRunSeparateTest.d.ts +18 -0
  118. package/dist/Experiment/StratifiedKFoldRunSeparateTest.js +43 -0
  119. package/dist/Experiment/StratifiedKFoldRunSeparateTest.js.map +1 -0
  120. package/dist/Experiment/StratifiedMxKFoldRun.d.ts +19 -0
  121. package/dist/Experiment/StratifiedMxKFoldRun.js +43 -0
  122. package/dist/Experiment/StratifiedMxKFoldRun.js.map +1 -0
  123. package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.d.ts +20 -0
  124. package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js +47 -0
  125. package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js.map +1 -0
  126. package/dist/Experiment/StratifiedSingleRunWithK.d.ts +18 -0
  127. package/dist/Experiment/StratifiedSingleRunWithK.js +39 -0
  128. package/dist/Experiment/StratifiedSingleRunWithK.js.map +1 -0
  129. package/dist/FeatureSelection/BackwardSelection.d.ts +18 -0
  130. package/dist/FeatureSelection/BackwardSelection.js +39 -0
  131. package/dist/FeatureSelection/BackwardSelection.js.map +1 -0
  132. package/dist/FeatureSelection/FeatureSubSet.d.ts +47 -0
  133. package/dist/FeatureSelection/FeatureSubSet.js +89 -0
  134. package/dist/FeatureSelection/FeatureSubSet.js.map +1 -0
  135. package/dist/FeatureSelection/FloatingSelection.d.ts +16 -0
  136. package/dist/FeatureSelection/FloatingSelection.js +38 -0
  137. package/dist/FeatureSelection/FloatingSelection.js.map +1 -0
  138. package/dist/FeatureSelection/ForwardSelection.d.ts +17 -0
  139. package/dist/FeatureSelection/ForwardSelection.js +38 -0
  140. package/dist/FeatureSelection/ForwardSelection.js.map +1 -0
  141. package/dist/FeatureSelection/SubSetSelection.d.ts +37 -0
  142. package/dist/FeatureSelection/SubSetSelection.js +90 -0
  143. package/dist/FeatureSelection/SubSetSelection.js.map +1 -0
  144. package/dist/Filter/DiscreteToContinuous.d.ts +25 -0
  145. package/dist/Filter/DiscreteToContinuous.js +69 -0
  146. package/dist/Filter/DiscreteToContinuous.js.map +1 -0
  147. package/dist/Filter/DiscreteToIndexed.d.ts +21 -0
  148. package/dist/Filter/DiscreteToIndexed.js +56 -0
  149. package/dist/Filter/DiscreteToIndexed.js.map +1 -0
  150. package/dist/Filter/FeatureFilter.d.ts +18 -0
  151. package/dist/Filter/FeatureFilter.js +36 -0
  152. package/dist/Filter/FeatureFilter.js.map +1 -0
  153. package/dist/Filter/LaryFilter.d.ts +27 -0
  154. package/dist/Filter/LaryFilter.js +62 -0
  155. package/dist/Filter/LaryFilter.js.map +1 -0
  156. package/dist/Filter/LaryToBinary.d.ts +23 -0
  157. package/dist/Filter/LaryToBinary.js +67 -0
  158. package/dist/Filter/LaryToBinary.js.map +1 -0
  159. package/dist/Filter/Normalize.d.ts +21 -0
  160. package/dist/Filter/Normalize.js +47 -0
  161. package/dist/Filter/Normalize.js.map +1 -0
  162. package/dist/Filter/Pca.d.ts +44 -0
  163. package/dist/Filter/Pca.js +109 -0
  164. package/dist/Filter/Pca.js.map +1 -0
  165. package/dist/Filter/TrainedFeatureFilter.d.ts +11 -0
  166. package/dist/Filter/TrainedFeatureFilter.js +26 -0
  167. package/dist/Filter/TrainedFeatureFilter.js.map +1 -0
  168. package/dist/Instance/CompositeInstance.d.ts +32 -0
  169. package/dist/Instance/CompositeInstance.js +61 -0
  170. package/dist/Instance/CompositeInstance.js.map +1 -0
  171. package/dist/Instance/Instance.d.ts +89 -0
  172. package/dist/Instance/Instance.js +170 -0
  173. package/dist/Instance/Instance.js.map +1 -0
  174. package/dist/InstanceList/InstanceList.d.ts +188 -0
  175. package/dist/InstanceList/InstanceList.js +517 -0
  176. package/dist/InstanceList/InstanceList.js.map +1 -0
  177. package/dist/InstanceList/InstanceListOfSameClass.d.ts +16 -0
  178. package/dist/InstanceList/InstanceListOfSameClass.js +35 -0
  179. package/dist/InstanceList/InstanceListOfSameClass.js.map +1 -0
  180. package/dist/InstanceList/Partition.d.ts +31 -0
  181. package/dist/InstanceList/Partition.js +143 -0
  182. package/dist/InstanceList/Partition.js.map +1 -0
  183. package/dist/Model/DecisionTree/DecisionCondition.d.ts +34 -0
  184. package/dist/Model/DecisionTree/DecisionCondition.js +84 -0
  185. package/dist/Model/DecisionTree/DecisionCondition.js.map +1 -0
  186. package/dist/Model/DecisionTree/DecisionNode.d.ts +82 -0
  187. package/dist/Model/DecisionTree/DecisionNode.js +271 -0
  188. package/dist/Model/DecisionTree/DecisionNode.js.map +1 -0
  189. package/dist/Model/DecisionTree/DecisionTree.d.ts +37 -0
  190. package/dist/Model/DecisionTree/DecisionTree.js +75 -0
  191. package/dist/Model/DecisionTree/DecisionTree.js.map +1 -0
  192. package/dist/Model/DeepNetworkModel.d.ts +42 -0
  193. package/dist/Model/DeepNetworkModel.js +163 -0
  194. package/dist/Model/DeepNetworkModel.js.map +1 -0
  195. package/dist/Model/DummyModel.d.ts +20 -0
  196. package/dist/Model/DummyModel.js +46 -0
  197. package/dist/Model/DummyModel.js.map +1 -0
  198. package/dist/Model/GaussianModel.d.ts +24 -0
  199. package/dist/Model/GaussianModel.js +59 -0
  200. package/dist/Model/GaussianModel.js.map +1 -0
  201. package/dist/Model/KMeansModel.d.ts +27 -0
  202. package/dist/Model/KMeansModel.js +48 -0
  203. package/dist/Model/KMeansModel.js.map +1 -0
  204. package/dist/Model/KnnInstance.d.ts +14 -0
  205. package/dist/Model/KnnInstance.js +33 -0
  206. package/dist/Model/KnnInstance.js.map +1 -0
  207. package/dist/Model/KnnModel.d.ts +36 -0
  208. package/dist/Model/KnnModel.js +83 -0
  209. package/dist/Model/KnnModel.js.map +1 -0
  210. package/dist/Model/LdaModel.d.ts +25 -0
  211. package/dist/Model/LdaModel.js +45 -0
  212. package/dist/Model/LdaModel.js.map +1 -0
  213. package/dist/Model/LinearPerceptronModel.d.ts +21 -0
  214. package/dist/Model/LinearPerceptronModel.js +62 -0
  215. package/dist/Model/LinearPerceptronModel.js.map +1 -0
  216. package/dist/Model/Model.d.ts +12 -0
  217. package/dist/Model/Model.js +31 -0
  218. package/dist/Model/Model.js.map +1 -0
  219. package/dist/Model/MultiLayerPerceptronModel.d.ts +29 -0
  220. package/dist/Model/MultiLayerPerceptronModel.js +101 -0
  221. package/dist/Model/MultiLayerPerceptronModel.js.map +1 -0
  222. package/dist/Model/NaiveBayesModel.d.ts +47 -0
  223. package/dist/Model/NaiveBayesModel.js +95 -0
  224. package/dist/Model/NaiveBayesModel.js.map +1 -0
  225. package/dist/Model/NeuralNetworkModel.d.ts +99 -0
  226. package/dist/Model/NeuralNetworkModel.js +180 -0
  227. package/dist/Model/NeuralNetworkModel.js.map +1 -0
  228. package/dist/Model/QdaModel.d.ts +26 -0
  229. package/dist/Model/QdaModel.js +45 -0
  230. package/dist/Model/QdaModel.js.map +1 -0
  231. package/dist/Model/RandomModel.d.ts +16 -0
  232. package/dist/Model/RandomModel.js +51 -0
  233. package/dist/Model/RandomModel.js.map +1 -0
  234. package/dist/Model/TreeEnsembleModel.d.ts +21 -0
  235. package/dist/Model/TreeEnsembleModel.js +49 -0
  236. package/dist/Model/TreeEnsembleModel.js.map +1 -0
  237. package/dist/Model/ValidatedModel.d.ts +12 -0
  238. package/dist/Model/ValidatedModel.js +35 -0
  239. package/dist/Model/ValidatedModel.js.map +1 -0
  240. package/dist/Parameter/ActivationFunction.d.ts +5 -0
  241. package/dist/Parameter/ActivationFunction.js +20 -0
  242. package/dist/Parameter/ActivationFunction.js.map +1 -0
  243. package/dist/Parameter/BaggingParameter.d.ts +17 -0
  244. package/dist/Parameter/BaggingParameter.js +36 -0
  245. package/dist/Parameter/BaggingParameter.js.map +1 -0
  246. package/dist/Parameter/C45Parameter.d.ts +25 -0
  247. package/dist/Parameter/C45Parameter.js +46 -0
  248. package/dist/Parameter/C45Parameter.js.map +1 -0
  249. package/dist/Parameter/DeepNetworkParameter.d.ts +38 -0
  250. package/dist/Parameter/DeepNetworkParameter.js +60 -0
  251. package/dist/Parameter/DeepNetworkParameter.js.map +1 -0
  252. package/dist/Parameter/KMeansParameter.d.ts +18 -0
  253. package/dist/Parameter/KMeansParameter.js +42 -0
  254. package/dist/Parameter/KMeansParameter.js.map +1 -0
  255. package/dist/Parameter/KnnParameter.d.ts +19 -0
  256. package/dist/Parameter/KnnParameter.js +37 -0
  257. package/dist/Parameter/KnnParameter.js.map +1 -0
  258. package/dist/Parameter/LinearPerceptronParameter.d.ts +41 -0
  259. package/dist/Parameter/LinearPerceptronParameter.js +66 -0
  260. package/dist/Parameter/LinearPerceptronParameter.js.map +1 -0
  261. package/dist/Parameter/MultiLayerPerceptronParameter.d.ts +30 -0
  262. package/dist/Parameter/MultiLayerPerceptronParameter.js +50 -0
  263. package/dist/Parameter/MultiLayerPerceptronParameter.js.map +1 -0
  264. package/dist/Parameter/Parameter.d.ts +15 -0
  265. package/dist/Parameter/Parameter.js +33 -0
  266. package/dist/Parameter/Parameter.js.map +1 -0
  267. package/dist/Parameter/RandomForestParameter.d.ts +18 -0
  268. package/dist/Parameter/RandomForestParameter.js +37 -0
  269. package/dist/Parameter/RandomForestParameter.js.map +1 -0
  270. package/dist/Performance/ClassificationPerformance.d.ts +17 -0
  271. package/dist/Performance/ClassificationPerformance.js +36 -0
  272. package/dist/Performance/ClassificationPerformance.js.map +1 -0
  273. package/dist/Performance/ConfusionMatrix.d.ts +81 -0
  274. package/dist/Performance/ConfusionMatrix.js +178 -0
  275. package/dist/Performance/ConfusionMatrix.js.map +1 -0
  276. package/dist/Performance/DetailedClassificationPerformance.d.ts +17 -0
  277. package/dist/Performance/DetailedClassificationPerformance.js +35 -0
  278. package/dist/Performance/DetailedClassificationPerformance.js.map +1 -0
  279. package/dist/Performance/ExperimentPerformance.d.ts +88 -0
  280. package/dist/Performance/ExperimentPerformance.js +176 -0
  281. package/dist/Performance/ExperimentPerformance.js.map +1 -0
  282. package/dist/Performance/Performance.d.ts +15 -0
  283. package/dist/Performance/Performance.js +33 -0
  284. package/dist/Performance/Performance.js.map +1 -0
  285. package/dist/StatisticalTest/Combined5x2F.d.ts +7 -0
  286. package/dist/StatisticalTest/Combined5x2F.js +43 -0
  287. package/dist/StatisticalTest/Combined5x2F.js.map +1 -0
  288. package/dist/StatisticalTest/Combined5x2t.d.ts +7 -0
  289. package/dist/StatisticalTest/Combined5x2t.js +43 -0
  290. package/dist/StatisticalTest/Combined5x2t.js.map +1 -0
  291. package/dist/StatisticalTest/Paired5x2t.d.ts +7 -0
  292. package/dist/StatisticalTest/Paired5x2t.js +42 -0
  293. package/dist/StatisticalTest/Paired5x2t.js.map +1 -0
  294. package/dist/StatisticalTest/PairedTest.d.ts +6 -0
  295. package/dist/StatisticalTest/PairedTest.js +35 -0
  296. package/dist/StatisticalTest/PairedTest.js.map +1 -0
  297. package/dist/StatisticalTest/Pairedt.d.ts +7 -0
  298. package/dist/StatisticalTest/Pairedt.js +40 -0
  299. package/dist/StatisticalTest/Pairedt.js.map +1 -0
  300. package/dist/StatisticalTest/Sign.d.ts +8 -0
  301. package/dist/StatisticalTest/Sign.js +52 -0
  302. package/dist/StatisticalTest/Sign.js.map +1 -0
  303. package/dist/StatisticalTest/StatisticalTestResult.d.ts +9 -0
  304. package/dist/StatisticalTest/StatisticalTestResult.js +53 -0
  305. package/dist/StatisticalTest/StatisticalTestResult.js.map +1 -0
  306. package/dist/StatisticalTest/StatisticalTestResultType.d.ts +7 -0
  307. package/dist/StatisticalTest/StatisticalTestResultType.js +22 -0
  308. package/dist/StatisticalTest/StatisticalTestResultType.js.map +1 -0
  309. package/index.js +100 -0
  310. package/package.json +30 -0
  311. package/source/Attribute/Attribute.ts +6 -0
  312. package/source/Attribute/AttributeType.ts +18 -0
  313. package/source/Attribute/BinaryAttribute.ts +14 -0
  314. package/source/Attribute/ContinuousAttribute.ts +53 -0
  315. package/source/Attribute/DiscreteAttribute.ts +46 -0
  316. package/source/Attribute/DiscreteIndexedAttribute.ts +54 -0
  317. package/source/Classifier/Bagging.ts +32 -0
  318. package/source/Classifier/C45.ts +30 -0
  319. package/source/Classifier/C45Stump.ts +19 -0
  320. package/source/Classifier/Classifier.ts +71 -0
  321. package/source/Classifier/DeepNetwork.ts +22 -0
  322. package/source/Classifier/Dummy.ts +19 -0
  323. package/source/Classifier/KMeans.ts +26 -0
  324. package/source/Classifier/Knn.ts +21 -0
  325. package/source/Classifier/Lda.ts +43 -0
  326. package/source/Classifier/LinearPerceptron.ts +23 -0
  327. package/source/Classifier/MultiLayerPerceptron.ts +23 -0
  328. package/source/Classifier/NaiveBayes.ts +62 -0
  329. package/source/Classifier/Qda.ts +42 -0
  330. package/source/Classifier/RandomClassifier.ts +22 -0
  331. package/source/Classifier/RandomForest.ts +29 -0
  332. package/source/DataSet/DataDefinition.ts +106 -0
  333. package/source/DataSet/DataSet.ts +279 -0
  334. package/source/DistanceMetric/DistanceMetric.ts +6 -0
  335. package/source/DistanceMetric/EuclidianDistance.ts +32 -0
  336. package/source/DistanceMetric/MahalanobisDistance.ts +33 -0
  337. package/source/Experiment/BootstrapRun.ts +37 -0
  338. package/source/Experiment/Experiment.ts +57 -0
  339. package/source/Experiment/KFoldRun.ts +44 -0
  340. package/source/Experiment/KFoldRunSeparateTest.ts +50 -0
  341. package/source/Experiment/MultipleRun.ts +7 -0
  342. package/source/Experiment/MxKFoldRun.ts +37 -0
  343. package/source/Experiment/MxKFoldRunSeparateTest.ts +40 -0
  344. package/source/Experiment/SingleRun.ts +7 -0
  345. package/source/Experiment/SingleRunWithK.ts +39 -0
  346. package/source/Experiment/StratifiedKFoldRun.ts +30 -0
  347. package/source/Experiment/StratifiedKFoldRunSeparateTest.ts +33 -0
  348. package/source/Experiment/StratifiedMxKFoldRun.ts +34 -0
  349. package/source/Experiment/StratifiedMxKFoldRunSeparateTest.ts +40 -0
  350. package/source/Experiment/StratifiedSingleRunWithK.ts +33 -0
  351. package/source/FeatureSelection/BackwardSelection.ts +28 -0
  352. package/source/FeatureSelection/FeatureSubSet.ts +82 -0
  353. package/source/FeatureSelection/FloatingSelection.ts +27 -0
  354. package/source/FeatureSelection/ForwardSelection.ts +27 -0
  355. package/source/FeatureSelection/SubSetSelection.ts +88 -0
  356. package/source/Filter/DiscreteToContinuous.ts +59 -0
  357. package/source/Filter/DiscreteToIndexed.ts +48 -0
  358. package/source/Filter/FeatureFilter.ts +31 -0
  359. package/source/Filter/LaryFilter.ts +54 -0
  360. package/source/Filter/LaryToBinary.ts +57 -0
  361. package/source/Filter/Normalize.ts +42 -0
  362. package/source/Filter/Pca.ts +104 -0
  363. package/source/Filter/TrainedFeatureFilter.ts +17 -0
  364. package/source/Instance/CompositeInstance.ts +53 -0
  365. package/source/Instance/Instance.ts +170 -0
  366. package/source/InstanceList/InstanceList.ts +519 -0
  367. package/source/InstanceList/InstanceListOfSameClass.ts +25 -0
  368. package/source/InstanceList/Partition.ts +131 -0
  369. package/source/Model/DecisionTree/DecisionCondition.ts +74 -0
  370. package/source/Model/DecisionTree/DecisionNode.ts +257 -0
  371. package/source/Model/DecisionTree/DecisionTree.ts +71 -0
  372. package/source/Model/DeepNetworkModel.ts +156 -0
  373. package/source/Model/DummyModel.ts +40 -0
  374. package/source/Model/GaussianModel.ts +58 -0
  375. package/source/Model/KMeansModel.ts +44 -0
  376. package/source/Model/KnnInstance.ts +27 -0
  377. package/source/Model/KnnModel.ts +78 -0
  378. package/source/Model/LdaModel.ts +39 -0
  379. package/source/Model/LinearPerceptronModel.ts +55 -0
  380. package/source/Model/Model.ts +22 -0
  381. package/source/Model/MultiLayerPerceptronModel.ts +95 -0
  382. package/source/Model/NaiveBayesModel.ts +89 -0
  383. package/source/Model/NeuralNetworkModel.ts +187 -0
  384. package/source/Model/QdaModel.ts +39 -0
  385. package/source/Model/RandomModel.ts +44 -0
  386. package/source/Model/TreeEnsembleModel.ts +43 -0
  387. package/source/Model/ValidatedModel.ts +23 -0
  388. package/source/Parameter/ActivationFunction.ts +3 -0
  389. package/source/Parameter/BaggingParameter.ts +26 -0
  390. package/source/Parameter/C45Parameter.ts +38 -0
  391. package/source/Parameter/DeepNetworkParameter.ts +59 -0
  392. package/source/Parameter/KMeansParameter.ts +33 -0
  393. package/source/Parameter/KnnParameter.ts +28 -0
  394. package/source/Parameter/LinearPerceptronParameter.ts +66 -0
  395. package/source/Parameter/MultiLayerPerceptronParameter.ts +49 -0
  396. package/source/Parameter/Parameter.ts +22 -0
  397. package/source/Parameter/RandomForestParameter.ts +27 -0
  398. package/source/Performance/ClassificationPerformance.ts +26 -0
  399. package/source/Performance/ConfusionMatrix.ts +175 -0
  400. package/source/Performance/DetailedClassificationPerformance.ts +26 -0
  401. package/source/Performance/ExperimentPerformance.ts +175 -0
  402. package/source/Performance/Performance.ts +22 -0
  403. package/source/StatisticalTest/Combined5x2F.ts +33 -0
  404. package/source/StatisticalTest/Combined5x2t.ts +33 -0
  405. package/source/StatisticalTest/Paired5x2t.ts +32 -0
  406. package/source/StatisticalTest/PairedTest.ts +24 -0
  407. package/source/StatisticalTest/Pairedt.ts +30 -0
  408. package/source/StatisticalTest/Sign.ts +41 -0
  409. package/source/StatisticalTest/StatisticalTestResult.ts +42 -0
  410. package/source/StatisticalTest/StatisticalTestResultType.ts +6 -0
  411. package/source/tsconfig.json +13 -0
  412. package/tsconfig.json +15 -0
@@ -0,0 +1,74 @@
1
+ import {Attribute} from "../../Attribute/Attribute";
2
+ import {Instance} from "../../Instance/Instance";
3
+ import {DiscreteIndexedAttribute} from "../../Attribute/DiscreteIndexedAttribute";
4
+ import {DiscreteAttribute} from "../../Attribute/DiscreteAttribute";
5
+ import {ContinuousAttribute} from "../../Attribute/ContinuousAttribute";
6
+
7
+ export class DecisionCondition {
8
+
9
+ private attributeIndex: number = -1
10
+ private comparison: string = undefined
11
+ private value: Attribute
12
+
13
+ /**
14
+ * A constructor that sets attributeIndex, comparison and {@link Attribute} value.
15
+ *
16
+ * @param attributeIndex Integer number that shows attribute index.
17
+ * @param value The value of the {@link Attribute}.
18
+ * @param comparison Comparison character.
19
+ */
20
+ constructor(attributeIndex: number, value: Attribute, comparison?: string) {
21
+ this.attributeIndex = attributeIndex;
22
+ this.comparison = comparison;
23
+ this.value = value;
24
+ }
25
+
26
+ getAttributeIndex(): number{
27
+ return this.attributeIndex
28
+ }
29
+
30
+ getValue(): Attribute{
31
+ return this.value
32
+ }
33
+
34
+ /**
35
+ * The satisfy method takes an {@link Instance} as an input.
36
+ * <p>
37
+ * If defined {@link Attribute} value is a {@link DiscreteIndexedAttribute} it compares the index of {@link Attribute} of instance at the
38
+ * attributeIndex and the index of {@link Attribute} value and returns the result.
39
+ * <p>
40
+ * If defined {@link Attribute} value is a {@link DiscreteAttribute} it compares the value of {@link Attribute} of instance at the
41
+ * attributeIndex and the value of {@link Attribute} value and returns the result.
42
+ * <p>
43
+ * If defined {@link Attribute} value is a {@link ContinuousAttribute} it compares the value of {@link Attribute} of instance at the
44
+ * attributeIndex and the value of {@link Attribute} value and returns the result according to the comparison character whether it is
45
+ * less than or greater than signs.
46
+ *
47
+ * @param instance Instance to compare.
48
+ * @return True if gicen instance satisfies the conditions.
49
+ */
50
+ satisfy(instance: Instance): boolean{
51
+ if (this.value instanceof DiscreteIndexedAttribute) {
52
+ if ((<DiscreteIndexedAttribute> this.value).getIndex() != -1) {
53
+ return (<DiscreteIndexedAttribute> instance.getAttribute(this.attributeIndex)).getIndex() == (<DiscreteIndexedAttribute> this.value).getIndex();
54
+ } else {
55
+ return true;
56
+ }
57
+ } else {
58
+ if (this.value instanceof DiscreteAttribute) {
59
+ return ((<DiscreteAttribute> instance.getAttribute(this.attributeIndex)).getValue() == (<DiscreteAttribute> this.value).getValue());
60
+ } else {
61
+ if (this.value instanceof ContinuousAttribute) {
62
+ if (this.comparison == "<") {
63
+ return (<ContinuousAttribute> instance.getAttribute(this.attributeIndex)).getValue() <= (<ContinuousAttribute> this.value).getValue();
64
+ } else {
65
+ if (this.comparison == ">") {
66
+ return (<ContinuousAttribute> instance.getAttribute(this.attributeIndex)).getValue() > (<ContinuousAttribute> this.value).getValue();
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+ return false;
73
+ }
74
+ }
@@ -0,0 +1,257 @@
1
+ import {InstanceList} from "../../InstanceList/InstanceList";
2
+ import {DecisionCondition} from "./DecisionCondition";
3
+ import {RandomForestParameter} from "../../Parameter/RandomForestParameter";
4
+ import {Model} from "../Model";
5
+ import {DiscreteIndexedAttribute} from "../../Attribute/DiscreteIndexedAttribute";
6
+ import {DiscreteAttribute} from "../../Attribute/DiscreteAttribute";
7
+ import {ContinuousAttribute} from "../../Attribute/ContinuousAttribute";
8
+ import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
9
+ import {Partition} from "../../InstanceList/Partition";
10
+ import {Instance} from "../../Instance/Instance";
11
+ import {CompositeInstance} from "../../Instance/CompositeInstance";
12
+
13
+ export class DecisionNode {
14
+
15
+ children: Array<DecisionNode> = undefined
16
+ private EPSILON: number = 0.0000000001;
17
+ private readonly data : InstanceList = undefined
18
+ private readonly classLabel : string = undefined
19
+ leaf: boolean = false
20
+ private condition: DecisionCondition = undefined
21
+
22
+ /**
23
+ * The DecisionNode method takes {@link InstanceList} data as input, and then it sets the class label parameter by finding
24
+ * the most occurred class label of given data, it then gets distinct class labels as class labels ArrayList. Later, it adds ordered
25
+ * indices to the indexList and shuffles them randomly. Then, it gets the class distribution of given data and finds the best entropy value
26
+ * of these class distribution.
27
+ * <p>
28
+ * If an attribute of given data is {@link DiscreteIndexedAttribute}, it creates a Distribution according to discrete indexed attribute class distribution
29
+ * 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
30
+ * the newly founded best entropy's index. At the end, it also adds new distribution to the class distribution .
31
+ * <p>
32
+ * If an attribute of given data is {@link DiscreteAttribute}, it directly finds the entropy. If it is better than the last best entropy it
33
+ * reassigns the best entropy, best attribute and best split value according to the newly founded best entropy's index.
34
+ * <p>
35
+ * If an attribute of given data is {@link ContinuousAttribute}, it creates two distributions; left and right according to class distribution
36
+ * and discrete distribution respectively, and finds the entropy. If it is better than the last best entropy it reassigns the best entropy,
37
+ * best attribute and best split value according to the newly founded best entropy's index. At the end, it also adds new distribution to
38
+ * the right distribution and removes from left distribution .
39
+ *
40
+ * @param data {@link InstanceList} input.
41
+ * @param condition {@link DecisionCondition} to check.
42
+ * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
43
+ * @param isStump Refers to decision trees with only 1 splitting rule.
44
+ */
45
+ constructor(data: InstanceList, condition: DecisionCondition, parameter: RandomForestParameter, isStump: boolean) {
46
+ let bestAttribute = -1
47
+ let bestSplitValue = 0
48
+ this.condition = condition;
49
+ this.data = data;
50
+ this.classLabel = Model.getMaximum(data.getClassLabels());
51
+ this.leaf = true;
52
+ let classLabels = data.getDistinctClassLabels();
53
+ if (classLabels.length == 1) {
54
+ return;
55
+ }
56
+ if (isStump && condition != null) {
57
+ return;
58
+ }
59
+ let indexList = new Array<number>();
60
+ for (let i = 0; i < data.get(0).attributeSize(); i++) {
61
+ indexList.push(i);
62
+ }
63
+ let size
64
+ if (parameter != undefined && parameter.getAttributeSubsetSize() < data.get(0).attributeSize()) {
65
+ size = parameter.getAttributeSubsetSize();
66
+ } else {
67
+ size = data.get(0).attributeSize();
68
+ }
69
+ let classDistribution = data.classDistribution();
70
+ let bestEntropy = data.classDistribution().entropy();
71
+ for (let j = 0; j < size; j++) {
72
+ let index = indexList[j];
73
+ if (data.get(0).getAttribute(index) instanceof DiscreteIndexedAttribute) {
74
+ for (let k = 0; k < (<DiscreteIndexedAttribute>data.get(0).getAttribute(index)).getMaxIndex(); k++) {
75
+ let distribution = data.discreteIndexedAttributeClassDistribution(index, k);
76
+ if (distribution.getSum() > 0) {
77
+ classDistribution.removeDistribution(distribution);
78
+ let entropy = (classDistribution.entropy() * classDistribution.getSum() + distribution.entropy() * distribution.getSum()) / data.size();
79
+ if (entropy + this.EPSILON < bestEntropy) {
80
+ bestEntropy = entropy;
81
+ bestAttribute = index;
82
+ bestSplitValue = k;
83
+ }
84
+ classDistribution.addDistribution(distribution);
85
+ }
86
+ }
87
+ } else {
88
+ if (data.get(0).getAttribute(index) instanceof DiscreteAttribute) {
89
+ let entropy = this.entropyForDiscreteAttribute(index);
90
+ if (entropy + this.EPSILON < bestEntropy) {
91
+ bestEntropy = entropy;
92
+ bestAttribute = index;
93
+ }
94
+ } else {
95
+ if (data.get(0).getAttribute(index) instanceof ContinuousAttribute) {
96
+ data.sort(index);
97
+ let previousValue = Number.NEGATIVE_INFINITY;
98
+ let leftDistribution = data.classDistribution();
99
+ let rightDistribution = new DiscreteDistribution();
100
+ for (let k = 0; k < data.size(); k++) {
101
+ let instance = data.get(k);
102
+ if (k == 0) {
103
+ previousValue = (<ContinuousAttribute>instance.getAttribute(index)).getValue();
104
+ } else {
105
+ if ((<ContinuousAttribute>instance.getAttribute(index)).getValue() != previousValue) {
106
+ let splitValue = (previousValue + (<ContinuousAttribute>instance.getAttribute(index)).getValue()) / 2;
107
+ previousValue = (<ContinuousAttribute>instance.getAttribute(index)).getValue();
108
+ let entropy = (leftDistribution.getSum() / data.size()) * leftDistribution.entropy() + (rightDistribution.getSum() / data.size()) * rightDistribution.entropy();
109
+ if (entropy + this.EPSILON < bestEntropy) {
110
+ bestEntropy = entropy;
111
+ bestSplitValue = splitValue;
112
+ bestAttribute = index;
113
+ }
114
+ }
115
+ }
116
+ leftDistribution.removeItem(instance.getClassLabel());
117
+ rightDistribution.addItem(instance.getClassLabel());
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ if (bestAttribute != -1) {
124
+ this.leaf = false;
125
+ if (data.get(0).getAttribute(bestAttribute) instanceof DiscreteIndexedAttribute) {
126
+ this.createChildrenForDiscreteIndexed(bestAttribute, bestSplitValue, parameter, isStump);
127
+ } else {
128
+ if (data.get(0).getAttribute(bestAttribute) instanceof DiscreteAttribute) {
129
+ this.createChildrenForDiscrete(bestAttribute, parameter, isStump);
130
+ } else {
131
+ if (data.get(0).getAttribute(bestAttribute) instanceof ContinuousAttribute) {
132
+ this.createChildrenForContinuous(bestAttribute, bestSplitValue, parameter, isStump);
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ /**
140
+ * The entropyForDiscreteAttribute method takes an attributeIndex and creates an ArrayList of DiscreteDistribution.
141
+ * Then loops through the distributions and calculates the total entropy.
142
+ *
143
+ * @param attributeIndex Index of the attribute.
144
+ * @return Total entropy for the discrete attribute.
145
+ */
146
+ private entropyForDiscreteAttribute(attributeIndex: number): number{
147
+ let sum = 0.0;
148
+ let distributions = this.data.attributeClassDistribution(attributeIndex);
149
+ for (let distribution of distributions) {
150
+ sum += (distribution.getSum() / this.data.size()) * distribution.entropy();
151
+ }
152
+ return sum;
153
+ }
154
+
155
+ /**
156
+ * The createChildrenForDiscreteIndexed method creates an ArrayList of DecisionNodes as children and a partition with respect to
157
+ * indexed attribute.
158
+ *
159
+ * @param attributeIndex Index of the attribute.
160
+ * @param attributeValue Value of the attribute.
161
+ * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
162
+ * @param isStump Refers to decision trees with only 1 splitting rule.
163
+ */
164
+ private createChildrenForDiscreteIndexed(attributeIndex: number, attributeValue: number, parameter: RandomForestParameter, isStump: boolean){
165
+ let childrenData = new Partition(this.data, attributeIndex, attributeValue);
166
+ this.children = new Array<DecisionNode>();
167
+ this.children.push(new DecisionNode(childrenData.get(0), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", attributeValue, (<DiscreteIndexedAttribute> this.data.get(0).getAttribute(attributeIndex)).getMaxIndex())), parameter, isStump));
168
+ this.children.push(new DecisionNode(childrenData.get(1), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", -1, (<DiscreteIndexedAttribute> this.data.get(0).getAttribute(attributeIndex)).getMaxIndex())), parameter, isStump));
169
+ }
170
+
171
+ /**
172
+ * The createChildrenForDiscrete method creates an ArrayList of values, a partition with respect to attributes and an ArrayList
173
+ * of DecisionNodes as children.
174
+ *
175
+ * @param attributeIndex Index of the attribute.
176
+ * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
177
+ * @param isStump Refers to decision trees with only 1 splitting rule.
178
+ */
179
+ private createChildrenForDiscrete(attributeIndex: number, parameter: RandomForestParameter, isStump: boolean){
180
+ let valueList = this.data.getAttributeValueList(attributeIndex);
181
+ let childrenData = new Partition(this.data, attributeIndex);
182
+ this.children = new Array<DecisionNode>();
183
+ for (let i = 0; i < valueList.length; i++) {
184
+ this.children.push(new DecisionNode(childrenData.get(i), new DecisionCondition(attributeIndex, new DiscreteAttribute(valueList[i])), parameter, isStump));
185
+ }
186
+ }
187
+
188
+ /**
189
+ * The createChildrenForContinuous method creates an ArrayList of DecisionNodes as children and a partition with respect to
190
+ * continuous attribute and the given split value.
191
+ *
192
+ * @param attributeIndex Index of the attribute.
193
+ * @param parameter RandomForestParameter like seed, ensembleSize, attributeSubsetSize.
194
+ * @param isStump Refers to decision trees with only 1 splitting rule.
195
+ * @param splitValue Split value is used for partitioning.
196
+ */
197
+ private createChildrenForContinuous(attributeIndex: number, splitValue: number, parameter: RandomForestParameter, isStump: boolean){
198
+ let childrenData = new Partition(this.data, attributeIndex, splitValue);
199
+ this.children = new Array<DecisionNode>();
200
+ this.children.push(new DecisionNode(childrenData.get(0), new DecisionCondition(attributeIndex, new ContinuousAttribute(splitValue), "<"), parameter, isStump));
201
+ this.children.push(new DecisionNode(childrenData.get(1), new DecisionCondition(attributeIndex, new ContinuousAttribute(splitValue), ">"), parameter, isStump));
202
+ }
203
+
204
+ /**
205
+ * The predict method takes an {@link Instance} as input and performs prediction on the DecisionNodes and returns the prediction
206
+ * for that instance.
207
+ *
208
+ * @param instance Instance to make prediction.
209
+ * @return The prediction for given instance.
210
+ */
211
+ predict(instance: Instance): string{
212
+ if (instance instanceof CompositeInstance) {
213
+ let possibleClassLabels = (<CompositeInstance> instance).getPossibleClassLabels();
214
+ let distribution = this.data.classDistribution();
215
+ let predictedClass = distribution.getMaxItem(possibleClassLabels);
216
+ if (this.leaf) {
217
+ return predictedClass;
218
+ } else {
219
+ for (let node of this.children) {
220
+ if (node.condition.satisfy(instance)) {
221
+ let childPrediction = node.predict(instance);
222
+ if (childPrediction != undefined) {
223
+ return childPrediction;
224
+ } else {
225
+ return predictedClass;
226
+ }
227
+ }
228
+ }
229
+ return predictedClass;
230
+ }
231
+ } else {
232
+ if (this.leaf) {
233
+ return this.classLabel;
234
+ } else {
235
+ for (let node of this.children) {
236
+ if (node.condition.satisfy(instance)) {
237
+ return node.predict(instance);
238
+ }
239
+ }
240
+ return this.classLabel;
241
+ }
242
+ }
243
+ }
244
+
245
+ predictProbabilityDistribution(instance: Instance): Map<string, number>{
246
+ if (this.leaf) {
247
+ return this.data.classDistribution().getProbabilityDistribution();
248
+ } else {
249
+ for (let node of this.children) {
250
+ if (node.condition.satisfy(instance)) {
251
+ return node.predictProbabilityDistribution(instance);
252
+ }
253
+ }
254
+ return this.data.classDistribution().getProbabilityDistribution();
255
+ }
256
+ }
257
+ }
@@ -0,0 +1,71 @@
1
+ import {ValidatedModel} from "../ValidatedModel";
2
+ import {DecisionNode} from "./DecisionNode";
3
+ import {Instance} from "../../Instance/Instance";
4
+ import {CompositeInstance} from "../../Instance/CompositeInstance";
5
+ import {InstanceList} from "../../InstanceList/InstanceList";
6
+
7
+ export class DecisionTree extends ValidatedModel{
8
+
9
+ private root: DecisionNode
10
+
11
+ /**
12
+ * Constructor that sets root node of the decision tree.
13
+ *
14
+ * @param root DecisionNode type input.
15
+ */
16
+ constructor(root: DecisionNode) {
17
+ super();
18
+ this.root = root
19
+ }
20
+
21
+ /**
22
+ * The predict method performs prediction on the root node of given instance, and if it is null, it returns the possible class labels.
23
+ * Otherwise it returns the returned class labels.
24
+ *
25
+ * @param instance Instance make prediction.
26
+ * @return Possible class labels.
27
+ */
28
+ predict(instance: Instance): string {
29
+ let predictedClass = this.root.predict(instance);
30
+ if ((predictedClass == null) && ((instance instanceof CompositeInstance))) {
31
+ predictedClass = (<CompositeInstance> instance).getPossibleClassLabels()[0];
32
+ }
33
+ return predictedClass;
34
+ }
35
+
36
+ predictProbability(instance: Instance): Map<string, number> {
37
+ return this.root.predictProbabilityDistribution(instance)
38
+ }
39
+
40
+ /**
41
+ * The prune method takes a {@link DecisionNode} and an {@link InstanceList} as inputs. It checks the classification performance
42
+ * of given InstanceList before pruning, i.e making a node leaf, and after pruning. If the after performance is better than the
43
+ * before performance it prune the given InstanceList from the tree.
44
+ *
45
+ * @param node DecisionNode that will be pruned if conditions hold.
46
+ * @param pruneSet Small subset of tree that will be removed from tree.
47
+ */
48
+ pruneNode(node: DecisionNode, pruneSet: InstanceList){
49
+ if (node.leaf){
50
+ return;
51
+ }
52
+ let before = this.testClassifier(pruneSet);
53
+ node.leaf = true;
54
+ let after = this.testClassifier(pruneSet);
55
+ if (after.getAccuracy() < before.getAccuracy()) {
56
+ node.leaf = false;
57
+ for (let child of node.children) {
58
+ this.pruneNode(child, pruneSet);
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * The prune method takes an {@link InstanceList} and performs pruning to the root node.
65
+ *
66
+ * @param pruneSet {@link InstanceList} to perform pruning.
67
+ */
68
+ prune(pruneSet: InstanceList){
69
+ this.pruneNode(this.root, pruneSet)
70
+ }
71
+ }
@@ -0,0 +1,156 @@
1
+ import {NeuralNetworkModel} from "./NeuralNetworkModel";
2
+ import {Matrix} from "nlptoolkit-math/dist/Matrix";
3
+ import {ActivationFunction} from "../Parameter/ActivationFunction";
4
+ import {DeepNetworkParameter} from "../Parameter/DeepNetworkParameter";
5
+ import {InstanceList} from "../InstanceList/InstanceList";
6
+ import {Vector} from "nlptoolkit-math/dist/Vector";
7
+ import {ClassificationPerformance} from "../Performance/ClassificationPerformance";
8
+
9
+ export class DeepNetworkModel extends NeuralNetworkModel{
10
+
11
+ private weights: Array<Matrix>
12
+ private hiddenLayerSize: number
13
+ private readonly activationFunction: ActivationFunction
14
+
15
+ /**
16
+ * The allocateWeights method takes {@link DeepNetworkParameter}s as an input. First it adds random weights to the {@link Array}
17
+ * of {@link Matrix} weights' first layer. Then it loops through the layers and adds random weights till the last layer.
18
+ * At the end it adds random weights to the last layer and also sets the hiddenLayerSize value.
19
+ *
20
+ * @param parameters {@link DeepNetworkParameter} input.
21
+ */
22
+ private allocateWeights(parameters: DeepNetworkParameter){
23
+ this.weights = new Array<Matrix>();
24
+ this.weights.push(this.allocateLayerWeights(parameters.getHiddenNodes(0), this.d + 1));
25
+ for (let i = 0; i < parameters.layerSize() - 1; i++) {
26
+ this.weights.push(this.allocateLayerWeights(parameters.getHiddenNodes(i + 1), parameters.getHiddenNodes(i) + 1));
27
+ }
28
+ this.weights.push(this.allocateLayerWeights(this.K, parameters.getHiddenNodes(parameters.layerSize() - 1) + 1));
29
+ this.hiddenLayerSize = parameters.layerSize();
30
+ }
31
+
32
+ /**
33
+ * The setBestWeights method creates an {@link Array} of Matrix as bestWeights and clones the values of weights {@link Array}
34
+ * into this newly created {@link Array}.
35
+ *
36
+ * @return An {@link Array} clones from the weights ArrayList.
37
+ */
38
+ private setBestWeights(): Array<Matrix>{
39
+ let bestWeights = new Array<Matrix>();
40
+ for (let m of this.weights) {
41
+ bestWeights.push(m.clone());
42
+ }
43
+ return bestWeights;
44
+ }
45
+
46
+ /**
47
+ * Constructor that takes two {@link InstanceList} train set and validation set and {@link DeepNetworkParameter} as inputs.
48
+ * First it sets the class labels, their sizes as K and the size of the continuous attributes as d of given train set and
49
+ * allocates weights and sets the best weights. At each epoch, it shuffles the train set and loops through the each item of that train set,
50
+ * it multiplies the weights Matrix with input Vector than applies the sigmoid function and stores the result as hidden and add bias.
51
+ * Then updates weights and at the end it compares the performance of these weights with validation set. It updates the bestClassificationPerformance and
52
+ * bestWeights according to the current situation. At the end it updates the learning rate via etaDecrease value and finishes
53
+ * with clearing the weights.
54
+ *
55
+ * @param trainSet {@link InstanceList} to be used as trainSet.
56
+ * @param validationSet {@link InstanceList} to be used as validationSet.
57
+ * @param parameters {@link DeepNetworkParameter} input.
58
+ */
59
+ constructor(trainSet: InstanceList, validationSet: InstanceList, parameters: DeepNetworkParameter) {
60
+ super(trainSet);
61
+ let tmpHidden = new Vector(0, 0);
62
+ let deltaWeights = new Array<Matrix>();
63
+ let hidden = new Array<Vector>();
64
+ let hiddenBiased = new Array<Vector>();
65
+ this.activationFunction = parameters.getActivationFunction();
66
+ this.allocateWeights(parameters);
67
+ let bestWeights = this.setBestWeights();
68
+ let bestClassificationPerformance = new ClassificationPerformance(0.0);
69
+ let epoch = parameters.getEpoch();
70
+ let learningRate = parameters.getLearningRate();
71
+ for (let i = 0; i < epoch; i++) {
72
+ trainSet.shuffle(parameters.getSeed());
73
+ for (let j = 0; j < trainSet.size(); j++) {
74
+ this.createInputVector(trainSet.get(j));
75
+ hidden = new Array<Vector>();
76
+ hiddenBiased = new Array<Vector>();
77
+ deltaWeights = new Array<Matrix>();
78
+ for (let k = 0; k < this.hiddenLayerSize; k++) {
79
+ if (k == 0) {
80
+ hidden.push(this.calculateHidden(this.x, this.weights[k], this.activationFunction));
81
+ } else {
82
+ hidden.push(this.calculateHidden(hiddenBiased[k - 1], this.weights[k], this.activationFunction));
83
+ }
84
+ hiddenBiased.push(hidden[k].biased());
85
+ }
86
+ let rMinusY = this.calculateRMinusY(trainSet.get(j), hiddenBiased[this.hiddenLayerSize - 1], this.weights[this.weights.length - 1]);
87
+ deltaWeights.splice(0, 0, new Matrix(rMinusY, hiddenBiased[this.hiddenLayerSize - 1]));
88
+ for (let k = this.weights.length - 2; k >= 0; k--) {
89
+ let tmph
90
+ if (k == this.weights.length - 2){
91
+ tmph = this.weights[k + 1].multiplyWithVectorFromLeft(rMinusY);
92
+ } else {
93
+ tmph = this.weights[k + 1].multiplyWithVectorFromLeft(tmpHidden);
94
+ }
95
+ tmph.remove(0);
96
+ let activationDerivative
97
+ switch (this.activationFunction){
98
+ case ActivationFunction.SIGMOID:
99
+ default:
100
+ let oneMinusHidden = this.calculateOneMinusHidden(hidden[k]);
101
+ activationDerivative = oneMinusHidden.elementProduct(hidden[k]);
102
+ break;
103
+ case ActivationFunction.TANH:
104
+ let one = new Vector(hidden.length, 1.0);
105
+ hidden[k].tanh();
106
+ activationDerivative = one.difference(hidden[k].elementProduct(hidden[k]));
107
+ break;
108
+ case ActivationFunction.RELU:
109
+ hidden[k].reluDerivative();
110
+ activationDerivative = hidden[k];
111
+ break;
112
+ }
113
+ tmpHidden = tmph.elementProduct(activationDerivative);
114
+ if (k == 0) {
115
+ deltaWeights.splice(0, 0, new Matrix(tmpHidden, this.x));
116
+ } else {
117
+ deltaWeights.splice(0, 0, new Matrix(tmpHidden, hiddenBiased[k - 1]));
118
+ }
119
+ }
120
+ for (let k = 0; k < this.weights.length; k++) {
121
+ deltaWeights[k].multiplyWithConstant(learningRate);
122
+ this.weights[k].add(deltaWeights[k]);
123
+ }
124
+ }
125
+ let currentClassificationPerformance = this.testClassifier(validationSet);
126
+ if (currentClassificationPerformance.getAccuracy() > bestClassificationPerformance.getAccuracy()) {
127
+ bestClassificationPerformance = currentClassificationPerformance;
128
+ bestWeights = this.setBestWeights();
129
+ }
130
+ learningRate *= parameters.getEtaDecrease();
131
+ }
132
+ this.weights = new Array<Matrix>();
133
+ for (let m of bestWeights) {
134
+ this.weights.push(m);
135
+ }
136
+ }
137
+
138
+ /**
139
+ * The calculateOutput method loops size of the weights times and calculate one hidden layer at a time and adds bias term.
140
+ * At the end it updates the output y value.
141
+ */
142
+ protected calculateOutput(): void {
143
+ let hiddenBiased
144
+ for (let i = 0; i < this.weights.length - 1; i++) {
145
+ let hidden
146
+ if (i == 0) {
147
+ hidden = this.calculateHidden(this.x, this.weights[i], this.activationFunction);
148
+ } else {
149
+ hidden = this.calculateHidden(hiddenBiased, this.weights[i], this.activationFunction);
150
+ }
151
+ hiddenBiased = hidden.biased();
152
+ }
153
+ this.y = this.weights[this.weights.length - 1].multiplyWithVectorFromRight(hiddenBiased);
154
+ }
155
+
156
+ }
@@ -0,0 +1,40 @@
1
+ import {Model} from "./Model";
2
+ import {Instance} from "../Instance/Instance";
3
+ import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
4
+ import {InstanceList} from "../InstanceList/InstanceList";
5
+ import {CompositeInstance} from "../Instance/CompositeInstance";
6
+
7
+ export class DummyModel extends Model{
8
+
9
+ private distribution: DiscreteDistribution
10
+
11
+ /**
12
+ * Constructor which sets the distribution using the given {@link InstanceList}.
13
+ *
14
+ * @param trainSet {@link InstanceList} which is used to get the class distribution.
15
+ */
16
+ constructor(trainSet: InstanceList) {
17
+ super();
18
+ this.distribution = trainSet.classDistribution();
19
+ }
20
+
21
+ /**
22
+ * The predict method takes an Instance as an input and returns the entry of distribution which has the maximum value.
23
+ *
24
+ * @param instance Instance to make prediction.
25
+ * @return The entry of distribution which has the maximum value.
26
+ */
27
+ predict(instance: Instance): string {
28
+ if ((instance instanceof CompositeInstance)) {
29
+ let possibleClassLabels = (<CompositeInstance> instance).getPossibleClassLabels();
30
+ return this.distribution.getMaxItem(possibleClassLabels);
31
+ } else {
32
+ return this.distribution.getMaxItem();
33
+ }
34
+ }
35
+
36
+ predictProbability(instance: Instance): Map<string, number> {
37
+ return this.distribution.getProbabilityDistribution();
38
+ }
39
+
40
+ }
@@ -0,0 +1,58 @@
1
+ import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
2
+ import {Instance} from "../Instance/Instance";
3
+ import {ValidatedModel} from "./ValidatedModel";
4
+ import {CompositeInstance} from "../Instance/CompositeInstance";
5
+
6
+ export abstract class GaussianModel extends ValidatedModel{
7
+
8
+ protected priorDistribution: DiscreteDistribution
9
+
10
+ /**
11
+ * Abstract method calculateMetric takes an {@link Instance} and a String as inputs.
12
+ *
13
+ * @param instance {@link Instance} input.
14
+ * @param Ci String input.
15
+ * @return A double value as metric.
16
+ */
17
+ abstract calculateMetric(instance: Instance, Ci: string): number
18
+
19
+ /**
20
+ * The predict method takes an Instance as an input. First it gets the size of prior distribution and loops this size times.
21
+ * Then it gets the possible class labels and and calculates metric value. At the end, it returns the class which has the
22
+ * maximum value of metric.
23
+ *
24
+ * @param instance {@link Instance} to predict.
25
+ * @return The class which has the maximum value of metric.
26
+ */
27
+ predict(instance: Instance): string {
28
+ let maxMetric = Number.NEGATIVE_INFINITY;
29
+ let predictedClass, size
30
+ if (instance instanceof CompositeInstance) {
31
+ predictedClass = (<CompositeInstance> instance).getPossibleClassLabels()[0];
32
+ size = (<CompositeInstance> instance).getPossibleClassLabels().length;
33
+ } else {
34
+ predictedClass = this.priorDistribution.getMaxItem();
35
+ size = this.priorDistribution.size;
36
+ }
37
+ for (let i = 0; i < size; i++) {
38
+ let Ci
39
+ if (instance instanceof CompositeInstance) {
40
+ Ci = (<CompositeInstance> instance).getPossibleClassLabels()[i];
41
+ } else {
42
+ Ci = this.priorDistribution.getItem(i);
43
+ }
44
+ if (this.priorDistribution.containsItem(Ci)) {
45
+ let metric = this.calculateMetric(instance, Ci);
46
+ if (metric > maxMetric) {
47
+ maxMetric = metric;
48
+ predictedClass = Ci;
49
+ }
50
+ }
51
+ }
52
+ return predictedClass;
53
+ }
54
+
55
+ predictProbability(instance: Instance): Map<string, number> {
56
+ return undefined;
57
+ }
58
+ }