From e7d9f70d838f748b2b89c6d63ec7294394bee557 Mon Sep 17 00:00:00 2001 From: Ricardo Rocha Date: Fri, 26 Mar 2021 23:21:09 +0000 Subject: [PATCH] Update version --- badges/badge-branches.svg | 2 +- package-lock.json | 4 ++-- package.json | 4 ++-- versions/1.4.0/index.js | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 versions/1.4.0/index.js diff --git a/badges/badge-branches.svg b/badges/badge-branches.svg index 589a37a..519dbea 100644 --- a/badges/badge-branches.svg +++ b/badges/badge-branches.svg @@ -1 +1 @@ -Coverage:branches: 92.95%Coverage:branches92.95% \ No newline at end of file +Coverage:branches: 93.31%Coverage:branches93.31% \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a53cbca..4dcb1f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fullexpression/confusion-matrix-stats", - "version": "1.4.0", + "version": "1.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -8136,4 +8136,4 @@ "dev": true } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index b725964..844e485 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fullexpression/confusion-matrix-stats", - "version": "1.4.0", + "version": "1.4.1", "description": "Calculate various statistics from a confusion matrixCalculates statistical value from a confusion matrix.", "main": "index.js", "types": "index.d.ts", @@ -38,4 +38,4 @@ "webpack-cli": "^4.5.0" }, "dependencies": {} -} \ No newline at end of file +} diff --git a/versions/1.4.0/index.js b/versions/1.4.0/index.js new file mode 100644 index 0000000..31f8ffc --- /dev/null +++ b/versions/1.4.0/index.js @@ -0,0 +1 @@ +!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var i=e();for(var s in i)("object"==typeof exports?exports:t)[s]=i[s]}}(self,(function(){return(()=>{"use strict";var t={975:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.AverageMethod=e.ConfusionMatrix=void 0;class i{constructor(t,e){this._labels=new Array,this._matrix=new Array,this.normalizations=new Array,this.history=new Array,this.historyPointer=-1,this.lastHistoryEvent=null,t&&(this._labels=this.deepCopy(t.labels),this._matrix=this.deepCopy(t.matrix),this.addToHistory()),e||this.validate()}set labels(t){this._labels=t,this.addToHistory()}get labels(){return this._labels}set matrix(t){this.addToHistory(),this._matrix=t}get matrix(){return this._matrix}setConfusionMatrix(t,e=!0){return t&&(this._labels=this.deepCopy(t._labels),this._matrix=this.deepCopy(t._matrix),e&&this.addToHistory()),this.validate(),this}normalize(t=0,e=1,s){if(t>=e)throw new Error("Min value cannot be equal or greater than max value.");this.validate();const r=this.getMinAndMax();if(r){this.addToHistory(),this.normalizations.push(new i(this));const a=r.min,l=r.max;for(let i=0;i0?this.labelAccuracy(t.label):this.matrixAccuracy(t.average)}labelAccuracy(t){this.validate();const{truePositive:e,trueNegative:i,falsePositive:s,falseNegative:r}=this.getConfusionMatrixClasses(t);return(e+i)/(e+i+s+r)||0}matrixAccuracy(t=s.Weighted){switch(this.validate(),t){case s.Micro:return this.microAccuracy();case s.Macro:return this.macroAccuracy();case s.Weighted:return this.weightedAccuracy()}}microAccuracy(){const{truePositive:t,trueNegative:e,falsePositive:i,falseNegative:s}=this.getSumConfusionMatrixClasses();return(t+e)/(t+e+i+s)||0}macroAccuracy(){let t=0;return this._labels.forEach((e=>t+=this.labelAccuracy(e))),t/this._labels.length||0}weightedAccuracy(){const t=this.getLabelsPredictionsSum(),e=this.getNumberOfPredictions();let i=0;return this._labels.forEach(((e,s)=>i+=this.labelAccuracy(e)*t[s])),i/e||0}missClassificationRate(t={average:s.Weighted}){return this.validate(),(null==t?void 0:t.label)&&(null==t?void 0:t.label.length)>0?this.labelMissClassificationRate(t.label):this.matrixMissClassificationRate(null==t?void 0:t.average)}labelMissClassificationRate(t){this.validate();const{truePositive:e,trueNegative:i,falsePositive:s,falseNegative:r}=this.getConfusionMatrixClasses(t);return(s+r)/(e+i+s+r)||0}matrixMissClassificationRate(t=s.Weighted){switch(this.validate(),t){case s.Micro:return this.microMissClassificationRate();case s.Macro:return this.macroMissClassificationRate();case s.Weighted:return this.weightedMissClassificationRate()}}microMissClassificationRate(){const{truePositive:t,trueNegative:e,falsePositive:i,falseNegative:s}=this.getSumConfusionMatrixClasses();return(i+s)/(t+e+i+s)||0}macroMissClassificationRate(){let t=0;return this._labels.forEach((e=>t+=this.labelMissClassificationRate(e))),t/this._labels.length||0}weightedMissClassificationRate(){const t=this.getLabelsPredictionsSum(),e=this.getNumberOfPredictions();let i=0;return this._labels.forEach(((e,s)=>i+=this.labelMissClassificationRate(e)*t[s])),i/e||0}precision(t={average:s.Weighted}){return this.validate(),(null==t?void 0:t.label)&&(null==t?void 0:t.label.length)>0?this.labelPrecision(t.label):this.matrixPrecision(null==t?void 0:t.average)}labelPrecision(t){this.validate();const{truePositive:e,falsePositive:i}=this.getConfusionMatrixClasses(t);return e/(e+i)||0}matrixPrecision(t=s.Weighted){switch(this.validate(),t){case s.Micro:return this.microPrecision();case s.Macro:return this.macroPrecision();case s.Weighted:return this.weightedPrecision()}}microPrecision(){const{truePositive:t,falsePositive:e}=this.getSumConfusionMatrixClasses();return t/(t+e)||0}macroPrecision(){let t=0;return this._labels.forEach((e=>t+=this.labelPrecision(e))),t/this._labels.length||0}weightedPrecision(){const t=this.getLabelsPredictionsSum(),e=this.getNumberOfPredictions();let i=0;return this._labels.forEach(((e,s)=>i+=this.labelPrecision(e)*t[s])),i/e||0}recall(t={average:s.Weighted}){return this.validate(),(null==t?void 0:t.label)&&(null==t?void 0:t.label.length)>0?this.labelRecall(t.label):this.matrixRecall(null==t?void 0:t.average)}labelRecall(t){this.validate();const{truePositive:e,falseNegative:i}=this.getConfusionMatrixClasses(t);return e/(e+i)||0}matrixRecall(t=s.Weighted){switch(this.validate(),t){case s.Micro:return this.microRecall();case s.Macro:return this.macroRecall();case s.Weighted:return this.weightedRecall()}}microRecall(){const{truePositive:t,falseNegative:e}=this.getSumConfusionMatrixClasses();return t/(t+e)||0}macroRecall(){let t=0;return this._labels.forEach((e=>t+=this.labelRecall(e))),t/this._labels.length||0}weightedRecall(){const t=this.getLabelsPredictionsSum(),e=this.getNumberOfPredictions();let i=0;return this._labels.forEach(((e,s)=>i+=this.labelRecall(e)*t[s])),i/e||0}specificity(t={average:s.Weighted}){return this.validate(),(null==t?void 0:t.label)&&(null==t?void 0:t.label.length)>0?this.labelSpecificity(t.label):this.matrixSpecificity(null==t?void 0:t.average)}matrixSpecificity(t=s.Weighted){switch(this.validate(),t){case s.Micro:return this.microSpecificity();case s.Macro:return this.macroSpecificity();case s.Weighted:return this.weightedSpecificity()}}microSpecificity(){const{trueNegative:t,falsePositive:e}=this.getSumConfusionMatrixClasses();return t/(t+e)||0}macroSpecificity(){let t=0;return this._labels.forEach((e=>t+=this.labelSpecificity(e))),t/this._labels.length||0}weightedSpecificity(){const t=this.getLabelsPredictionsSum(),e=this.getNumberOfPredictions();let i=0;return this._labels.forEach(((e,s)=>i+=this.labelSpecificity(e)*t[s])),i/e||0}labelSpecificity(t){this.validate();const{trueNegative:e,falsePositive:i}=this.getConfusionMatrixClasses(t);return e/(e+i)||0}f1Score(t){return this.validate(),(null==t?void 0:t.label)&&(null==t?void 0:t.label.length)>0?this.labelF1Score(t.label):this.matrixF1Score(null==t?void 0:t.average)}matrixF1Score(t=s.Weighted){switch(this.validate(),t){case s.Micro:return this.microF1Score();case s.Macro:return this.macroF1Score();case s.Weighted:return this.weightedF1Score()}}labelF1Score(t){this.validate();const e=this.precision({label:t}),i=this.recall({label:t});return e*i/(e+i)*2||0}microF1Score(){const t=this.microPrecision(),e=this.microRecall();return this.applyF1ScoreFormula(t,e)}macroF1Score(){let t=0;return this._labels.forEach((e=>t+=this.labelF1Score(e))),t/this._labels.length||0}weightedF1Score(){const t=this.getLabelsPredictionsSum(),e=this.getNumberOfPredictions();let i=0;return this._labels.forEach(((e,s)=>i+=this.labelF1Score(e)*t[s])),i/e||0}getAllMatrixClasses(){this.validate();const t=new Array;return this._labels.forEach((e=>t.push({label:e,confusionMatrixClasses:this.getConfusionMatrixClasses(e)}))),t}getSumConfusionMatrixClasses(){const t={truePositive:0,trueNegative:0,falsePositive:0,falseNegative:0};return this.getAllMatrixClasses().forEach((e=>{t.truePositive+=e.confusionMatrixClasses.truePositive,t.trueNegative+=e.confusionMatrixClasses.trueNegative,t.falsePositive+=e.confusionMatrixClasses.falsePositive,t.falseNegative+=e.confusionMatrixClasses.falseNegative})),t}getConfusionMatrixClasses(t){if(this.validate(),!t)throw new Error("A valid label should be passed.");const e=this._labels.findIndex((e=>e===t));if(-1==e)throw new Error("The label does not exists in the matrix.");const i=this.getNumberOfPredictions(),s=this._matrix[e][e],r=this._matrix[e].reduce(((t,e)=>t+e))-s;let a=0;for(let t=0;ts?s:t;return{min:t,max:e}}revertAllNormalizations(){return this.normalizations&&this.normalizations.length>0?this.setConfusionMatrix(this.normalizations[0]):this}getNumberOfPredictions(t){const e=this.getLabelsPredictionsSum();return t&&t.length>0?e[this._labels.findIndex((e=>e===t))]:e.reduce(((t,e)=>t+e))}getLabelsPredictionsSum(){let t=new Array(this._labels.length).fill(0,0,this._labels.length);return this._matrix.forEach((e=>e.forEach(((e,i)=>{t[i]+=e})))),t}validate(t=this){if(t._labels.length!==t._matrix.length)throw new Error("The labels length should be equals to the matrix columns length.");for(let e=0;e{if(e.length!==t._matrix.length)throw new Error("The confusion matrix does not have the columns/rows length.")})),t}transpose(){return this.addToHistory(),this._matrix=this._matrix[0].map(((t,e)=>this._matrix.map((t=>t[e])))),this}isUndoAvailable(){return this.historyPointer>-1&&this.history.length>1}undo(){if(this.isUndoAvailable())return"redo"===this.lastHistoryEvent&&this.historyPointer--,this.historyPointer===this.history.length-1&&(this.historyPointer=this.history.length-2),this.setConfusionMatrix(this.history[this.historyPointer],!1),this.historyPointer--,this.lastHistoryEvent="undo",this}isRedoAvailable(){return this.history.length>1&&this.historyPointere===t));if(e>-1){this._labels.splice(e,1),this._matrix.splice(e,1);for(let t=0;t