Where the All Contributors machine can learn about your contributions.
yarn add ac-learn --save
#or
npm i -D ac-learn
const Learner = require('ac-learn')
//If you want to load a learner from a JSON export:
const learner = Learner.fromJSON(require('./your-learner.json'))
//If you want to use the default one
const learner = new Learner()
//If you want to your own dataset or customise the learner, check https://github.com/all-contributors/ac-learn#learner
//Training
learner.train() //Or
learner.train(someTrainingSet)
//Testing and getting stats
const fullStats = learner.eval()
//Cross-validation
const {microAvg, macroAvg} = learner.crossValidate()
//Confusion matrix (as string or console table)
const textualTable = learner.confusionMatrix.toString()
const cmTable = learner.confusionMatrix.toTable()
//Classifying an input
const output = learner.classify(someInput)
//Getting an input from an output
const input = learner.backClassify(someOutput)
//Saving the model to a JSON file
const savedModel = learner.toJSON()
const {writeFileSync} = require('fs')
writeFileSync('your-learner.json', JSON.stringify(jsonData))
- Learner
- ConfusionMatrix
- addEntry
- setEntry
- getEntry
- getTotal
- getTP
- getFP
- getFN
- getTN
- getDiagonal
- getTrue
- getFalse
- getPositive
- getNegative
- getPredPositive
- getPredNegative
- getSupport
- getAccuracy
- getMicroAccuracy
- getMacroAccuracy
- getWeightedAccuracy
- getTotalPositiveRate
- getMicroRecall
- getMacroRecall
- getWeightedRecall
- getPositivePredictiveValue
- getPositivePredictiveValue
- getMicroPrecision
- getMacroPrecision
- getWeightedPrecision
- getMicroF1
- getMacroF1
- getWeightedF1
- getFalseNegativeRate
- getMicroMissRate
- getMacroMissRate
- getWeightedMissRate
- getFalsePositiveRate
- getMicroFallOut
- getMacroFallOut
- getWeightedFallOut
- getTrueNegativeRate
- getMicroSpecificity
- getMacroSpecificity
- getWeightedSpecificity
- getPrevalence
- getMicroPrevalence
- getMacroPrevalence
- getWeightedPrevalence
- toString
- toTable
- getShortStats
- getStats
- fromData
NodeJS Classification-based learner.
opts
Object Options.opts.dataset
Array<Object> Dataset (for training and testing) (optional, defaultrequire('./conv')('io')
)opts.splits
number Dataset split percentage for the training/validation set (default: 70%/15%/15%) (optional, default[.7,.15]
)opts.classifier
function (): Object Classifier builder function (optional, defaultclassifierBuilder
)opts.pastTrainingSamples
Array<Object> Past training samples for the classifier (optional, default[]
)opts.classes
Array<string> List of classes (categories) (optional, defaultrequire('./categories')
)
Using pre-defined data
const learner = new Learner()
Using a custom dataset
const learner = new Learner({
dataset: [
{input: 'something bad', output: 'bad'},
{input: 'a good thing', output: 'good'},
],
})
Using a specified classifier function
const learner = new Learner({
classifier: myClassifierBuilderFn, //see {@link module:./classifier} for an example (or checkout `limdu`'s examples)
})
Changing the train/test split percentage
const learner = new Learner({
splits: [0.6, 0.2],
})
(Re-)Using past-training samples
const learner = new Learner({
pastTrainingSamples: [
{input: 'something bad', output: 'bad'},
{input: 'a good thing', output: 'good'},
],
})
log
boolean Log events (optional, defaultfalse
)
Returns Object Statistics from a confusion matrix
Returns string Serialized classifier
file
string Filename (optional, default'classifier.json'
)
Returns Promise<(string | Error)> Serialized classifier
serializedClassifier
string .
Returns Object Deserialized classifier
file
string Filename (optional, default'classifier.json'
)
Returns Promise<(string | Error)> Deserialized classifier
data
{input: any, output: any} Data to classify
numOfFolds
number Cross-validation folds (optional, default5
)verboseLevel
number Verbosity level on limdu's explainations (optional, default0
)log
boolean Cross-validation logging (optional, defaultfalse
)
Returns {microAvg: Object, macroAvg: Object} Averages
category
string Category name.
Returns
Array<string>
Labels associated with category
JSON representation of the learner with the serialized classification model.
Returns Object JSON representation
Returns Learner Generated learner from json
Get the observational overall/train/validation/test count for each classes in the associated dataset.
log
boolean Log events (optional, defaultfalse
)outputFile
string Filename for the output (to be used by chart.html) (optional, default''
)
Returns Object<string, {overall: number, test: number, validation: number, train: number}> Partitions
log
boolean Log events (optional, defaultfalse
)categoryPartitionOutput
string Filename for the output of the category partitions. (optional, default''
)
Returns Object Statistics
Multi-class focused confusion matrix.
Returns number Updated entry
Returns number Entry
Get the total count of all entries.
Returns number Total count
Number of elements in the category
class correctly predicted.
category
string Class/category considered as positive
Returns number True Positives
Number of elements that aren't in the category
class but predicted as such.
category
string Class/category considered as positive
Returns number False Positives
Number of elements in the category
class but predicted as not being in it.
category
string Class/category considered as positive
Returns number False Negatives
Number of elements that aren't in the category
class correctly predicted.
category
string Class/category considered as positive
Returns number True Negatives
Diagonal of truth (top-left β bottom-right)
Returns Array<number> Numbers in the diagonal
Number of correct (truthful) predictions.
Returns number TP
Number of incorrect predictions.
Returns number FP + FN
Number of real (actual) "positive" elements (i.e. elements that belong to the
category
class).
category
string Class/category considered as positive
Returns number TP + FN
Number of real (actual) "negative" elements (i.e. elements that don't belong to
the category
class).
category
string Class/category considered as positive
Returns number TN + FP
Number of predicted "positive" elements (i.e. elements guessed as belonging to
the category
class).
category
string Class/category considered as positive
Returns number TP + FN
Number of predicted "negative" elements (i.e. elements guessed as not belonging
to the category
class).
category
string Class/category considered as positive
Returns number TN + FP
Support value (count/occurrences) of category
in the matrix
category
string Class/category to look at
Returns number Support value
Prediction accuracy for category
.
category
string Class/category considered as positive
Returns number (TP + TN) / (TP + TN + FP + FN)
Micro-average of accuracy.
Returns number (TP0 + ... + TPn + TN0 + ... + TNn) / (TP0 + ... + TPn + TN0 + ... + TNn + FP0 + ... + FPn + FN0 + ... + FNn)
Macro-average of accuracy.
Returns number (A0 + ...+ An_1) / n
Weighted accuracy.
Returns number (A0 _ s0 + ... + An _ sn) / Total
Predicition recall.
category
string Class/category considered as positive
Returns number TP / (TP + FN)
Micro-average of recall.
Returns number (TP0 + ... + TPn) / (TP0 + ... + TPn + FN0 + ... + FNn)
Macro-average of recall.
Returns number (R0 + R1 + ... + Rn-1) / n
Weighted recalll.
Returns number (R0 _ s0 + ... + Rn _ sn) / Total
Prediction precision for category
.
category
string Class/category considered as positive
Returns number TP / (TP + FP)
Prediction F1 score for category
.
category
string Class/category considered as positive
Returns number 2 _ (Pr _ R) / (Pr + R)
Micro-average of the precision.
Returns number (TP0 + ... + TPn) / (TP0 + ... + TPn + FP0 + ... FPn)
Macro-average of the precsion.
Returns number (Pr0 + Pr1 + ... + Pr_n-1) / n
Weighted precision.
Returns number (Pr0 _ s0 + ... + Prn _ sn) / Total
Micro-average of the F1 score.
Returns number 2 _ (TP0 + ... + TPn) / (2 _ (TP0 + ... + TPn) + (FN0 + ... + FNn) + (FP0 + ... + FPn))
Macro-average of the F1 score.
Returns number (F0_1 + F1_1 + ... + F_n-1_1) / n
Weighted F1.
Returns number (F01 * s0 + ... + Fn1 * sn) / Total
Miss rates on predictions for category
.
category
string Class/category considered as positive
Returns number FN / (TP + FN)
Micro-average of the miss rate.
Returns number (FN0 + ... + FNn) / (TP0 + ... + TPn + FN0 + ... FNn)
Macro-average of the miss rate.
Returns number (M0 + M1 + ... + Mn) / n
Weighted miss rate.
Returns number (M0 _ s0 + ... + Mn _ sn) / Total
Fall out (false alarm) on predictions for category
.
category
string Class/category considered as positive
Returns number FP / (FP + TN)
Micro-average of the fall out.
Returns number (FP0 + ... + FPn) / (FP0 + ... + FPn + TN0 + ... TNn)
Macro-average of the fall out.
Returns number (Fo0 + Fo1 + ... + Fo_n) / n
Weighted fall out.
Returns number (Fo0 _ s0 + ... + Fon _ sn) / Total
Specificity on predictions for category
.
category
string Class/category considered as positive
Returns number TN / (FP + TN)
Micro-average of the specificity.
Returns number (TN0 + ... + TNn) / (FP0 + ... + FPn + TN0 + ... TNn)
Macro-average of the specificity.
Returns number (S0 + S1 + ... + Sn) / n
Weighted specificity.
Returns number (S0 _ s0 + ... + Sn _ sn) / Total
Prevalence on predictions for category
.
category
string Class/category considered as positive
Returns number (TP + FN) / (TP + TN + FP + FN)
Micro-average of the prevalence.
Returns number (TP0 + ... + TPn + FN0 + ... + FNn) / (TP0 + ... + TPn + TN0 + ... + TNn + FP0 + ... + FPn + FN0 + ... + FNn)
Macro-average of the prevalence.
Returns number (Pe0 + Pe1 + ... + Pen) / n
Weighted prevalence.
Returns number (Pe0 _ s0 + ... + Pen _ sn) / Total
Textual tabular representation of the confusion matrix.
opt
Object Options (optional, default{}
)
Example output (cf. /src/tests/confusionMatrix.js)
```
Actual \\ Predicted bug code other
------------------ ---- ---- -----
bug 5.00 0.00 1.00
code 1.00 2.00 0.00
other 0.00 3.00 8.00
Returns string String representation
console.table
version of confusionMatrix.toString()
.
opt
Object Options (optional, default{}
)
type
string Type of stats (micro
/macro
/weighted
average) (optional, default'micro'
)
Returns string Short statistics (total, true, false, accuracy, precision, recall and f1)
Returns {total: number, correctPredictions: number, incorrectPredictions: number, classes: Array<string>, microAvg: Object, macroAvg: Object, results: Object} (Long) statistics
Creates a confusion matrix from the actual
and predictions
classes.
actual
Array<string> Actual classespredictions
Array<string> Predicted classesclasses
Array<string> Classes/categories to use (optional, default[]
)
Returns ConfusionMatrix Filled confusion matrix
Maximilian Berkmann π» π π€ π§ π¦ π |
Angel Aviel Domaoan π» π§ π |
Dependabot π‘οΈ |
Gregor Martynus π |
This project follows the all-contributors specification. Contributions of any kind welcome!