nlptoolkit-classification 1.0.3 → 1.0.4
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/dist/Classifier/Bagging.d.ts +1 -0
- package/dist/Classifier/Bagging.js +3 -0
- package/dist/Classifier/Bagging.js.map +1 -1
- package/dist/Classifier/C45.d.ts +1 -0
- package/dist/Classifier/C45.js +3 -0
- package/dist/Classifier/C45.js.map +1 -1
- package/dist/Classifier/C45Stump.d.ts +1 -0
- package/dist/Classifier/C45Stump.js +3 -0
- package/dist/Classifier/C45Stump.js.map +1 -1
- package/dist/Classifier/Classifier.d.ts +1 -0
- package/dist/Classifier/Classifier.js.map +1 -1
- package/dist/Classifier/DeepNetwork.d.ts +1 -0
- package/dist/Classifier/DeepNetwork.js +3 -0
- package/dist/Classifier/DeepNetwork.js.map +1 -1
- package/dist/Classifier/Dummy.d.ts +1 -0
- package/dist/Classifier/Dummy.js +3 -0
- package/dist/Classifier/Dummy.js.map +1 -1
- package/dist/Classifier/KMeans.d.ts +1 -0
- package/dist/Classifier/KMeans.js +3 -0
- package/dist/Classifier/KMeans.js.map +1 -1
- package/dist/Classifier/Knn.d.ts +1 -0
- package/dist/Classifier/Knn.js +3 -0
- package/dist/Classifier/Knn.js.map +1 -1
- package/dist/Classifier/Lda.d.ts +1 -0
- package/dist/Classifier/Lda.js +3 -0
- package/dist/Classifier/Lda.js.map +1 -1
- package/dist/Classifier/LinearPerceptron.d.ts +1 -0
- package/dist/Classifier/LinearPerceptron.js +3 -0
- package/dist/Classifier/LinearPerceptron.js.map +1 -1
- package/dist/Classifier/MultiLayerPerceptron.d.ts +1 -0
- package/dist/Classifier/MultiLayerPerceptron.js +3 -0
- package/dist/Classifier/MultiLayerPerceptron.js.map +1 -1
- package/dist/Classifier/NaiveBayes.d.ts +1 -0
- package/dist/Classifier/NaiveBayes.js +3 -0
- package/dist/Classifier/NaiveBayes.js.map +1 -1
- package/dist/Classifier/Qda.d.ts +1 -0
- package/dist/Classifier/Qda.js +3 -0
- package/dist/Classifier/Qda.js.map +1 -1
- package/dist/Classifier/RandomClassifier.d.ts +1 -0
- package/dist/Classifier/RandomClassifier.js +3 -0
- package/dist/Classifier/RandomClassifier.js.map +1 -1
- package/dist/Classifier/RandomForest.d.ts +1 -0
- package/dist/Classifier/RandomForest.js +3 -0
- package/dist/Classifier/RandomForest.js.map +1 -1
- package/dist/DataSet/DataSet.js +1 -1
- package/dist/DataSet/DataSet.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionNode.d.ts +6 -3
- package/dist/Model/DecisionTree/DecisionNode.js +43 -3
- package/dist/Model/DecisionTree/DecisionNode.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionTree.d.ts +4 -3
- package/dist/Model/DecisionTree/DecisionTree.js +14 -4
- package/dist/Model/DecisionTree/DecisionTree.js.map +1 -1
- package/dist/Model/DeepNetworkModel.d.ts +6 -3
- package/dist/Model/DeepNetworkModel.js +53 -30
- package/dist/Model/DeepNetworkModel.js.map +1 -1
- package/dist/Model/DummyModel.d.ts +2 -1
- package/dist/Model/DummyModel.js +22 -2
- package/dist/Model/DummyModel.js.map +1 -1
- package/dist/Model/GaussianModel.d.ts +4 -0
- package/dist/Model/GaussianModel.js +28 -1
- package/dist/Model/GaussianModel.js.map +1 -1
- package/dist/Model/KMeansModel.d.ts +3 -2
- package/dist/Model/KMeansModel.js +19 -6
- package/dist/Model/KMeansModel.js.map +1 -1
- package/dist/Model/KnnModel.d.ts +3 -2
- package/dist/Model/KnnModel.js +18 -6
- package/dist/Model/KnnModel.js.map +1 -1
- package/dist/Model/LdaModel.d.ts +5 -2
- package/dist/Model/LdaModel.js +28 -6
- package/dist/Model/LdaModel.js.map +1 -1
- package/dist/Model/LinearPerceptronModel.d.ts +5 -2
- package/dist/Model/LinearPerceptronModel.js +38 -10
- package/dist/Model/LinearPerceptronModel.js.map +1 -1
- package/dist/Model/Model.d.ts +7 -0
- package/dist/Model/Model.js +40 -1
- package/dist/Model/Model.js.map +1 -1
- package/dist/Model/MultiLayerPerceptronModel.d.ts +5 -2
- package/dist/Model/MultiLayerPerceptronModel.js +33 -13
- package/dist/Model/MultiLayerPerceptronModel.js.map +1 -1
- package/dist/Model/NaiveBayesModel.d.ts +2 -1
- package/dist/Model/NaiveBayesModel.js +18 -6
- package/dist/Model/NaiveBayesModel.js.map +1 -1
- package/dist/Model/NeuralNetworkModel.d.ts +4 -1
- package/dist/Model/NeuralNetworkModel.js +27 -6
- package/dist/Model/NeuralNetworkModel.js.map +1 -1
- package/dist/Model/QdaModel.d.ts +3 -2
- package/dist/Model/QdaModel.js +23 -5
- package/dist/Model/QdaModel.js.map +1 -1
- package/dist/Model/RandomModel.d.ts +4 -2
- package/dist/Model/RandomModel.js +20 -4
- package/dist/Model/RandomModel.js.map +1 -1
- package/dist/Model/TreeEnsembleModel.d.ts +3 -2
- package/dist/Model/TreeEnsembleModel.js +19 -4
- package/dist/Model/TreeEnsembleModel.js.map +1 -1
- package/models/bagging-bupa.txt +25666 -0
- package/models/bagging-car.txt +78923 -0
- package/models/bagging-dermatology.txt +7276 -0
- package/models/bagging-iris.txt +3131 -0
- package/models/bagging-tictactoe.txt +61186 -0
- package/models/c45-bupa.txt +3 -0
- package/models/c45-car.txt +331 -0
- package/models/c45-dermatology.txt +43 -0
- package/models/c45-iris.txt +13 -0
- package/models/c45-tictactoe.txt +270 -0
- package/models/c45stump-bupa.txt +8 -0
- package/models/c45stump-car.txt +11 -0
- package/models/c45stump-chess.txt +8 -0
- package/models/c45stump-dermatology.txt +8 -0
- package/models/c45stump-iris.txt +8 -0
- package/models/c45stump-nursery.txt +11 -0
- package/models/c45stump-tictactoe.txt +11 -0
- package/models/deepNetwork-bupa.txt +40 -0
- package/models/deepNetwork-dermatology.txt +37 -0
- package/models/deepNetwork-iris.txt +22 -0
- package/models/dummy-bupa.txt +3 -0
- package/models/dummy-car.txt +5 -0
- package/models/dummy-chess.txt +19 -0
- package/models/dummy-dermatology.txt +7 -0
- package/models/dummy-iris.txt +4 -0
- package/models/dummy-nursery.txt +6 -0
- package/models/dummy-tictactoe.txt +3 -0
- package/models/kMeans-bupa.txt +7 -0
- package/models/kMeans-car.txt +11 -0
- package/models/kMeans-chess.txt +39 -0
- package/models/kMeans-dermatology.txt +15 -0
- package/models/kMeans-iris.txt +9 -0
- package/models/kMeans-nursery.txt +13 -0
- package/models/kMeans-tictactoe.txt +7 -0
- package/models/knn-bupa.txt +348 -0
- package/models/knn-car.txt +1731 -0
- package/models/knn-dermatology.txt +369 -0
- package/models/knn-iris.txt +153 -0
- package/models/knn-tictactoe.txt +961 -0
- package/models/lda-bupa.txt +7 -0
- package/models/lda-dermatology.txt +19 -0
- package/models/lda-iris.txt +10 -0
- package/models/linearPerceptron-bupa.txt +6 -0
- package/models/linearPerceptron-dermatology.txt +14 -0
- package/models/linearPerceptron-iris.txt +8 -0
- package/models/multiLayerPerceptron-bupa.txt +38 -0
- package/models/multiLayerPerceptron-dermatology.txt +36 -0
- package/models/multiLayerPerceptron-iris.txt +13 -0
- package/models/naiveBayes-bupa.txt +7 -0
- package/models/naiveBayes-dermatology.txt +19 -0
- package/models/naiveBayes-iris.txt +10 -0
- package/models/qda-bupa.txt +23 -0
- package/models/qda-iris.txt +28 -0
- package/models/random-bupa.txt +4 -0
- package/models/random-car.txt +6 -0
- package/models/random-chess.txt +20 -0
- package/models/random-dermatology.txt +8 -0
- package/models/random-iris.txt +5 -0
- package/models/random-nursery.txt +7 -0
- package/models/random-tictactoe.txt +4 -0
- package/models/randomforest-bupa.txt +25666 -0
- package/models/randomforest-car.txt +78923 -0
- package/models/randomforest-dermatology.txt +7276 -0
- package/models/randomforest-iris.txt +3131 -0
- package/models/randomforest-tictactoe.txt +61186 -0
- package/package.json +3 -3
- package/source/Classifier/Bagging.ts +3 -0
- package/source/Classifier/C45.ts +4 -0
- package/source/Classifier/C45Stump.ts +4 -0
- package/source/Classifier/Classifier.ts +1 -0
- package/source/Classifier/DeepNetwork.ts +4 -0
- package/source/Classifier/Dummy.ts +4 -0
- package/source/Classifier/KMeans.ts +4 -0
- package/source/Classifier/Knn.ts +5 -1
- package/source/Classifier/Lda.ts +4 -0
- package/source/Classifier/LinearPerceptron.ts +4 -0
- package/source/Classifier/MultiLayerPerceptron.ts +4 -0
- package/source/Classifier/NaiveBayes.ts +4 -0
- package/source/Classifier/Qda.ts +4 -0
- package/source/Classifier/RandomClassifier.ts +4 -0
- package/source/Classifier/RandomForest.ts +4 -0
- package/source/DataSet/DataSet.ts +1 -1
- package/source/Model/DecisionTree/DecisionNode.ts +65 -29
- package/source/Model/DecisionTree/DecisionTree.ts +14 -4
- package/source/Model/DeepNetworkModel.ts +40 -16
- package/source/Model/DummyModel.ts +20 -2
- package/source/Model/GaussianModel.ts +29 -0
- package/source/Model/KMeansModel.ts +18 -6
- package/source/Model/KnnModel.ts +19 -6
- package/source/Model/LdaModel.ts +29 -6
- package/source/Model/LinearPerceptronModel.ts +52 -25
- package/source/Model/Model.ts +43 -0
- package/source/Model/MultiLayerPerceptronModel.ts +34 -12
- package/source/Model/NaiveBayesModel.ts +19 -7
- package/source/Model/NeuralNetworkModel.ts +31 -7
- package/source/Model/QdaModel.ts +22 -4
- package/source/Model/RandomModel.ts +21 -4
- package/source/Model/TreeEnsembleModel.ts +17 -3
- package/tests/Classifier/C45Test.ts +67 -0
- package/tests/Classifier/DeepNetworkTest.ts +52 -0
- package/tests/Classifier/DummyTest.ts +89 -0
- package/tests/Classifier/KMeansTest.ts +89 -0
- package/tests/Classifier/KnnTest.ts +68 -0
- package/tests/Classifier/LdaTest.ts +45 -0
- package/tests/Classifier/LinearPerceptronTest.ts +49 -0
- package/tests/Classifier/MultiLayerPerceptronTest.ts +51 -0
- package/tests/Classifier/NaiveBayesTest.ts +45 -0
- package/tests/Classifier/QdaTest.ts +35 -0
- package/tests/Classifier/RandomForestTest.ts +68 -0
package/source/Model/LdaModel.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {GaussianModel} from "./GaussianModel";
|
|
|
2
2
|
import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
3
3
|
import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
|
|
4
4
|
import {Instance} from "../Instance/Instance";
|
|
5
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
5
6
|
|
|
6
7
|
export class LdaModel extends GaussianModel{
|
|
7
8
|
|
|
@@ -11,15 +12,23 @@ export class LdaModel extends GaussianModel{
|
|
|
11
12
|
/**
|
|
12
13
|
* A constructor which sets the priorDistribution, w and w0 according to given inputs.
|
|
13
14
|
*
|
|
14
|
-
* @param
|
|
15
|
+
* @param priorDistributionOrFileName {@link DiscreteDistribution} input.
|
|
15
16
|
* @param w {@link HashMap} of String and Vectors.
|
|
16
17
|
* @param w0 {@link HashMap} of String and Double.
|
|
17
18
|
*/
|
|
18
|
-
constructor(
|
|
19
|
-
super()
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
constructor(priorDistributionOrFileName?: DiscreteDistribution | string, w?: Map<string, Vector>, w0?: Map<string, number>) {
|
|
20
|
+
super()
|
|
21
|
+
if (priorDistributionOrFileName instanceof DiscreteDistribution){
|
|
22
|
+
this.priorDistribution = priorDistributionOrFileName
|
|
23
|
+
this.w = w
|
|
24
|
+
this.w0 = w0
|
|
25
|
+
} else {
|
|
26
|
+
if (priorDistributionOrFileName != undefined){
|
|
27
|
+
let input = new FileContents(priorDistributionOrFileName)
|
|
28
|
+
let size = this.loadPriorDistribution(input)
|
|
29
|
+
this.loadWandW0(input, size)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
23
32
|
}
|
|
24
33
|
|
|
25
34
|
/**
|
|
@@ -36,4 +45,18 @@ export class LdaModel extends GaussianModel{
|
|
|
36
45
|
let w0i = this.w0.get(Ci);
|
|
37
46
|
return wi.dotProduct(xi) + w0i;
|
|
38
47
|
}
|
|
48
|
+
|
|
49
|
+
loadWandW0(input: FileContents, size: number){
|
|
50
|
+
this.w0 = new Map<string, number>()
|
|
51
|
+
for (let i = 0; i < size; i++){
|
|
52
|
+
let line = input.readLine()
|
|
53
|
+
let items = line.split(" ")
|
|
54
|
+
this.w0.set(items[0], parseFloat(items[1]))
|
|
55
|
+
}
|
|
56
|
+
this.w = this.loadVectors(input, size)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
saveTxt(fileName: string){
|
|
60
|
+
}
|
|
61
|
+
|
|
39
62
|
}
|
|
@@ -4,45 +4,69 @@ import {InstanceList} from "../InstanceList/InstanceList";
|
|
|
4
4
|
import {LinearPerceptronParameter} from "../Parameter/LinearPerceptronParameter";
|
|
5
5
|
import {ClassificationPerformance} from "../Performance/ClassificationPerformance";
|
|
6
6
|
import {Random} from "nlptoolkit-util/dist/Random";
|
|
7
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
7
8
|
|
|
8
9
|
export class LinearPerceptronModel extends NeuralNetworkModel{
|
|
9
10
|
|
|
10
11
|
protected W: Matrix
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
* Constructor that takes {@link InstanceList}s as trainsSet and validationSet. Initially it allocates layer weights,
|
|
14
|
-
* then creates an input vector by using given trainSet and finds error. Via the validationSet it finds the classification
|
|
15
|
-
* performance and at the end it reassigns the allocated weight Matrix with the matrix that has the best accuracy.
|
|
16
|
-
*
|
|
17
|
-
* @param trainSet InstanceList that is used to train.
|
|
18
|
-
* @param validationSet InstanceList that is used to validate.
|
|
19
|
-
* @param parameters Linear perceptron parameters; learningRate, etaDecrease, crossValidationRatio, epoch.
|
|
20
|
-
*/
|
|
21
|
-
constructor(trainSet: InstanceList, validationSet?: InstanceList, parameters?: LinearPerceptronParameter) {
|
|
22
|
-
super(trainSet);
|
|
13
|
+
constructor1(trainSet: InstanceList, validationSet?: InstanceList, parameters?: LinearPerceptronParameter){
|
|
23
14
|
if (validationSet != undefined){
|
|
24
|
-
|
|
25
|
-
let bestW = W.clone();
|
|
26
|
-
let bestClassificationPerformance = new ClassificationPerformance(0.0)
|
|
27
|
-
let epoch = parameters.getEpoch()
|
|
28
|
-
let learningRate = parameters.getLearningRate()
|
|
15
|
+
this.W = this.allocateLayerWeights(this.K, this.d + 1, new Random(parameters.getSeed()));
|
|
16
|
+
let bestW = this.W.clone();
|
|
17
|
+
let bestClassificationPerformance = new ClassificationPerformance(0.0)
|
|
18
|
+
let epoch = parameters.getEpoch()
|
|
19
|
+
let learningRate = parameters.getLearningRate()
|
|
29
20
|
for (let i = 0; i < epoch; i++) {
|
|
30
|
-
trainSet.shuffle(new Random(parameters.getSeed()))
|
|
21
|
+
trainSet.shuffle(new Random(parameters.getSeed()))
|
|
31
22
|
for (let j = 0; j < trainSet.size(); j++) {
|
|
32
|
-
this.createInputVector(trainSet.get(j))
|
|
33
|
-
let rMinusY = this.calculateRMinusY(trainSet.get(j), this.x, W)
|
|
23
|
+
this.createInputVector(trainSet.get(j))
|
|
24
|
+
let rMinusY = this.calculateRMinusY(trainSet.get(j), this.x, this.W)
|
|
34
25
|
let deltaW = new Matrix(rMinusY, this.x);
|
|
35
26
|
deltaW.multiplyWithConstant(learningRate);
|
|
36
|
-
W.add(deltaW)
|
|
27
|
+
this.W.add(deltaW)
|
|
37
28
|
}
|
|
38
|
-
let currentClassificationPerformance = this.testClassifier(validationSet)
|
|
29
|
+
let currentClassificationPerformance = this.testClassifier(validationSet)
|
|
39
30
|
if (currentClassificationPerformance.getAccuracy() > bestClassificationPerformance.getAccuracy()) {
|
|
40
|
-
bestClassificationPerformance = currentClassificationPerformance
|
|
41
|
-
bestW = W.clone()
|
|
31
|
+
bestClassificationPerformance = currentClassificationPerformance
|
|
32
|
+
bestW = this.W.clone()
|
|
42
33
|
}
|
|
43
|
-
learningRate *= parameters.getEtaDecrease()
|
|
34
|
+
learningRate *= parameters.getEtaDecrease()
|
|
35
|
+
}
|
|
36
|
+
this.W = bestW
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
constructor2(fileName: string){
|
|
41
|
+
let input = new FileContents(fileName)
|
|
42
|
+
this.loadClassLabels(input)
|
|
43
|
+
this.W = this.loadMatrix(input)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Constructor that takes {@link InstanceList}s as trainsSet and validationSet. Initially it allocates layer weights,
|
|
48
|
+
* then creates an input vector by using given trainSet and finds error. Via the validationSet it finds the classification
|
|
49
|
+
* performance and at the end it reassigns the allocated weight Matrix with the matrix that has the best accuracy.
|
|
50
|
+
*
|
|
51
|
+
* @param trainSetOrFileName InstanceList that is used to train.
|
|
52
|
+
* @param validationSet InstanceList that is used to validate.
|
|
53
|
+
* @param parameters Linear perceptron parameters; learningRate, etaDecrease, crossValidationRatio, epoch.
|
|
54
|
+
*/
|
|
55
|
+
constructor(trainSetOrFileName?: InstanceList | string, validationSet?: InstanceList, parameters?: LinearPerceptronParameter) {
|
|
56
|
+
if (trainSetOrFileName instanceof InstanceList){
|
|
57
|
+
if (validationSet != undefined){
|
|
58
|
+
super(trainSetOrFileName)
|
|
59
|
+
this.constructor1(trainSetOrFileName, validationSet, parameters)
|
|
60
|
+
} else {
|
|
61
|
+
super(trainSetOrFileName)
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
if (trainSetOrFileName != undefined){
|
|
65
|
+
super()
|
|
66
|
+
this.constructor2(trainSetOrFileName)
|
|
67
|
+
} else {
|
|
68
|
+
super()
|
|
44
69
|
}
|
|
45
|
-
W = bestW;
|
|
46
70
|
}
|
|
47
71
|
}
|
|
48
72
|
|
|
@@ -53,4 +77,7 @@ export class LinearPerceptronModel extends NeuralNetworkModel{
|
|
|
53
77
|
this.y = this.W.multiplyWithVectorFromRight(this.x);
|
|
54
78
|
}
|
|
55
79
|
|
|
80
|
+
saveTxt(fileName: string){
|
|
81
|
+
}
|
|
82
|
+
|
|
56
83
|
}
|
package/source/Model/Model.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import {Instance} from "../Instance/Instance";
|
|
2
2
|
import {CounterHashMap} from "nlptoolkit-datastructure/dist/CounterHashMap";
|
|
3
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
4
|
+
import {InstanceList} from "../InstanceList/InstanceList";
|
|
5
|
+
import {Matrix} from "nlptoolkit-math/dist/Matrix";
|
|
3
6
|
|
|
4
7
|
export abstract class Model {
|
|
5
8
|
|
|
6
9
|
abstract predict(instance: Instance): string
|
|
7
10
|
abstract predictProbability(instance: Instance): Map<string, number>
|
|
11
|
+
abstract saveTxt(fileName: String): void
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
14
|
* Given an array of class labels, returns the maximum occurred one.
|
|
@@ -19,4 +23,43 @@ export abstract class Model {
|
|
|
19
23
|
}
|
|
20
24
|
return frequencies.max();
|
|
21
25
|
}
|
|
26
|
+
|
|
27
|
+
loadInstance(line: string, attributeTypes: string[]): Instance{
|
|
28
|
+
let items = line.split(",")
|
|
29
|
+
let instance = new Instance(items[items.length - 1])
|
|
30
|
+
for (let i = 0; i < items.length - 1; i++){
|
|
31
|
+
switch (attributeTypes[i]){
|
|
32
|
+
case "DISCRETE":
|
|
33
|
+
instance.addAttribute(items[i])
|
|
34
|
+
break
|
|
35
|
+
case "CONTINUOUS":
|
|
36
|
+
instance.addAttribute(parseFloat(items[i]))
|
|
37
|
+
break
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return instance
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
loadInstanceList(input: FileContents): InstanceList{
|
|
44
|
+
let types = input.readLine().split(" ")
|
|
45
|
+
let instanceCount = parseInt(input.readLine())
|
|
46
|
+
let instanceList = new InstanceList()
|
|
47
|
+
for (let i = 0; i < instanceCount; i++){
|
|
48
|
+
instanceList.add(this.loadInstance(input.readLine(), types))
|
|
49
|
+
}
|
|
50
|
+
return instanceList;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
loadMatrix(input: FileContents): Matrix{
|
|
54
|
+
let items = input.readLine().split(" ")
|
|
55
|
+
let matrix = new Matrix(parseInt(items[0]), parseInt(items[1]))
|
|
56
|
+
for (let j = 0; j < matrix.getRow(); j++){
|
|
57
|
+
let line = input.readLine()
|
|
58
|
+
items = line.split(" ")
|
|
59
|
+
for (let k = 0; k < matrix.getColumn(); k++){
|
|
60
|
+
matrix.setValue(j, k, parseFloat(items[k]))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return matrix
|
|
64
|
+
}
|
|
22
65
|
}
|
|
@@ -6,6 +6,7 @@ import {MultiLayerPerceptronParameter} from "../Parameter/MultiLayerPerceptronPa
|
|
|
6
6
|
import {ClassificationPerformance} from "../Performance/ClassificationPerformance";
|
|
7
7
|
import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
8
8
|
import {Random} from "nlptoolkit-util/dist/Random";
|
|
9
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
9
10
|
|
|
10
11
|
export class MultiLayerPerceptronModel extends LinearPerceptronModel{
|
|
11
12
|
|
|
@@ -23,18 +24,7 @@ export class MultiLayerPerceptronModel extends LinearPerceptronModel{
|
|
|
23
24
|
this.V = this.allocateLayerWeights(this.K, H + 1, random);
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
* A constructor that takes {@link InstanceList}s as trainsSet and validationSet. It sets the {@link NeuralNetworkModel}
|
|
28
|
-
* nodes with given {@link InstanceList} then creates an input vector by using given trainSet and finds error.
|
|
29
|
-
* Via the validationSet it finds the classification performance and reassigns the allocated weight Matrix with the matrix
|
|
30
|
-
* that has the best accuracy and the Matrix V with the best Vector input.
|
|
31
|
-
*
|
|
32
|
-
* @param trainSet InstanceList that is used to train.
|
|
33
|
-
* @param validationSet InstanceList that is used to validate.
|
|
34
|
-
* @param parameters Multi layer perceptron parameters; seed, learningRate, etaDecrease, crossValidationRatio, epoch, hiddenNodes.
|
|
35
|
-
*/
|
|
36
|
-
constructor(trainSet: InstanceList, validationSet: InstanceList, parameters: MultiLayerPerceptronParameter) {
|
|
37
|
-
super(trainSet);
|
|
27
|
+
constructor1(trainSet: InstanceList, validationSet?: InstanceList, parameters?: MultiLayerPerceptronParameter){
|
|
38
28
|
this.activationFunction = parameters.getActivationFunction();
|
|
39
29
|
this.allocateWeights(parameters.getHiddenNodes(), new Random(parameters.getSeed()));
|
|
40
30
|
let bestW = this.W.clone();
|
|
@@ -88,10 +78,42 @@ export class MultiLayerPerceptronModel extends LinearPerceptronModel{
|
|
|
88
78
|
this.V = bestV;
|
|
89
79
|
}
|
|
90
80
|
|
|
81
|
+
constructor2(fileName: string){
|
|
82
|
+
let input = new FileContents(fileName)
|
|
83
|
+
this.activationFunction = this.loadActivationFunction(input)
|
|
84
|
+
this.loadClassLabels(input)
|
|
85
|
+
this.W = this.loadMatrix(input)
|
|
86
|
+
this.V = this.loadMatrix(input)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A constructor that takes {@link InstanceList}s as trainsSet and validationSet. It sets the {@link NeuralNetworkModel}
|
|
91
|
+
* nodes with given {@link InstanceList} then creates an input vector by using given trainSet and finds error.
|
|
92
|
+
* Via the validationSet it finds the classification performance and reassigns the allocated weight Matrix with the matrix
|
|
93
|
+
* that has the best accuracy and the Matrix V with the best Vector input.
|
|
94
|
+
*
|
|
95
|
+
* @param trainSetOrFileName InstanceList that is used to train.
|
|
96
|
+
* @param validationSet InstanceList that is used to validate.
|
|
97
|
+
* @param parameters Multi layer perceptron parameters; seed, learningRate, etaDecrease, crossValidationRatio, epoch, hiddenNodes.
|
|
98
|
+
*/
|
|
99
|
+
constructor(trainSetOrFileName: InstanceList | string, validationSet?: InstanceList, parameters?: MultiLayerPerceptronParameter) {
|
|
100
|
+
if (trainSetOrFileName instanceof InstanceList){
|
|
101
|
+
super(trainSetOrFileName)
|
|
102
|
+
this.constructor1(trainSetOrFileName, validationSet, parameters)
|
|
103
|
+
} else {
|
|
104
|
+
super()
|
|
105
|
+
this.constructor2(trainSetOrFileName)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
91
109
|
/**
|
|
92
110
|
* The calculateOutput method calculates the forward single hidden layer by using Matrices W and V.
|
|
93
111
|
*/
|
|
94
112
|
protected calculateOutput() {
|
|
95
113
|
this.calculateForwardSingleHiddenLayer(this.W, this.V, this.activationFunction);
|
|
96
114
|
}
|
|
115
|
+
|
|
116
|
+
saveTxt(fileName: string){
|
|
117
|
+
}
|
|
118
|
+
|
|
97
119
|
}
|
|
@@ -4,6 +4,7 @@ import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
|
4
4
|
import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
|
|
5
5
|
import {ContinuousAttribute} from "../Attribute/ContinuousAttribute";
|
|
6
6
|
import {DiscreteAttribute} from "../Attribute/DiscreteAttribute";
|
|
7
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
7
8
|
|
|
8
9
|
export class NaiveBayesModel extends GaussianModel{
|
|
9
10
|
|
|
@@ -18,14 +19,21 @@ export class NaiveBayesModel extends GaussianModel{
|
|
|
18
19
|
* @param classMeans A {@link Map} of String and {@link Vector}.
|
|
19
20
|
* @param classDeviations A {@link Map} of String and {@link Vector}.
|
|
20
21
|
*/
|
|
21
|
-
constructor(priorDistribution: DiscreteDistribution, classMeans
|
|
22
|
-
super()
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
constructor(priorDistribution: DiscreteDistribution | string, classMeans?: any, classDeviations?: Map<string, Vector>) {
|
|
23
|
+
super()
|
|
24
|
+
if (priorDistribution instanceof DiscreteDistribution){
|
|
25
|
+
this.priorDistribution = priorDistribution
|
|
26
|
+
if (classDeviations != undefined){
|
|
27
|
+
this.classMeans = classMeans
|
|
28
|
+
this.classDeviations = classDeviations
|
|
29
|
+
} else {
|
|
30
|
+
this.classAttributeDistributions = classMeans
|
|
31
|
+
}
|
|
27
32
|
} else {
|
|
28
|
-
|
|
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)
|
|
29
37
|
}
|
|
30
38
|
}
|
|
31
39
|
|
|
@@ -86,4 +94,8 @@ export class NaiveBayesModel extends GaussianModel{
|
|
|
86
94
|
}
|
|
87
95
|
return logLikelihood;
|
|
88
96
|
}
|
|
97
|
+
|
|
98
|
+
saveTxt(fileName: string){
|
|
99
|
+
}
|
|
100
|
+
|
|
89
101
|
}
|
|
@@ -6,6 +6,7 @@ import {Instance} from "../Instance/Instance";
|
|
|
6
6
|
import {ActivationFunction} from "../Parameter/ActivationFunction";
|
|
7
7
|
import {CompositeInstance} from "../Instance/CompositeInstance";
|
|
8
8
|
import {Random} from "nlptoolkit-util/dist/Random";
|
|
9
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
9
10
|
|
|
10
11
|
export abstract class NeuralNetworkModel extends ValidatedModel{
|
|
11
12
|
|
|
@@ -23,11 +24,13 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
|
|
|
23
24
|
*
|
|
24
25
|
* @param trainSet {@link InstanceList} to use as train set.
|
|
25
26
|
*/
|
|
26
|
-
protected constructor(trainSet
|
|
27
|
+
protected constructor(trainSet?: InstanceList) {
|
|
27
28
|
super();
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if (trainSet != undefined){
|
|
30
|
+
this.classLabels = trainSet.getDistinctClassLabels()
|
|
31
|
+
this.K = this.classLabels.length
|
|
32
|
+
this.d = trainSet.get(0).continuousAttributeSize()
|
|
33
|
+
}
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
/**
|
|
@@ -52,10 +55,10 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
|
|
|
52
55
|
protected normalizeOutput(o: Vector): Vector{
|
|
53
56
|
let sum = 0.0;
|
|
54
57
|
let values = new Array<number>()
|
|
55
|
-
for (let i = 0; i <
|
|
58
|
+
for (let i = 0; i < o.size(); i++){
|
|
56
59
|
sum += Math.exp(o.getValue(i));
|
|
57
60
|
}
|
|
58
|
-
for (let i = 0; i <
|
|
61
|
+
for (let i = 0; i < o.size(); i++){
|
|
59
62
|
values.push(Math.exp(o.getValue(i)) / sum);
|
|
60
63
|
}
|
|
61
64
|
return new Vector(values);
|
|
@@ -135,7 +138,7 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
|
|
|
135
138
|
* @return Difference between newly created Vector and normalized output.
|
|
136
139
|
*/
|
|
137
140
|
protected calculateRMinusY(instance: Instance, input: Vector, weights: Matrix): Vector{
|
|
138
|
-
this.r = new Vector(this.K, this.classLabels.indexOf(instance.getClassLabel())
|
|
141
|
+
this.r = new Vector(this.K, 1.0, this.classLabels.indexOf(instance.getClassLabel()));
|
|
139
142
|
let o = weights.multiplyWithVectorFromRight(input);
|
|
140
143
|
this.y = this.normalizeOutput(o);
|
|
141
144
|
return this.r.difference(this.y);
|
|
@@ -186,4 +189,25 @@ export abstract class NeuralNetworkModel extends ValidatedModel{
|
|
|
186
189
|
}
|
|
187
190
|
return result;
|
|
188
191
|
}
|
|
192
|
+
|
|
193
|
+
loadClassLabels(input: FileContents){
|
|
194
|
+
let items = input.readLine().split(" ")
|
|
195
|
+
this.K = parseInt(items[0])
|
|
196
|
+
this.d = parseInt(items[1])
|
|
197
|
+
this.classLabels = new Array<string>()
|
|
198
|
+
for (let i = 0; i < this.K; i++){
|
|
199
|
+
this.classLabels.push(input.readLine())
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
loadActivationFunction(input: FileContents): ActivationFunction{
|
|
204
|
+
switch (input.readLine()){
|
|
205
|
+
case "TANH":
|
|
206
|
+
return ActivationFunction.TANH
|
|
207
|
+
case "RELU":
|
|
208
|
+
return ActivationFunction.RELU
|
|
209
|
+
default:
|
|
210
|
+
return ActivationFunction.SIGMOID
|
|
211
|
+
}
|
|
212
|
+
}
|
|
189
213
|
}
|
package/source/Model/QdaModel.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {Matrix} from "nlptoolkit-math/dist/Matrix";
|
|
|
3
3
|
import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
|
|
4
4
|
import {Vector} from "nlptoolkit-math/dist/Vector";
|
|
5
5
|
import {Instance} from "../Instance/Instance";
|
|
6
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
6
7
|
|
|
7
8
|
export class QdaModel extends LdaModel{
|
|
8
9
|
|
|
@@ -11,14 +12,27 @@ export class QdaModel extends LdaModel{
|
|
|
11
12
|
/**
|
|
12
13
|
* The constructor which sets the priorDistribution, w w1 and HashMap of String Matrix.
|
|
13
14
|
*
|
|
14
|
-
* @param
|
|
15
|
+
* @param priorDistributionOrFileName {@link DiscreteDistribution} input.
|
|
15
16
|
* @param W {@link HashMap} of String and Matrix.
|
|
16
17
|
* @param w {@link HashMap} of String and Vectors.
|
|
17
18
|
* @param w0 {@link HashMap} of String and Double.
|
|
18
19
|
*/
|
|
19
|
-
constructor(
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
constructor(priorDistributionOrFileName: DiscreteDistribution | string, W?: Map<string, Matrix>, w?: Map<string, Vector>, w0?: Map<string, number>) {
|
|
21
|
+
if (priorDistributionOrFileName instanceof DiscreteDistribution){
|
|
22
|
+
super(priorDistributionOrFileName, w, w0);
|
|
23
|
+
this.W = W
|
|
24
|
+
} else {
|
|
25
|
+
super()
|
|
26
|
+
let input = new FileContents(priorDistributionOrFileName)
|
|
27
|
+
let size = this.loadPriorDistribution(input)
|
|
28
|
+
this.loadWandW0(input, size)
|
|
29
|
+
this.W = new Map<string, Matrix>()
|
|
30
|
+
for (let i = 0; i < size; i++){
|
|
31
|
+
let c = input.readLine()
|
|
32
|
+
let matrix = this.loadMatrix(input)
|
|
33
|
+
this.W.set(c, matrix)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
22
36
|
}
|
|
23
37
|
|
|
24
38
|
/**
|
|
@@ -36,4 +50,8 @@ export class QdaModel extends LdaModel{
|
|
|
36
50
|
let w0i = this.w0.get(Ci);
|
|
37
51
|
return Wi.multiplyWithVectorFromLeft(xi).dotProduct(xi) + wi.dotProduct(xi) + w0i;
|
|
38
52
|
}
|
|
53
|
+
|
|
54
|
+
saveTxt(fileName: string){
|
|
55
|
+
}
|
|
56
|
+
|
|
39
57
|
}
|
|
@@ -2,16 +2,30 @@ import {Model} from "./Model";
|
|
|
2
2
|
import {Instance} from "../Instance/Instance";
|
|
3
3
|
import {CompositeInstance} from "../Instance/CompositeInstance";
|
|
4
4
|
import {Random} from "nlptoolkit-util/dist/Random";
|
|
5
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
5
6
|
|
|
6
7
|
export class RandomModel extends Model{
|
|
7
8
|
|
|
8
|
-
private classLabels: Array<string>
|
|
9
|
+
private readonly classLabels: Array<string>
|
|
9
10
|
private random: Random
|
|
11
|
+
private seed: number
|
|
10
12
|
|
|
11
|
-
constructor(
|
|
13
|
+
constructor(classLabelsOrFileName: Array<string> | string, seed?: number) {
|
|
12
14
|
super();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
if (classLabelsOrFileName instanceof Array){
|
|
16
|
+
this.classLabels = classLabelsOrFileName
|
|
17
|
+
this.random = new Random(seed)
|
|
18
|
+
this.seed = seed
|
|
19
|
+
} else {
|
|
20
|
+
let input = new FileContents(classLabelsOrFileName)
|
|
21
|
+
seed = parseInt(input.readLine())
|
|
22
|
+
this.random = new Random(seed)
|
|
23
|
+
let size = parseInt(input.readLine())
|
|
24
|
+
this.classLabels = new Array<string>()
|
|
25
|
+
for (let i = 0; i < size; i++){
|
|
26
|
+
this.classLabels.push(input.readLine())
|
|
27
|
+
}
|
|
28
|
+
}
|
|
15
29
|
}
|
|
16
30
|
|
|
17
31
|
/**
|
|
@@ -42,4 +56,7 @@ export class RandomModel extends Model{
|
|
|
42
56
|
return result;
|
|
43
57
|
}
|
|
44
58
|
|
|
59
|
+
saveTxt(fileName: string){
|
|
60
|
+
}
|
|
61
|
+
|
|
45
62
|
}
|
|
@@ -2,6 +2,8 @@ import {Model} from "./Model";
|
|
|
2
2
|
import {Instance} from "../Instance/Instance";
|
|
3
3
|
import {DecisionTree} from "./DecisionTree/DecisionTree";
|
|
4
4
|
import {DiscreteDistribution} from "nlptoolkit-math/dist/DiscreteDistribution";
|
|
5
|
+
import {FileContents} from "nlptoolkit-util/dist/FileContents";
|
|
6
|
+
import {DecisionNode} from "./DecisionTree/DecisionNode";
|
|
5
7
|
|
|
6
8
|
export class TreeEnsembleModel extends Model{
|
|
7
9
|
|
|
@@ -10,11 +12,20 @@ export class TreeEnsembleModel extends Model{
|
|
|
10
12
|
/**
|
|
11
13
|
* A constructor which sets the {@link Array} of {@link DecisionTree} with given input.
|
|
12
14
|
*
|
|
13
|
-
* @param
|
|
15
|
+
* @param forestOrFileName An {@link Array} of {@link DecisionTree}.
|
|
14
16
|
*/
|
|
15
|
-
constructor(
|
|
17
|
+
constructor(forestOrFileName: Array<DecisionTree> | string) {
|
|
16
18
|
super();
|
|
17
|
-
|
|
19
|
+
if (forestOrFileName instanceof Array){
|
|
20
|
+
this.forest = forestOrFileName
|
|
21
|
+
} else {
|
|
22
|
+
let input = new FileContents(forestOrFileName)
|
|
23
|
+
let numberOfTrees = parseInt(input.readLine())
|
|
24
|
+
this.forest = new Array<DecisionTree>()
|
|
25
|
+
for (let i = 0; i < numberOfTrees; i++){
|
|
26
|
+
this.forest.push(new DecisionTree(new DecisionNode(input)))
|
|
27
|
+
}
|
|
28
|
+
}
|
|
18
29
|
}
|
|
19
30
|
|
|
20
31
|
/**
|
|
@@ -40,4 +51,7 @@ export class TreeEnsembleModel extends Model{
|
|
|
40
51
|
return distribution.getProbabilityDistribution();
|
|
41
52
|
}
|
|
42
53
|
|
|
54
|
+
saveTxt(fileName: string){
|
|
55
|
+
}
|
|
56
|
+
|
|
43
57
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as assert from "assert";
|
|
2
|
+
import {C45} from "../../dist/Classifier/C45";
|
|
3
|
+
import {C45Parameter} from "../../dist/Parameter/C45Parameter";
|
|
4
|
+
import {AttributeType} from "../../dist/Attribute/AttributeType";
|
|
5
|
+
import {DataDefinition} from "../../dist/DataSet/DataDefinition";
|
|
6
|
+
import {DataSet} from "../../dist/DataSet/DataSet";
|
|
7
|
+
|
|
8
|
+
describe('C45Test', function() {
|
|
9
|
+
describe('C45Test', function() {
|
|
10
|
+
let c45 = new C45()
|
|
11
|
+
let c45Parameter = new C45Parameter(1, true, 0.2)
|
|
12
|
+
let attributeTypes = new Array<AttributeType>();
|
|
13
|
+
for (let i = 0; i < 4; i++){
|
|
14
|
+
attributeTypes.push(AttributeType.CONTINUOUS)
|
|
15
|
+
}
|
|
16
|
+
let dataDefinition = new DataDefinition(attributeTypes)
|
|
17
|
+
let iris = new DataSet(dataDefinition, ",", "datasets/iris.data")
|
|
18
|
+
attributeTypes = new Array<AttributeType>();
|
|
19
|
+
for (let i = 0; i < 6; i++){
|
|
20
|
+
attributeTypes.push(AttributeType.CONTINUOUS)
|
|
21
|
+
}
|
|
22
|
+
dataDefinition = new DataDefinition(attributeTypes)
|
|
23
|
+
let bupa = new DataSet(dataDefinition, ",", "datasets/bupa.data")
|
|
24
|
+
attributeTypes = new Array<AttributeType>()
|
|
25
|
+
for (let i = 0; i < 34; i++){
|
|
26
|
+
attributeTypes.push(AttributeType.CONTINUOUS)
|
|
27
|
+
}
|
|
28
|
+
dataDefinition = new DataDefinition(attributeTypes)
|
|
29
|
+
let dermatology = new DataSet(dataDefinition, ",", "datasets/dermatology.data")
|
|
30
|
+
attributeTypes = new Array<AttributeType>()
|
|
31
|
+
for (let i = 0; i < 6; i++){
|
|
32
|
+
attributeTypes.push(AttributeType.DISCRETE)
|
|
33
|
+
}
|
|
34
|
+
dataDefinition = new DataDefinition(attributeTypes);
|
|
35
|
+
let car = new DataSet(dataDefinition, ",", "datasets/car.data");
|
|
36
|
+
attributeTypes = new Array<AttributeType>()
|
|
37
|
+
for (let i = 0; i < 9; i++){
|
|
38
|
+
attributeTypes.push(AttributeType.DISCRETE)
|
|
39
|
+
}
|
|
40
|
+
dataDefinition = new DataDefinition(attributeTypes);
|
|
41
|
+
let tictactoe = new DataSet(dataDefinition, ",", "datasets/tictactoe.data")
|
|
42
|
+
it('testTrain', function() {
|
|
43
|
+
c45.train(iris.getInstanceList(), c45Parameter);
|
|
44
|
+
assert.ok(Math.abs(4.00 - 100 * c45.test(iris.getInstanceList()).getErrorRate()) <= 0.01);
|
|
45
|
+
c45.train(bupa.getInstanceList(), c45Parameter);
|
|
46
|
+
assert.ok(Math.abs(28.41 - 100 * c45.test(bupa.getInstanceList()).getErrorRate()) <= 0.01);
|
|
47
|
+
c45.train(dermatology.getInstanceList(), c45Parameter);
|
|
48
|
+
assert.ok(Math.abs(2.46 - 100 * c45.test(dermatology.getInstanceList()).getErrorRate()) <= 0.01);
|
|
49
|
+
c45.train(car.getInstanceList(), c45Parameter);
|
|
50
|
+
assert.ok(Math.abs(21.35 - 100 * c45.test(car.getInstanceList()).getErrorRate()) <= 0.01);
|
|
51
|
+
c45.train(tictactoe.getInstanceList(), c45Parameter);
|
|
52
|
+
assert.ok(Math.abs(19.94 - 100 * c45.test(tictactoe.getInstanceList()).getErrorRate()) <= 0.01);
|
|
53
|
+
});
|
|
54
|
+
it('testLoad', function() {
|
|
55
|
+
c45.loadModel("models/c45-iris.txt");
|
|
56
|
+
assert.ok(Math.abs(4.00 - 100 * c45.test(iris.getInstanceList()).getErrorRate()) <= 0.01);
|
|
57
|
+
c45.loadModel("models/c45-bupa.txt");
|
|
58
|
+
assert.ok(Math.abs(42.03 - 100 * c45.test(bupa.getInstanceList()).getErrorRate()) <= 0.01);
|
|
59
|
+
c45.loadModel("models/c45-dermatology.txt");
|
|
60
|
+
assert.ok(Math.abs(2.19 - 100 * c45.test(dermatology.getInstanceList()).getErrorRate()) <= 0.01);
|
|
61
|
+
c45.loadModel("models/c45-car.txt");
|
|
62
|
+
assert.ok(Math.abs(8.16 - 100 * c45.test(car.getInstanceList()).getErrorRate()) <= 0.01);
|
|
63
|
+
c45.loadModel("models/c45-tictactoe.txt");
|
|
64
|
+
assert.ok(Math.abs(14.61 - 100 * c45.test(tictactoe.getInstanceList()).getErrorRate()) <= 0.01);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as assert from "assert";
|
|
2
|
+
import {AttributeType} from "../../dist/Attribute/AttributeType";
|
|
3
|
+
import {DataDefinition} from "../../dist/DataSet/DataDefinition";
|
|
4
|
+
import {DataSet} from "../../dist/DataSet/DataSet";
|
|
5
|
+
import {MultiLayerPerceptron} from "../../dist/Classifier/MultiLayerPerceptron";
|
|
6
|
+
import {MultiLayerPerceptronParameter} from "../../dist/Parameter/MultiLayerPerceptronParameter";
|
|
7
|
+
import {ActivationFunction} from "../../dist/Parameter/ActivationFunction";
|
|
8
|
+
import {DeepNetwork} from "../../dist/Classifier/DeepNetwork";
|
|
9
|
+
import {DeepNetworkParameter} from "../../dist/Parameter/DeepNetworkParameter";
|
|
10
|
+
|
|
11
|
+
describe('DeepNetworkTest', function() {
|
|
12
|
+
describe('DeepNetworkTest', function() {
|
|
13
|
+
let deepNetwork = new DeepNetwork()
|
|
14
|
+
let attributeTypes = new Array<AttributeType>();
|
|
15
|
+
for (let i = 0; i < 4; i++){
|
|
16
|
+
attributeTypes.push(AttributeType.CONTINUOUS)
|
|
17
|
+
}
|
|
18
|
+
let dataDefinition = new DataDefinition(attributeTypes)
|
|
19
|
+
let iris = new DataSet(dataDefinition, ",", "datasets/iris.data")
|
|
20
|
+
attributeTypes = new Array<AttributeType>();
|
|
21
|
+
for (let i = 0; i < 6; i++){
|
|
22
|
+
attributeTypes.push(AttributeType.CONTINUOUS)
|
|
23
|
+
}
|
|
24
|
+
dataDefinition = new DataDefinition(attributeTypes)
|
|
25
|
+
let bupa = new DataSet(dataDefinition, ",", "datasets/bupa.data")
|
|
26
|
+
attributeTypes = new Array<AttributeType>()
|
|
27
|
+
for (let i = 0; i < 34; i++){
|
|
28
|
+
attributeTypes.push(AttributeType.CONTINUOUS)
|
|
29
|
+
}
|
|
30
|
+
dataDefinition = new DataDefinition(attributeTypes)
|
|
31
|
+
let dermatology = new DataSet(dataDefinition, ",", "datasets/dermatology.data")
|
|
32
|
+
it('testTrain', function() {
|
|
33
|
+
let deepNetworkParameter = new DeepNetworkParameter(1, 0.1, 0.99, 0.2, 100, [5, 5], ActivationFunction.SIGMOID)
|
|
34
|
+
deepNetwork.train(iris.getInstanceList(), deepNetworkParameter);
|
|
35
|
+
assert.ok(Math.abs(2.67 - 100 * deepNetwork.test(iris.getInstanceList()).getErrorRate()) <= 0.01);
|
|
36
|
+
deepNetworkParameter = new DeepNetworkParameter(1, 0.01, 0.99, 0.2, 100, [15, 15], ActivationFunction.SIGMOID)
|
|
37
|
+
deepNetwork.train(bupa.getInstanceList(), deepNetworkParameter);
|
|
38
|
+
assert.ok(Math.abs(28.70 - 100 * deepNetwork.test(bupa.getInstanceList()).getErrorRate()) <= 0.01);
|
|
39
|
+
deepNetworkParameter = new DeepNetworkParameter(1, 0.01, 0.99, 0.2, 100, [20], ActivationFunction.SIGMOID)
|
|
40
|
+
deepNetwork.train(dermatology.getInstanceList(), deepNetworkParameter);
|
|
41
|
+
assert.ok(Math.abs(2.73 - 100 * deepNetwork.test(dermatology.getInstanceList()).getErrorRate()) <= 0.01);
|
|
42
|
+
});
|
|
43
|
+
it('testLoad', function() {
|
|
44
|
+
deepNetwork.loadModel("models/deepNetwork-iris.txt");
|
|
45
|
+
assert.ok(Math.abs(1.33 - 100 * deepNetwork.test(iris.getInstanceList()).getErrorRate()) <= 0.01);
|
|
46
|
+
deepNetwork.loadModel("models/deepNetwork-bupa.txt");
|
|
47
|
+
assert.ok(Math.abs(28.99 - 100 * deepNetwork.test(bupa.getInstanceList()).getErrorRate()) <= 0.01);
|
|
48
|
+
deepNetwork.loadModel("models/deepNetwork-dermatology.txt");
|
|
49
|
+
assert.ok(Math.abs(1.09 - 100 * deepNetwork.test(dermatology.getInstanceList()).getErrorRate()) <= 0.01);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
});
|