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.
- package/README.md +1 -0
- package/dist/Attribute/DiscreteAttribute.d.ts +1 -1
- package/dist/Attribute/DiscreteAttribute.js.map +1 -1
- package/dist/Attribute/DiscreteIndexedAttribute.d.ts +2 -2
- package/dist/DataSet/DataDefinition.d.ts +11 -0
- package/dist/DataSet/DataDefinition.js +11 -0
- package/dist/DataSet/DataDefinition.js.map +1 -1
- package/dist/Experiment/BootstrapRun.js +2 -2
- package/dist/Experiment/BootstrapRun.js.map +1 -1
- package/dist/Experiment/Experiment.d.ts +7 -7
- package/dist/Experiment/Experiment.js +6 -6
- package/dist/Experiment/Experiment.js.map +1 -1
- package/dist/Experiment/KFoldRun.d.ts +17 -2
- package/dist/Experiment/KFoldRun.js +19 -4
- package/dist/Experiment/KFoldRun.js.map +1 -1
- package/dist/Experiment/KFoldRunSeparateTest.d.ts +13 -2
- package/dist/Experiment/KFoldRunSeparateTest.js +15 -4
- package/dist/Experiment/KFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/MxKFoldRun.js +1 -1
- package/dist/Experiment/MxKFoldRun.js.map +1 -1
- package/dist/Experiment/MxKFoldRunSeparateTest.js +1 -1
- package/dist/Experiment/MxKFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/SingleRunWithK.d.ts +18 -3
- package/dist/Experiment/SingleRunWithK.js +18 -3
- package/dist/Experiment/SingleRunWithK.js.map +1 -1
- package/dist/Experiment/StratifiedKFoldRun.js +1 -1
- package/dist/Experiment/StratifiedKFoldRun.js.map +1 -1
- package/dist/Experiment/StratifiedKFoldRunSeparateTest.js +1 -1
- package/dist/Experiment/StratifiedKFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/StratifiedMxKFoldRun.js +1 -1
- package/dist/Experiment/StratifiedMxKFoldRun.js.map +1 -1
- package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js +3 -3
- package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/StratifiedSingleRunWithK.js +1 -1
- package/dist/Experiment/StratifiedSingleRunWithK.js.map +1 -1
- package/dist/Filter/LaryFilter.d.ts +1 -1
- package/dist/Filter/LaryFilter.js +1 -1
- package/dist/Filter/TrainedFeatureFilter.d.ts +1 -1
- package/dist/Filter/TrainedFeatureFilter.js +1 -1
- package/dist/InstanceList/Partition.d.ts +45 -0
- package/dist/InstanceList/Partition.js +125 -70
- package/dist/InstanceList/Partition.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionCondition.d.ts +8 -0
- package/dist/Model/DecisionTree/DecisionCondition.js +8 -0
- package/dist/Model/DecisionTree/DecisionCondition.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionNode.d.ts +39 -1
- package/dist/Model/DecisionTree/DecisionNode.js +64 -22
- package/dist/Model/DecisionTree/DecisionNode.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionStump.d.ts +17 -0
- package/dist/{Classifier/C45Stump.js → Model/DecisionTree/DecisionStump.js} +13 -10
- package/dist/Model/DecisionTree/DecisionStump.js.map +1 -0
- package/dist/Model/DecisionTree/DecisionTree.d.ts +24 -9
- package/dist/Model/DecisionTree/DecisionTree.js +39 -16
- package/dist/Model/DecisionTree/DecisionTree.js.map +1 -1
- package/dist/Model/DummyModel.d.ts +28 -3
- package/dist/Model/DummyModel.js +36 -23
- package/dist/Model/DummyModel.js.map +1 -1
- package/dist/{Classifier/Bagging.d.ts → Model/Ensemble/BaggingModel.d.ts} +8 -4
- package/dist/{Classifier/Bagging.js → Model/Ensemble/BaggingModel.js} +16 -14
- package/dist/Model/Ensemble/BaggingModel.js.map +1 -0
- package/dist/{Classifier/RandomForest.d.ts → Model/Ensemble/RandomForestModel.d.ts} +8 -4
- package/dist/{Classifier/RandomForest.js → Model/Ensemble/RandomForestModel.js} +16 -14
- package/dist/Model/Ensemble/RandomForestModel.js.map +1 -0
- package/dist/Model/Ensemble/TreeEnsembleModel.d.ts +32 -0
- package/dist/Model/{TreeEnsembleModel.js → Ensemble/TreeEnsembleModel.js} +23 -17
- package/dist/Model/Ensemble/TreeEnsembleModel.js.map +1 -0
- package/dist/Model/Model.d.ts +52 -0
- package/dist/Model/Model.js +84 -1
- package/dist/Model/Model.js.map +1 -1
- package/dist/Model/NeuralNetwork/DeepNetworkModel.d.ts +47 -0
- package/dist/Model/{DeepNetworkModel.js → NeuralNetwork/DeepNetworkModel.js} +55 -55
- package/dist/Model/NeuralNetwork/DeepNetworkModel.js.map +1 -0
- package/dist/Model/NeuralNetwork/LinearPerceptronModel.d.ts +31 -0
- package/dist/Model/NeuralNetwork/LinearPerceptronModel.js +84 -0
- package/dist/Model/NeuralNetwork/LinearPerceptronModel.js.map +1 -0
- package/dist/Model/NeuralNetwork/MultiLayerPerceptronModel.d.ts +39 -0
- package/dist/Model/{MultiLayerPerceptronModel.js → NeuralNetwork/MultiLayerPerceptronModel.js} +41 -37
- package/dist/Model/NeuralNetwork/MultiLayerPerceptronModel.js.map +1 -0
- package/dist/Model/{NeuralNetworkModel.d.ts → NeuralNetwork/NeuralNetworkModel.d.ts} +19 -5
- package/dist/Model/{NeuralNetworkModel.js → NeuralNetwork/NeuralNetworkModel.js} +19 -6
- package/dist/Model/NeuralNetwork/NeuralNetworkModel.js.map +1 -0
- package/dist/Model/{KnnInstance.d.ts → NonParametric/KnnInstance.d.ts} +1 -1
- package/dist/Model/NonParametric/KnnInstance.js.map +1 -0
- package/dist/Model/{KnnModel.d.ts → NonParametric/KnnModel.d.ts} +30 -6
- package/dist/Model/{KnnModel.js → NonParametric/KnnModel.js} +42 -19
- package/dist/Model/NonParametric/KnnModel.js.map +1 -0
- package/dist/Model/{GaussianModel.d.ts → Parametric/GaussianModel.d.ts} +20 -2
- package/dist/Model/{GaussianModel.js → Parametric/GaussianModel.js} +21 -3
- package/dist/Model/Parametric/GaussianModel.js.map +1 -0
- package/dist/Model/Parametric/KMeansModel.d.ts +36 -0
- package/dist/Model/Parametric/KMeansModel.js +73 -0
- package/dist/Model/Parametric/KMeansModel.js.map +1 -0
- package/dist/Model/Parametric/LdaModel.d.ts +44 -0
- package/dist/Model/Parametric/LdaModel.js +98 -0
- package/dist/Model/Parametric/LdaModel.js.map +1 -0
- package/dist/Model/{NaiveBayesModel.d.ts → Parametric/NaiveBayesModel.d.ts} +30 -10
- package/dist/Model/{NaiveBayesModel.js → Parametric/NaiveBayesModel.js} +64 -27
- package/dist/Model/Parametric/NaiveBayesModel.js.map +1 -0
- package/dist/Model/Parametric/QdaModel.d.ts +30 -0
- package/dist/Model/Parametric/QdaModel.js +84 -0
- package/dist/Model/Parametric/QdaModel.js.map +1 -0
- package/dist/Model/RandomModel.d.ts +32 -2
- package/dist/Model/RandomModel.js +48 -16
- package/dist/Model/RandomModel.js.map +1 -1
- package/dist/Parameter/C45Parameter.d.ts +2 -2
- package/dist/Parameter/DeepNetworkParameter.d.ts +2 -2
- package/dist/Parameter/KnnParameter.d.ts +1 -1
- package/dist/Parameter/LinearPerceptronParameter.d.ts +4 -4
- package/dist/Parameter/MultiLayerPerceptronParameter.d.ts +2 -2
- package/dist/Parameter/Parameter.d.ts +1 -1
- package/dist/Performance/ConfusionMatrix.d.ts +1 -1
- package/dist/StatisticalTest/Combined5x2F.d.ts +12 -0
- package/dist/StatisticalTest/Combined5x2F.js +12 -0
- package/dist/StatisticalTest/Combined5x2F.js.map +1 -1
- package/dist/StatisticalTest/Combined5x2t.d.ts +13 -0
- package/dist/StatisticalTest/Combined5x2t.js +13 -0
- package/dist/StatisticalTest/Combined5x2t.js.map +1 -1
- package/dist/StatisticalTest/Paired5x2t.d.ts +12 -0
- package/dist/StatisticalTest/Paired5x2t.js +12 -0
- package/dist/StatisticalTest/Paired5x2t.js.map +1 -1
- package/dist/StatisticalTest/PairedTest.d.ts +12 -0
- package/dist/StatisticalTest/PairedTest.js +12 -0
- package/dist/StatisticalTest/PairedTest.js.map +1 -1
- package/dist/StatisticalTest/Pairedt.d.ts +12 -0
- package/dist/StatisticalTest/Pairedt.js +12 -0
- package/dist/StatisticalTest/Pairedt.js.map +1 -1
- package/dist/StatisticalTest/Sign.d.ts +17 -0
- package/dist/StatisticalTest/Sign.js +17 -0
- package/dist/StatisticalTest/Sign.js.map +1 -1
- package/dist/StatisticalTest/StatisticalTestResult.d.ts +30 -0
- package/dist/StatisticalTest/StatisticalTestResult.js +30 -0
- package/dist/StatisticalTest/StatisticalTestResult.js.map +1 -1
- package/dist/index.d.ts +88 -0
- package/dist/index.js +111 -0
- package/dist/index.js.map +1 -0
- package/models/bagging-bupa.txt +10346 -0
- package/models/bagging-car.txt +40752 -0
- package/models/bagging-dermatology.txt +2990 -0
- package/models/bagging-iris.txt +1332 -0
- package/models/bagging-tictactoe.txt +29442 -0
- package/models/c45-bupa.txt +3 -0
- package/models/c45-car.txt +212 -0
- package/models/c45-carIndexed.txt +92 -0
- package/models/c45-dermatology.txt +22 -0
- package/models/c45-iris.txt +7 -0
- package/models/c45-tictactoe.txt +147 -0
- package/models/c45-tictactoeIndexed.txt +79 -0
- package/models/c45stump-bupa.txt +6 -0
- package/models/c45stump-car.txt +11 -0
- package/models/c45stump-chess.txt +35 -0
- package/models/c45stump-dermatology.txt +11 -0
- package/models/c45stump-iris.txt +5 -0
- package/models/c45stump-nursery.txt +10 -0
- package/models/c45stump-tictactoe.txt +9 -0
- package/models/randomforest-bupa.txt +10346 -0
- package/models/randomforest-car.txt +40752 -0
- package/models/randomforest-carIndexed.txt +15594 -0
- package/models/randomforest-dermatology.txt +2990 -0
- package/models/randomforest-iris.txt +1332 -0
- package/models/randomforest-tictactoe.txt +29442 -0
- package/package.json +7 -7
- package/source/Attribute/DiscreteAttribute.ts +1 -1
- package/source/Attribute/DiscreteIndexedAttribute.ts +2 -2
- package/source/DataSet/DataDefinition.ts +11 -0
- package/source/Experiment/BootstrapRun.ts +2 -2
- package/source/Experiment/Experiment.ts +10 -10
- package/source/Experiment/KFoldRun.ts +20 -5
- package/source/Experiment/KFoldRunSeparateTest.ts +16 -5
- package/source/Experiment/MxKFoldRun.ts +1 -1
- package/source/Experiment/MxKFoldRunSeparateTest.ts +1 -1
- package/source/Experiment/SingleRunWithK.ts +20 -5
- package/source/Experiment/StratifiedKFoldRun.ts +1 -1
- package/source/Experiment/StratifiedKFoldRunSeparateTest.ts +1 -1
- package/source/Experiment/StratifiedMxKFoldRun.ts +1 -1
- package/source/Experiment/StratifiedMxKFoldRunSeparateTest.ts +3 -3
- package/source/Experiment/StratifiedSingleRunWithK.ts +1 -1
- package/source/Filter/LaryFilter.ts +1 -1
- package/source/Filter/TrainedFeatureFilter.ts +1 -1
- package/source/InstanceList/Partition.ts +125 -65
- package/source/Model/DecisionTree/DecisionCondition.ts +8 -0
- package/source/Model/DecisionTree/DecisionNode.ts +65 -22
- package/source/Model/DecisionTree/DecisionStump.ts +26 -0
- package/source/Model/DecisionTree/DecisionTree.ts +44 -15
- package/source/Model/DummyModel.ts +40 -19
- package/source/{Classifier/Bagging.ts → Model/Ensemble/BaggingModel.ts} +15 -12
- package/source/{Classifier/RandomForest.ts → Model/Ensemble/RandomForestModel.ts} +14 -12
- package/source/Model/{TreeEnsembleModel.ts → Ensemble/TreeEnsembleModel.ts} +26 -18
- package/source/Model/Model.ts +92 -0
- package/source/Model/{DeepNetworkModel.ts → NeuralNetwork/DeepNetworkModel.ts} +59 -56
- package/source/Model/NeuralNetwork/LinearPerceptronModel.ts +81 -0
- package/source/Model/{MultiLayerPerceptronModel.ts → NeuralNetwork/MultiLayerPerceptronModel.ts} +46 -39
- package/source/Model/{NeuralNetworkModel.ts → NeuralNetwork/NeuralNetworkModel.ts} +20 -7
- package/source/Model/{KnnInstance.ts → NonParametric/KnnInstance.ts} +1 -1
- package/source/Model/{KnnModel.ts → NonParametric/KnnModel.ts} +48 -19
- package/source/Model/{GaussianModel.ts → Parametric/GaussianModel.ts} +21 -3
- package/source/Model/Parametric/KMeansModel.ts +72 -0
- package/source/Model/Parametric/LdaModel.ts +98 -0
- package/source/Model/{NaiveBayesModel.ts → Parametric/NaiveBayesModel.ts} +68 -25
- package/source/Model/Parametric/QdaModel.ts +83 -0
- package/source/Model/RandomModel.ts +54 -16
- package/source/Parameter/C45Parameter.ts +2 -2
- package/source/Parameter/DeepNetworkParameter.ts +2 -2
- package/source/Parameter/KnnParameter.ts +1 -1
- package/source/Parameter/LinearPerceptronParameter.ts +4 -4
- package/source/Parameter/MultiLayerPerceptronParameter.ts +2 -2
- package/source/Parameter/Parameter.ts +1 -1
- package/source/Performance/ConfusionMatrix.ts +1 -1
- package/source/StatisticalTest/Combined5x2F.ts +12 -0
- package/source/StatisticalTest/Combined5x2t.ts +13 -0
- package/source/StatisticalTest/Paired5x2t.ts +12 -0
- package/source/StatisticalTest/PairedTest.ts +12 -0
- package/source/StatisticalTest/Pairedt.ts +12 -0
- package/source/StatisticalTest/Sign.ts +17 -0
- package/source/StatisticalTest/StatisticalTestResult.ts +30 -0
- package/source/index.ts +88 -0
- package/source/tsconfig.json +1 -1
- package/tests/Classifier/C45Test.ts +2 -2
- package/tests/Classifier/DeepNetworkTest.ts +3 -5
- package/tests/Classifier/DummyTest.ts +2 -2
- package/tests/Classifier/KMeansTest.ts +2 -2
- package/tests/Classifier/KnnTest.ts +2 -2
- package/tests/Classifier/LdaTest.ts +2 -2
- package/tests/Classifier/LinearPerceptronTest.ts +2 -2
- package/tests/Classifier/MultiLayerPerceptronTest.ts +2 -3
- package/tests/Classifier/NaiveBayesTest.ts +2 -2
- package/tests/Classifier/QdaTest.ts +2 -2
- package/tests/Classifier/RandomForestTest.ts +2 -2
- package/tsconfig.json +1 -2
- package/dist/Classifier/Bagging.js.map +0 -1
- package/dist/Classifier/C45.d.ts +0 -14
- package/dist/Classifier/C45.js +0 -43
- package/dist/Classifier/C45.js.map +0 -1
- package/dist/Classifier/C45Stump.d.ts +0 -13
- package/dist/Classifier/C45Stump.js.map +0 -1
- package/dist/Classifier/Classifier.d.ts +0 -40
- package/dist/Classifier/Classifier.js +0 -72
- package/dist/Classifier/Classifier.js.map +0 -1
- package/dist/Classifier/DeepNetwork.d.ts +0 -14
- package/dist/Classifier/DeepNetwork.js +0 -34
- package/dist/Classifier/DeepNetwork.js.map +0 -1
- package/dist/Classifier/Dummy.d.ts +0 -14
- package/dist/Classifier/Dummy.js +0 -32
- package/dist/Classifier/Dummy.js.map +0 -1
- package/dist/Classifier/KMeans.d.ts +0 -13
- package/dist/Classifier/KMeans.js +0 -39
- package/dist/Classifier/KMeans.js.map +0 -1
- package/dist/Classifier/Knn.d.ts +0 -14
- package/dist/Classifier/Knn.js +0 -32
- package/dist/Classifier/Knn.js.map +0 -1
- package/dist/Classifier/Lda.d.ts +0 -13
- package/dist/Classifier/Lda.js +0 -55
- package/dist/Classifier/Lda.js.map +0 -1
- package/dist/Classifier/LinearPerceptron.d.ts +0 -15
- package/dist/Classifier/LinearPerceptron.js +0 -35
- package/dist/Classifier/LinearPerceptron.js.map +0 -1
- package/dist/Classifier/MultiLayerPerceptron.d.ts +0 -15
- package/dist/Classifier/MultiLayerPerceptron.js +0 -35
- package/dist/Classifier/MultiLayerPerceptron.js.map +0 -1
- package/dist/Classifier/NaiveBayes.d.ts +0 -26
- package/dist/Classifier/NaiveBayes.js +0 -70
- package/dist/Classifier/NaiveBayes.js.map +0 -1
- package/dist/Classifier/Qda.d.ts +0 -13
- package/dist/Classifier/Qda.js +0 -53
- package/dist/Classifier/Qda.js.map +0 -1
- package/dist/Classifier/RandomClassifier.d.ts +0 -13
- package/dist/Classifier/RandomClassifier.js +0 -35
- package/dist/Classifier/RandomClassifier.js.map +0 -1
- package/dist/Classifier/RandomForest.js.map +0 -1
- package/dist/Model/DeepNetworkModel.d.ts +0 -45
- package/dist/Model/DeepNetworkModel.js.map +0 -1
- package/dist/Model/GaussianModel.js.map +0 -1
- package/dist/Model/KMeansModel.d.ts +0 -28
- package/dist/Model/KMeansModel.js +0 -61
- package/dist/Model/KMeansModel.js.map +0 -1
- package/dist/Model/KnnInstance.js.map +0 -1
- package/dist/Model/KnnModel.js.map +0 -1
- package/dist/Model/LdaModel.d.ts +0 -28
- package/dist/Model/LdaModel.js +0 -67
- package/dist/Model/LdaModel.js.map +0 -1
- package/dist/Model/LinearPerceptronModel.d.ts +0 -24
- package/dist/Model/LinearPerceptronModel.js +0 -91
- package/dist/Model/LinearPerceptronModel.js.map +0 -1
- package/dist/Model/MultiLayerPerceptronModel.d.ts +0 -33
- package/dist/Model/MultiLayerPerceptronModel.js.map +0 -1
- package/dist/Model/NaiveBayesModel.js.map +0 -1
- package/dist/Model/NeuralNetworkModel.js.map +0 -1
- package/dist/Model/QdaModel.d.ts +0 -27
- package/dist/Model/QdaModel.js +0 -63
- package/dist/Model/QdaModel.js.map +0 -1
- package/dist/Model/TreeEnsembleModel.d.ts +0 -22
- package/dist/Model/TreeEnsembleModel.js.map +0 -1
- package/index.js +0 -100
- package/source/Classifier/C45.ts +0 -34
- package/source/Classifier/C45Stump.ts +0 -23
- package/source/Classifier/Classifier.ts +0 -72
- package/source/Classifier/DeepNetwork.ts +0 -26
- package/source/Classifier/Dummy.ts +0 -23
- package/source/Classifier/KMeans.ts +0 -30
- package/source/Classifier/Knn.ts +0 -25
- package/source/Classifier/Lda.ts +0 -47
- package/source/Classifier/LinearPerceptron.ts +0 -27
- package/source/Classifier/MultiLayerPerceptron.ts +0 -27
- package/source/Classifier/NaiveBayes.ts +0 -66
- package/source/Classifier/Qda.ts +0 -46
- package/source/Classifier/RandomClassifier.ts +0 -26
- package/source/Model/KMeansModel.ts +0 -56
- package/source/Model/LdaModel.ts +0 -62
- package/source/Model/LinearPerceptronModel.ts +0 -83
- package/source/Model/QdaModel.ts +0 -57
- /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.
|
|
31
|
-
|
|
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 =
|
|
225
|
+
let distributions = data.attributeClassDistribution(attributeIndex);
|
|
193
226
|
for (let distribution of distributions) {
|
|
194
|
-
sum += (distribution.getSum() /
|
|
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(
|
|
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>
|
|
212
|
-
this.children.push(new DecisionNode(childrenData.get(1), new DecisionCondition(attributeIndex, new DiscreteIndexedAttribute("", -1, (<DiscreteIndexedAttribute>
|
|
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 =
|
|
225
|
-
let childrenData = new Partition(
|
|
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(
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
13
|
+
protected root: DecisionNode
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
constructor(
|
|
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
|
-
|
|
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
|
|
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
|
-
*
|
|
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
|
|
17
|
+
* @param trainSet Training data given to the algorithm.
|
|
16
18
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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 {
|
|
2
|
-
import {InstanceList} from "
|
|
3
|
-
import {Parameter} from "
|
|
4
|
-
import {BaggingParameter} from "
|
|
5
|
-
import {DecisionTree} from "../
|
|
6
|
-
import {DecisionNode} from "../
|
|
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
|
|
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
|
-
|
|
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.
|
|
35
|
+
this.constructor2(fileName)
|
|
34
36
|
}
|
|
37
|
+
|
|
35
38
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {InstanceList} from "
|
|
3
|
-
import {Parameter} from "
|
|
4
|
-
import {RandomForestParameter} from "
|
|
5
|
-
import {DecisionTree} from "../
|
|
6
|
-
import {DecisionNode} from "../
|
|
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
|
|
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
|
-
|
|
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.
|
|
32
|
+
this.constructor2(fileName)
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
}
|
|
@@ -1,30 +1,33 @@
|
|
|
1
|
-
import {Model} from "
|
|
2
|
-
import {Instance} from "
|
|
3
|
-
import {DecisionTree} from "
|
|
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 "
|
|
6
|
+
import {DecisionNode} from "../DecisionTree/DecisionNode";
|
|
7
7
|
|
|
8
|
-
export class TreeEnsembleModel extends Model{
|
|
8
|
+
export abstract class TreeEnsembleModel extends Model{
|
|
9
9
|
|
|
10
|
-
|
|
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
|
|
15
|
+
* @param forest An {@link Array} of {@link DecisionTree}.
|
|
16
16
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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) {
|