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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {GaussianModel} from "./GaussianModel";
|
|
2
|
+
import {InstanceList} from "../../InstanceList/InstanceList";
|
|
3
|
+
import {DistanceMetric} from "../../DistanceMetric/DistanceMetric";
|
|
4
|
+
import {Instance} from "../../Instance/Instance";
|
|
5
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
6
|
+
import {EuclidianDistance} from "../../DistanceMetric/EuclidianDistance";
|
|
7
|
+
import {Parameter} from "../../Parameter/Parameter";
|
|
8
|
+
import {Partition} from "../../InstanceList/Partition";
|
|
9
|
+
import {KMeansParameter} from "../../Parameter/KMeansParameter";
|
|
10
|
+
|
|
11
|
+
export class KMeansModel extends GaussianModel{
|
|
12
|
+
|
|
13
|
+
private classMeans: InstanceList
|
|
14
|
+
private distanceMetric: DistanceMetric
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Loads a K-means model from an input model file.
|
|
18
|
+
* @param fileName Model file name.
|
|
19
|
+
*/
|
|
20
|
+
constructor2(fileName: string) {
|
|
21
|
+
this.distanceMetric = new EuclidianDistance()
|
|
22
|
+
let input = new FileContents(fileName)
|
|
23
|
+
this.loadPriorDistribution(input)
|
|
24
|
+
this.classMeans = this.loadInstanceList(input)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The calculateMetric method takes an {@link Instance} and a String as inputs. It loops through the class means, if
|
|
29
|
+
* the corresponding class label is same as the given String it returns the negated distance between given instance and the
|
|
30
|
+
* current item of class means. Otherwise it returns the smallest negative number.
|
|
31
|
+
*
|
|
32
|
+
* @param instance {@link Instance} input.
|
|
33
|
+
* @param Ci String input.
|
|
34
|
+
* @return The negated distance between given instance and the current item of class means.
|
|
35
|
+
*/
|
|
36
|
+
calculateMetric(instance: Instance, Ci: string): number {
|
|
37
|
+
for (let i = 0; i < this.classMeans.size(); i++) {
|
|
38
|
+
if (this.classMeans.get(i).getClassLabel() == Ci) {
|
|
39
|
+
return -this.distanceMetric.distance(instance, this.classMeans.get(i));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Number.NEGATIVE_INFINITY;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
saveTxt(fileName: string){
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Training algorithm for K-Means classifier. K-Means finds the mean of each class for training.
|
|
50
|
+
*
|
|
51
|
+
* @param trainSet Training data given to the algorithm.
|
|
52
|
+
* @param parameters distanceMetric: distance metric used to calculate the distance between two instances.
|
|
53
|
+
*/
|
|
54
|
+
train(trainSet: InstanceList, parameters: Parameter): void {
|
|
55
|
+
this.priorDistribution = trainSet.classDistribution();
|
|
56
|
+
this.classMeans = new InstanceList();
|
|
57
|
+
let classLists = new Partition(trainSet);
|
|
58
|
+
for (let i = 0; i < classLists.size(); i++) {
|
|
59
|
+
this.classMeans.add(classLists.get(i).average());
|
|
60
|
+
}
|
|
61
|
+
this.distanceMetric = (<KMeansParameter> parameters).getDistanceMetric()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Loads the K-means model from an input file.
|
|
66
|
+
* @param fileName File name of the K-means model.
|
|
67
|
+
*/
|
|
68
|
+
loadModel(fileName: string): void{
|
|
69
|
+
this.constructor2(fileName)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {GaussianModel} from "./GaussianModel";
|
|
2
|
+
import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
3
|
+
import {Instance} from "../../Instance/Instance";
|
|
4
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
5
|
+
import {InstanceList} from "../../InstanceList/InstanceList";
|
|
6
|
+
import {Parameter} from "../../Parameter/Parameter";
|
|
7
|
+
import {Partition} from "../../InstanceList/Partition";
|
|
8
|
+
import {Matrix} from "nlptoolkit-math/dist/Matrix";
|
|
9
|
+
import {InstanceListOfSameClass} from "../../InstanceList/InstanceListOfSameClass";
|
|
10
|
+
|
|
11
|
+
export class LdaModel extends GaussianModel{
|
|
12
|
+
|
|
13
|
+
protected w0: Map<string, number>
|
|
14
|
+
protected w: Map<string, Vector>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Loads a Linear Discriminant Analysis model from an input model file.
|
|
18
|
+
* @param fileName Model file name.
|
|
19
|
+
*/
|
|
20
|
+
constructor2(fileName: string) {
|
|
21
|
+
let input = new FileContents(fileName)
|
|
22
|
+
let size = this.loadPriorDistribution(input)
|
|
23
|
+
this.loadWandW0(input, size)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The calculateMetric method takes an {@link Instance} and a String as inputs. It returns the dot product of given Instance
|
|
28
|
+
* and wi plus w0i.
|
|
29
|
+
*
|
|
30
|
+
* @param instance {@link Instance} input.
|
|
31
|
+
* @param Ci String input.
|
|
32
|
+
* @return The dot product of given Instance and wi plus w0i.
|
|
33
|
+
*/
|
|
34
|
+
calculateMetric(instance: Instance, Ci: string): number {
|
|
35
|
+
let xi = instance.toVector();
|
|
36
|
+
let wi = this.w.get(Ci);
|
|
37
|
+
let w0i = this.w0.get(Ci);
|
|
38
|
+
return wi.dotProduct(xi) + w0i;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Loads w0 and w hash maps from an input file. The number of items in the hash map is given by the parameter size.
|
|
43
|
+
* @param input Input file
|
|
44
|
+
* @param size Number of items in the hash map read.
|
|
45
|
+
* @throws IOException If the file can not be read, it throws IOException.
|
|
46
|
+
*/
|
|
47
|
+
loadWandW0(input: FileContents, size: number){
|
|
48
|
+
this.w0 = new Map<string, number>()
|
|
49
|
+
for (let i = 0; i < size; i++){
|
|
50
|
+
let line = input.readLine()
|
|
51
|
+
let items = line.split(" ")
|
|
52
|
+
this.w0.set(items[0], parseFloat(items[1]))
|
|
53
|
+
}
|
|
54
|
+
this.w = this.loadVectors(input, size)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
saveTxt(fileName: string){
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Training algorithm for the linear discriminant analysis classifier (Introduction to Machine Learning, Alpaydin, 2015).
|
|
62
|
+
*
|
|
63
|
+
* @param trainSet Training data given to the algorithm.
|
|
64
|
+
* @param parameters -
|
|
65
|
+
*/
|
|
66
|
+
train(trainSet: InstanceList, parameters: Parameter): void {
|
|
67
|
+
this.w0 = new Map<string, number>();
|
|
68
|
+
this.w = new Map<string, Vector>();
|
|
69
|
+
this.priorDistribution = trainSet.classDistribution();
|
|
70
|
+
let classLists = new Partition(trainSet);
|
|
71
|
+
let covariance = new Matrix(trainSet.get(0).continuousAttributeSize(), trainSet.get(0).continuousAttributeSize());
|
|
72
|
+
for (let i = 0; i < classLists.size(); i++) {
|
|
73
|
+
let averageVector = new Vector(classLists.get(i).continuousAttributeAverage());
|
|
74
|
+
let classCovariance = classLists.get(i).covariance(averageVector);
|
|
75
|
+
classCovariance.multiplyWithConstant(classLists.get(i).size() - 1);
|
|
76
|
+
covariance.add(classCovariance);
|
|
77
|
+
}
|
|
78
|
+
covariance.divideByConstant(trainSet.size() - classLists.size());
|
|
79
|
+
covariance.inverse();
|
|
80
|
+
for (let i = 0; i < classLists.size(); i++) {
|
|
81
|
+
let Ci = (<InstanceListOfSameClass> classLists.get(i)).getClassLabel();
|
|
82
|
+
let averageVector = new Vector(classLists.get(i).continuousAttributeAverage());
|
|
83
|
+
let wi = covariance.multiplyWithVectorFromRight(averageVector);
|
|
84
|
+
this.w.set(Ci, wi);
|
|
85
|
+
let w0i = -0.5 * wi.dotProduct(averageVector) + Math.log(this.priorDistribution.getProbability(Ci));
|
|
86
|
+
this.w0.set(Ci, w0i);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Loads the Lda model from an input file.
|
|
92
|
+
* @param fileName File name of the Lda model.
|
|
93
|
+
*/
|
|
94
|
+
loadModel(fileName: string): void{
|
|
95
|
+
this.constructor2(fileName)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
}
|
|
@@ -1,40 +1,30 @@
|
|
|
1
1
|
import {GaussianModel} from "./GaussianModel";
|
|
2
|
-
import {Instance} from "
|
|
2
|
+
import {Instance} from "../../Instance/Instance";
|
|
3
3
|
import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
4
4
|
import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
|
|
5
|
-
import {ContinuousAttribute} from "
|
|
6
|
-
import {DiscreteAttribute} from "
|
|
5
|
+
import {ContinuousAttribute} from "../../Attribute/ContinuousAttribute";
|
|
6
|
+
import {DiscreteAttribute} from "../../Attribute/DiscreteAttribute";
|
|
7
7
|
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
8
|
+
import {Partition} from "../../InstanceList/Partition";
|
|
9
|
+
import {InstanceListOfSameClass} from "../../InstanceList/InstanceListOfSameClass";
|
|
10
|
+
import {InstanceList} from "../../InstanceList/InstanceList";
|
|
11
|
+
import {Parameter} from "../../Parameter/Parameter";
|
|
8
12
|
|
|
9
13
|
export class NaiveBayesModel extends GaussianModel{
|
|
10
14
|
|
|
11
15
|
private classMeans: Map<string, Vector> = undefined
|
|
12
16
|
private classDeviations: Map<string, Vector> = undefined
|
|
13
|
-
private
|
|
17
|
+
private classAttributeDistributions: Map<string, Array<DiscreteDistribution>> = undefined
|
|
14
18
|
|
|
15
19
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param priorDistribution {@link DiscreteDistribution} input.
|
|
19
|
-
* @param classMeans A {@link Map} of String and {@link Vector}.
|
|
20
|
-
* @param classDeviations A {@link Map} of String and {@link Vector}.
|
|
20
|
+
* Loads a naive Bayes model from an input model file.
|
|
21
|
+
* @param fileName Model file name.
|
|
21
22
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.classMeans = classMeans
|
|
28
|
-
this.classDeviations = classDeviations
|
|
29
|
-
} else {
|
|
30
|
-
this.classAttributeDistributions = classMeans
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
let input = new FileContents(priorDistribution)
|
|
34
|
-
let size = this.loadPriorDistribution(input)
|
|
35
|
-
this.classMeans = this.loadVectors(input, size)
|
|
36
|
-
this.classDeviations = this.loadVectors(input, size)
|
|
37
|
-
}
|
|
23
|
+
constructor3(fileName: string) {
|
|
24
|
+
let input = new FileContents(fileName)
|
|
25
|
+
let size = this.loadPriorDistribution(input)
|
|
26
|
+
this.classMeans = this.loadVectors(input, size)
|
|
27
|
+
this.classDeviations = this.loadVectors(input, size)
|
|
38
28
|
}
|
|
39
29
|
|
|
40
30
|
/**
|
|
@@ -98,4 +88,57 @@ export class NaiveBayesModel extends GaussianModel{
|
|
|
98
88
|
saveTxt(fileName: string){
|
|
99
89
|
}
|
|
100
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Training algorithm for Naive Bayes algorithm with a continuous data set.
|
|
93
|
+
*
|
|
94
|
+
* @param classLists Instances are divided into K lists, where each list contains only instances from a single class
|
|
95
|
+
*/
|
|
96
|
+
private trainContinuousVersion(classLists: Partition){
|
|
97
|
+
this.classMeans = new Map<string, Vector>();
|
|
98
|
+
this.classDeviations = new Map<string, Vector>();
|
|
99
|
+
for (let i = 0; i < classLists.size(); i++){
|
|
100
|
+
let classLabel = (<InstanceListOfSameClass> classLists.get(i)).getClassLabel();
|
|
101
|
+
let averageVector = classLists.get(i).average().toVector();
|
|
102
|
+
this.classMeans.set(classLabel, averageVector);
|
|
103
|
+
let standardDeviationVector = classLists.get(i).standardDeviation().toVector();
|
|
104
|
+
this.classDeviations.set(classLabel, standardDeviationVector);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Training algorithm for Naive Bayes algorithm with a discrete data set.
|
|
110
|
+
* @param classLists Instances are divided into K lists, where each list contains only instances from a single class
|
|
111
|
+
*/
|
|
112
|
+
private trainDiscreteVersion(classLists: Partition){
|
|
113
|
+
this.classAttributeDistributions = new Map<string, Array<DiscreteDistribution>>();
|
|
114
|
+
for (let i = 0; i < classLists.size(); i++){
|
|
115
|
+
this.classAttributeDistributions.set((<InstanceListOfSameClass> classLists.get(i)).getClassLabel(),
|
|
116
|
+
classLists.get(i).allAttributesDistribution());
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Training algorithm for Naive Bayes algorithm. It basically calls trainContinuousVersion for continuous data sets,
|
|
122
|
+
* trainDiscreteVersion for discrete data sets.
|
|
123
|
+
* @param trainSet Training data given to the algorithm
|
|
124
|
+
* @param parameters -
|
|
125
|
+
*/
|
|
126
|
+
train(trainSet: InstanceList, parameters: Parameter): void {
|
|
127
|
+
this.priorDistribution = trainSet.classDistribution();
|
|
128
|
+
let classLists = new Partition(trainSet);
|
|
129
|
+
if (classLists.get(0).get(0).getAttribute(0) instanceof DiscreteAttribute){
|
|
130
|
+
this.trainDiscreteVersion(classLists);
|
|
131
|
+
} else {
|
|
132
|
+
this.trainContinuousVersion(classLists);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Loads the naive Bayes model from an input file.
|
|
138
|
+
* @param fileName File name of the naive Bayes model.
|
|
139
|
+
*/
|
|
140
|
+
loadModel(fileName: string): void{
|
|
141
|
+
this.constructor3(fileName)
|
|
142
|
+
}
|
|
143
|
+
|
|
101
144
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {LdaModel} from "./LdaModel";
|
|
2
|
+
import {Matrix} from "nlptoolkit-math/dist/Matrix";
|
|
3
|
+
import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
4
|
+
import {Instance} from "../../Instance/Instance";
|
|
5
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
6
|
+
import {InstanceList} from "../../InstanceList/InstanceList";
|
|
7
|
+
import {Parameter} from "../../Parameter/Parameter";
|
|
8
|
+
import {Partition} from "../../InstanceList/Partition";
|
|
9
|
+
import {InstanceListOfSameClass} from "../../InstanceList/InstanceListOfSameClass";
|
|
10
|
+
|
|
11
|
+
export class QdaModel extends LdaModel{
|
|
12
|
+
|
|
13
|
+
private W: Map<string, Matrix>
|
|
14
|
+
|
|
15
|
+
constructor2(fileName: string) {
|
|
16
|
+
let input = new FileContents(fileName)
|
|
17
|
+
let size = this.loadPriorDistribution(input)
|
|
18
|
+
this.loadWandW0(input, size)
|
|
19
|
+
this.W = new Map<string, Matrix>()
|
|
20
|
+
for (let i = 0; i < size; i++){
|
|
21
|
+
let c = input.readLine()
|
|
22
|
+
let matrix = this.loadMatrix(input)
|
|
23
|
+
this.W.set(c, matrix)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The calculateMetric method takes an {@link Instance} and a String as inputs. It multiplies Matrix Wi with Vector xi
|
|
29
|
+
* then calculates the dot product of it with xi. Then, again it finds the dot product of wi and xi and returns the summation with w0i.
|
|
30
|
+
*
|
|
31
|
+
* @param instance {@link Instance} input.
|
|
32
|
+
* @param Ci String input.
|
|
33
|
+
* @return The result of Wi.multiplyWithVectorFromLeft(xi).dotProduct(xi) + wi.dotProduct(xi) + w0i.
|
|
34
|
+
*/
|
|
35
|
+
calculateMetric(instance: Instance, Ci: string): number {
|
|
36
|
+
let xi = instance.toVector();
|
|
37
|
+
let Wi = this.W.get(Ci);
|
|
38
|
+
let wi = this.w.get(Ci);
|
|
39
|
+
let w0i = this.w0.get(Ci);
|
|
40
|
+
return Wi.multiplyWithVectorFromLeft(xi).dotProduct(xi) + wi.dotProduct(xi) + w0i;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
saveTxt(fileName: string){
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Training algorithm for the quadratic discriminant analysis classifier (Introduction to Machine Learning, Alpaydin, 2015).
|
|
48
|
+
*
|
|
49
|
+
* @param trainSet Training data given to the algorithm.
|
|
50
|
+
* @param parameters -
|
|
51
|
+
*/
|
|
52
|
+
train(trainSet: InstanceList, parameters: Parameter) {
|
|
53
|
+
let determinant = 0
|
|
54
|
+
this.w0 = new Map<string, number>();
|
|
55
|
+
this.w = new Map<string, Vector>();
|
|
56
|
+
this.W = new Map<string, Matrix>();
|
|
57
|
+
let classLists = new Partition(trainSet);
|
|
58
|
+
this.priorDistribution = trainSet.classDistribution();
|
|
59
|
+
for (let i = 0; i < classLists.size(); i++) {
|
|
60
|
+
let Ci = (<InstanceListOfSameClass> classLists.get(i)).getClassLabel();
|
|
61
|
+
let averageVector = new Vector(classLists.get(i).continuousAttributeAverage());
|
|
62
|
+
let classCovariance = classLists.get(i).covariance(averageVector);
|
|
63
|
+
determinant = classCovariance.determinant();
|
|
64
|
+
classCovariance.inverse();
|
|
65
|
+
let Wi = classCovariance.clone();
|
|
66
|
+
Wi.multiplyWithConstant(-0.5);
|
|
67
|
+
this.W.set(Ci, Wi);
|
|
68
|
+
let wi = classCovariance.multiplyWithVectorFromLeft(averageVector);
|
|
69
|
+
this.w.set(Ci, wi);
|
|
70
|
+
let w0i = -0.5 * (wi.dotProduct(averageVector) + Math.log(determinant)) + Math.log(this.priorDistribution.getProbability(Ci));
|
|
71
|
+
this.w0.set(Ci, w0i);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Loads the Qda model from an input file.
|
|
77
|
+
* @param fileName File name of the Qda model.
|
|
78
|
+
*/
|
|
79
|
+
loadModel(fileName: string): void{
|
|
80
|
+
this.constructor2(fileName)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
@@ -3,28 +3,39 @@ import {Instance} from "../Instance/Instance";
|
|
|
3
3
|
import {CompositeInstance} from "../Instance/CompositeInstance";
|
|
4
4
|
import {Random} from "nlptoolkit-util/dist/Random";
|
|
5
5
|
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
6
|
+
import {InstanceList} from "../InstanceList/InstanceList";
|
|
7
|
+
import {Parameter} from "../Parameter/Parameter";
|
|
6
8
|
|
|
7
9
|
export class RandomModel extends Model{
|
|
8
10
|
|
|
9
|
-
private
|
|
11
|
+
private classLabels: Array<string>
|
|
10
12
|
private random: Random
|
|
11
13
|
private seed: number
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
/**
|
|
16
|
+
* A constructor that sets the class labels.
|
|
17
|
+
*
|
|
18
|
+
* @param classLabels An ArrayList of class labels.
|
|
19
|
+
* @param seed Seed of the random function.
|
|
20
|
+
*/
|
|
21
|
+
constructor1(classLabels: Array<string>, seed: number) {
|
|
22
|
+
this.classLabels = classLabels
|
|
23
|
+
this.random = new Random(seed)
|
|
24
|
+
this.seed = seed
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Loads a random classifier model from an input model file.
|
|
29
|
+
* @param fileName Model file name.
|
|
30
|
+
*/
|
|
31
|
+
constructor2(fileName: string) {
|
|
32
|
+
let input = new FileContents(fileName)
|
|
33
|
+
this.seed = parseInt(input.readLine())
|
|
34
|
+
this.random = new Random(this.seed)
|
|
35
|
+
let size = parseInt(input.readLine())
|
|
36
|
+
this.classLabels = new Array<string>()
|
|
37
|
+
for (let i = 0; i < size; i++){
|
|
38
|
+
this.classLabels.push(input.readLine())
|
|
28
39
|
}
|
|
29
40
|
}
|
|
30
41
|
|
|
@@ -48,6 +59,11 @@ export class RandomModel extends Model{
|
|
|
48
59
|
}
|
|
49
60
|
}
|
|
50
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Calculates the posterior probability distribution for the given instance according to random model.
|
|
64
|
+
* @param instance Instance for which posterior probability distribution is calculated.
|
|
65
|
+
* @return Posterior probability distribution for the given instance.
|
|
66
|
+
*/
|
|
51
67
|
predictProbability(instance: Instance): Map<string, number> {
|
|
52
68
|
let result = new Map<string, number>();
|
|
53
69
|
for (let classLabel of this.classLabels){
|
|
@@ -59,4 +75,26 @@ export class RandomModel extends Model{
|
|
|
59
75
|
saveTxt(fileName: string){
|
|
60
76
|
}
|
|
61
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Training algorithm for random classifier.
|
|
80
|
+
*
|
|
81
|
+
* @param trainSet Training data given to the algorithm.
|
|
82
|
+
* @param parameters -
|
|
83
|
+
*/
|
|
84
|
+
train(trainSet: InstanceList, parameters: Parameter): void {
|
|
85
|
+
let result = new Array<string>()
|
|
86
|
+
for (let s of trainSet.classDistribution().keys()){
|
|
87
|
+
result.push(s)
|
|
88
|
+
}
|
|
89
|
+
this.constructor1(result, parameters.getSeed());
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Loads the random classifier model from an input file.
|
|
94
|
+
* @param fileName File name of the random classifier model.
|
|
95
|
+
*/
|
|
96
|
+
loadModel(fileName: string): void{
|
|
97
|
+
this.constructor2(fileName)
|
|
98
|
+
}
|
|
99
|
+
|
|
62
100
|
}
|
|
@@ -2,8 +2,8 @@ import {Parameter} from "./Parameter";
|
|
|
2
2
|
|
|
3
3
|
export class C45Parameter extends Parameter{
|
|
4
4
|
|
|
5
|
-
private prune: boolean
|
|
6
|
-
private crossValidationRatio: number
|
|
5
|
+
private readonly prune: boolean
|
|
6
|
+
private readonly crossValidationRatio: number
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Parameters of the C4.5 univariate decision tree classifier.
|
|
@@ -2,8 +2,8 @@ import {LinearPerceptronParameter} from "./LinearPerceptronParameter";
|
|
|
2
2
|
import {ActivationFunction} from "./ActivationFunction";
|
|
3
3
|
|
|
4
4
|
export class DeepNetworkParameter extends LinearPerceptronParameter{
|
|
5
|
-
private hiddenLayers: Array<number>
|
|
6
|
-
private activationFunction: ActivationFunction
|
|
5
|
+
private readonly hiddenLayers: Array<number>
|
|
6
|
+
private readonly activationFunction: ActivationFunction
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Parameters of the deep network classifier.
|
|
@@ -2,10 +2,10 @@ import {Parameter} from "./Parameter";
|
|
|
2
2
|
|
|
3
3
|
export class LinearPerceptronParameter extends Parameter{
|
|
4
4
|
|
|
5
|
-
private learningRate: number
|
|
6
|
-
private etaDecrease: number
|
|
7
|
-
private crossValidationRatio: number
|
|
8
|
-
private epoch: number
|
|
5
|
+
private readonly learningRate: number
|
|
6
|
+
private readonly etaDecrease: number
|
|
7
|
+
private readonly crossValidationRatio: number
|
|
8
|
+
private readonly epoch: number
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Parameters of the linear perceptron algorithm.
|
|
@@ -3,8 +3,8 @@ import {ActivationFunction} from "./ActivationFunction";
|
|
|
3
3
|
|
|
4
4
|
export class MultiLayerPerceptronParameter extends LinearPerceptronParameter{
|
|
5
5
|
|
|
6
|
-
private hiddenNodes: number
|
|
7
|
-
private activationFunction: ActivationFunction
|
|
6
|
+
private readonly hiddenNodes: number
|
|
7
|
+
private readonly activationFunction: ActivationFunction
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Parameters of the multi layer perceptron algorithm.
|
|
@@ -3,7 +3,7 @@ import {CounterHashMap} from "nlptoolkit-datastructure/dist/CounterHashMap";
|
|
|
3
3
|
export class ConfusionMatrix {
|
|
4
4
|
|
|
5
5
|
private matrix: Map<string, CounterHashMap<string>> = new Map<string, CounterHashMap<string>>()
|
|
6
|
-
private classLabels: Array<string>
|
|
6
|
+
private readonly classLabels: Array<string>
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Constructor that sets the class labels {@link Array} and creates new {@link Map} matrix
|
|
@@ -5,6 +5,12 @@ import {Distribution} from "nlptoolkit-math/dist/Distribution";
|
|
|
5
5
|
|
|
6
6
|
export class Combined5x2F extends PairedTest{
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Calculates the test statistic of the combined 5x2 cv F test.
|
|
10
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
11
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
12
|
+
* @return Given the performances of two classifiers, the test statistic of the combined 5x2 cv F test.
|
|
13
|
+
*/
|
|
8
14
|
private testStatistic(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): number{
|
|
9
15
|
let difference = new Array<number>();
|
|
10
16
|
let numerator = 0
|
|
@@ -23,6 +29,12 @@ export class Combined5x2F extends PairedTest{
|
|
|
23
29
|
return numerator / denominator;
|
|
24
30
|
}
|
|
25
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Compares two classification algorithms based on their performances (accuracy or error rate) using combined 5x2 cv F test.
|
|
34
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
35
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
36
|
+
* @return Statistical test result of the comparison.
|
|
37
|
+
*/
|
|
26
38
|
compareClassifiers(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): StatisticalTestResult {
|
|
27
39
|
let statistic = this.testStatistic(classifier1, classifier2);
|
|
28
40
|
let degreeOfFreedom1 = classifier1.numberOfExperiments();
|
|
@@ -5,6 +5,12 @@ import {Distribution} from "nlptoolkit-math/dist/Distribution";
|
|
|
5
5
|
|
|
6
6
|
export class Combined5x2t extends PairedTest{
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Calculates the test statistic of the combined 5x2 cv t test.
|
|
10
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
11
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
12
|
+
* @return Given the performances of two classifiers, the test statistic of the combined 5x2 cv t test.
|
|
13
|
+
*/
|
|
8
14
|
private testStatistic(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): number{
|
|
9
15
|
let difference = new Array<number>();
|
|
10
16
|
for (let i = 0; i < classifier1.numberOfExperiments(); i++){
|
|
@@ -24,6 +30,13 @@ export class Combined5x2t extends PairedTest{
|
|
|
24
30
|
return numerator / denominator;
|
|
25
31
|
}
|
|
26
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Compares two classification algorithms based on their performances (accuracy or error rate) using combined 5x2
|
|
35
|
+
* cv t test.
|
|
36
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
37
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
38
|
+
* @return Statistical test result of the comparison.
|
|
39
|
+
*/
|
|
27
40
|
compareClassifiers(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): StatisticalTestResult {
|
|
28
41
|
let statistic = this.testStatistic(classifier1, classifier2);
|
|
29
42
|
let degreeOfFreedom = classifier1.numberOfExperiments() / 2;
|
|
@@ -5,6 +5,12 @@ import {Distribution} from "nlptoolkit-math/dist/Distribution";
|
|
|
5
5
|
|
|
6
6
|
export class Paired5x2t extends PairedTest{
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Calculates the test statistic of the 5x2 t test.
|
|
10
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
11
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
12
|
+
* @return Given the performances of two classifiers, the test statistic of the 5x2 t test.
|
|
13
|
+
*/
|
|
8
14
|
private testStatistic(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): number{
|
|
9
15
|
let difference = new Array<number>();
|
|
10
16
|
let sum = 0.0;
|
|
@@ -23,6 +29,12 @@ export class Paired5x2t extends PairedTest{
|
|
|
23
29
|
return difference[0] / denominator;
|
|
24
30
|
}
|
|
25
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Compares two classification algorithms based on their performances (accuracy or error rate) using 5x2 t test.
|
|
34
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
35
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
36
|
+
* @return Statistical test result of the comparison.
|
|
37
|
+
*/
|
|
26
38
|
compareClassifiers(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): StatisticalTestResult {
|
|
27
39
|
let statistic = this.testStatistic(classifier1, classifier2);
|
|
28
40
|
let degreeOfFreedom = classifier1.numberOfExperiments() / 2;
|
|
@@ -6,6 +6,18 @@ export abstract class PairedTest {
|
|
|
6
6
|
|
|
7
7
|
abstract compareClassifiers(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance): StatisticalTestResult
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Compares two classification algorithms based on their performances (accuracy or error rate). The method first
|
|
11
|
+
* checks the null hypothesis mu1 < mu2, if the test rejects this null hypothesis with alpha level of confidence, it
|
|
12
|
+
* decides mu1 > mu2. The algorithm then checks the null hypothesis mu1 > mu2, if the test rejects that null
|
|
13
|
+
* hypothesis with alpha level of confidence, if decides mu1 < mu2. If none of the two tests are rejected, it can not
|
|
14
|
+
* make a decision about the performances of algorithms.
|
|
15
|
+
* @param classifier1 Performance (error rate or accuracy) results of the first classifier.
|
|
16
|
+
* @param classifier2 Performance (error rate or accuracy) results of the second classifier.
|
|
17
|
+
* @param alpha Alpha level defined for the statistical test.
|
|
18
|
+
* @return 1 if the performance of the first algorithm is larger than the second algorithm, -1 if the performance of
|
|
19
|
+
* the second algorithm is larger than the first algorithm, 0 if they have similar performance.
|
|
20
|
+
*/
|
|
9
21
|
compare(classifier1: ExperimentPerformance, classifier2: ExperimentPerformance, alpha: number): number{
|
|
10
22
|
let testResult1 = this.compareClassifiers(classifier1, classifier2);
|
|
11
23
|
let testResult2 = this.compareClassifiers(classifier2, classifier1);
|