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))
NodeJS Classification-based learner.
opts
Object
Options.
opts.dataset
Array<Object>
Dataset (for training and testing) (optional, default
require('./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, default classifierBuilder
)opts.pastTrainingSamples
Array<Object>
Past training samples for the classifier (optional, default []
)opts.classes
Array<string>
List of classes (categories) (optional, default require('./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, default false
)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 classifynumOfFolds
number
Cross-validation folds (optional, default 5
)verboseLevel
number
Verbosity level on limdu's explainations (optional, default 0
)log
boolean
Cross-validation logging (optional, default false
)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, default false
)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, default false
)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 positiveReturns number True Positives
Number of elements that aren't in the category
class but predicted as such.
category
string
Class/category considered as positiveReturns number False Positives
Number of elements in the category
class but predicted as not being in it.
category
string
Class/category considered as positiveReturns number False Negatives
Number of elements that aren't in the category
class correctly predicted.
category
string
Class/category considered as positiveReturns 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 positiveReturns 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 positiveReturns number TN + FP
Number of predicted "positive" elements (i.e. elements guessed as belonging to
the category
class).
category
string
Class/category considered as positiveReturns 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 positiveReturns number TN + FP
Support value (count/occurrences) of category
in the matrix
category
string
Class/category to look atReturns number Support value
Prediction accuracy for category
.
category
string
Class/category considered as positiveReturns 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 positiveReturns 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 positiveReturns number TP / (TP + FP)
Prediction F1 score for category
.
category
string
Class/category considered as positiveReturns 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 positiveReturns 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 positiveReturns 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 positiveReturns 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 positiveReturns 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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
String representation
#### toTable
`console.table` version of `confusionMatrix.toString()`.
##### Parameters
- `opt`
**[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
Options (optional, default `{}`)
- `opt.split`
**[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Split the classes in half (β 2 matrices) (optional, default `false`)
- `opt.clean`
**[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Remove empty column/row pairs (optional, default `false`)
- `opt.colours`
**[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Colourize cells (optional, default `true`)
- `opt.maxValue` (optional, default `100`)
#### getShortStats
##### Parameters
- `type`
**[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
Type of stats (`micro`/`macro`/`weighted` average) (optional, default
`'micro'`)
Returns
**[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
Short statistics (total, true, false, accuracy, precision, recall and f1)
#### getStats
Returns **{total:
[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number),
correctPredictions:
[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number),
incorrectPredictions:
[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number),
classes:
[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>,
microAvg:
[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object),
macroAvg:
[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object),
results:
[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)}**
(Long) statistics
#### fromData
Creates a confusion matrix from the `actual` and `predictions` classes.
##### Parameters
- `actual`
**[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
Actual classes
- `predictions`
**[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
Predicted classes
- `classes`
**[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
Classes/categories to use (optional, default `[]`)
Returns **[ConfusionMatrix](#confusionmatrix)** Filled confusion matrix
## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="http://maxcubing.wordpress.com"><img src="https://avatars0.githubusercontent.com/u/8260834?v=4?s=100" width="100px;" alt="Maximilian Berkmann"/><br /><sub><b>Maximilian Berkmann</b></sub></a><br /><a href="https://github.com/all-contributors/ac-learn/commits?author=Berkmann18" title="Code">π»</a> <a href="https://github.com/all-contributors/ac-learn/commits?author=Berkmann18" title="Documentation">π</a> <a href="#ideas-Berkmann18" title="Ideas, Planning, & Feedback">π€</a> <a href="#maintenance-Berkmann18" title="Maintenance">π§</a> <a href="#platform-Berkmann18" title="Packaging/porting to new platform">π¦</a> <a href="#infra-Berkmann18" title="Infrastructure (Hosting, Build-Tools, etc)">π</a> <a href="https://github.com/all-contributors/ac-learn/commits?author=Berkmann18" title="Tests">β οΈ</a> <a href="#security-Berkmann18" title="Security">π‘οΈ</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://tenshiamd.com"><img src="https://avatars.githubusercontent.com/u/13580338?v=4?s=100" width="100px;" alt="Angel Aviel Domaoan"/><br /><sub><b>Angel Aviel Domaoan</b></sub></a><br /><a href="https://github.com/all-contributors/ac-learn/commits?author=tenshiAMD" title="Code">π»</a> <a href="#maintenance-tenshiAMD" title="Maintenance">π§</a> <a href="https://github.com/all-contributors/ac-learn/pulls?q=is%3Apr+reviewed-by%3AtenshiAMD" title="Reviewed Pull Requests">π</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/features/security"><img src="https://avatars.githubusercontent.com/u/27347476?v=4?s=100" width="100px;" alt="Dependabot"/><br /><sub><b>Dependabot</b></sub></a><br /><a href="#security-dependabot" title="Security">π‘οΈ</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://dev.to/gr2m"><img src="https://avatars.githubusercontent.com/u/39992?v=4?s=100" width="100px;" alt="Gregor Martynus"/><br /><sub><b>Gregor Martynus</b></sub></a><br /><a href="https://github.com/all-contributors/ac-learn/pulls?q=is%3Apr+reviewed-by%3Agr2m" title="Reviewed Pull Requests">π</a></td>
</tr>
</tbody>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the
[all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!