* * @param data * @returns { * { * error: number, * misclasses: Array * } * }
(data)
| 738 | * } |
| 739 | */ |
| 740 | test(data) { |
| 741 | data = this._formatData(data); |
| 742 | |
| 743 | // for binary classification problems with one output node |
| 744 | let isBinary = data[0].output.length === 1; |
| 745 | let falsePos = 0; |
| 746 | let falseNeg = 0; |
| 747 | let truePos = 0; |
| 748 | let trueNeg = 0; |
| 749 | |
| 750 | // for classification problems |
| 751 | let misclasses = []; |
| 752 | |
| 753 | // run each pattern through the trained network and collect |
| 754 | // error and misclassification statistics |
| 755 | let sum = 0; |
| 756 | for (let i = 0; i < data.length; i++) { |
| 757 | let output = this.runInput(data[i].input); |
| 758 | let target = data[i].output; |
| 759 | |
| 760 | let actual, expected; |
| 761 | if (isBinary) { |
| 762 | actual = output[0] > this.binaryThresh ? 1 : 0; |
| 763 | expected = target[0]; |
| 764 | } |
| 765 | else { |
| 766 | actual = output.indexOf(max(output)); |
| 767 | expected = target.indexOf(max(target)); |
| 768 | } |
| 769 | |
| 770 | if (actual !== expected) { |
| 771 | let misclass = data[i]; |
| 772 | Object.assign(misclass, { |
| 773 | actual: actual, |
| 774 | expected: expected |
| 775 | }); |
| 776 | misclasses.push(misclass); |
| 777 | } |
| 778 | |
| 779 | if (isBinary) { |
| 780 | if (actual === 0 && expected === 0) { |
| 781 | trueNeg++; |
| 782 | } else if (actual === 1 && expected === 1) { |
| 783 | truePos++; |
| 784 | } else if (actual === 0 && expected === 1) { |
| 785 | falseNeg++; |
| 786 | } else if (actual === 1 && expected === 0) { |
| 787 | falsePos++; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | let errors = output.map((value, i) => { |
| 792 | return target[i] - value; |
| 793 | }); |
| 794 | sum += mse(errors); |
| 795 | } |
| 796 | let error = sum / data.length; |
| 797 |
no test coverage detected