A collection of machine-learning algorithms.
npm install callidusjsA collection of machine-learning algorithms.
The object that you require has the objects Regress, Classify, Cluster, and Tools.
Regress has
- Linear
- Logarithmic
- ExponentialE
- ExponentialAB
- Power
- Quadratic
- Inverse
- Polynomial
Classify has
- Distance: {
+ KNearestNeighbors
+ NearestCentroid
}
- Weights: {
+ Perceptron
}
- NaiveBayes: {
+ Bernoulli
+ Multinomial
}
- Rules: {
+ ZeroR
+ OneR
}
These are algorithms for finding the best fit line between arrays of inputs/outputs based on a variety of functions.
Input and output must both be arrays of numbers.
- Polynomial: constructor([input = [], output = [], degree = 2])
- Others: constructor([input = [], output = []])
Training:
- All: train()
Predicting:
- All: predict(x)
Error:
- All: findStandardError()
Correlation:
- All: findCorrelation()
``javascript
const { Regress } = require("callidusjs");
var input = [1, 2, 3, 4, 5];
var output = [2, 4, 8, 16, 32];
var model = new Regress.ExponentialAB(input, output);
console.log(model.train().predict(6)); //=> 64
`
These are algorithms for categorizing arrays of input data into different classes based on a variety of methods.
For KNearestNeighbors and Perceptron, input must be an array of arrays of numbers, and output must be an array of numbers. For the Naive Bayes classifiers, the input must be an array of arrays of anything, and output must be an array of anything. Remember, when using the bayesian classifiers, you must not provide data with values based on index in the inputs as with the other classifiers; rather, it's recommended to use an array of unique feature names (like strings) so that the classifiers can see whether/how many times each of these features occurs in a given input. This works especially well for NLP, where you can pass an array of arrays of tokens or stems directly into these classifiers.
- KNearestNeighbors: constructor([input = [], output = []])NearestCentroid
- : constructor([input = [], output = []])Perceptron
- : constructor([input = [], output = [], options = {a: 1}]) where a is the learning rateBernoulli
- : constructor([input = []], output = []])Multinomial
- : constructor([input = []], output = []])ZeroR
- : constructor([output = []])OneR
- : constructor([input = [], output = []])
Batch training:
- KNearestNeighbors: doesn't need to be trainedNearestCentroid
- : train()Perceptron
- : train(gamma[, maxIterations = 10000])Bernoulli
- : train()Multinomial
- : train()ZeroR
- : doesn't need to be trainedOneR
- : train()
Online training (for any not specified here, just use the usual train method):Perceptron
- : trainFor([iterations = 1])
Predicting:
- KNearestNeighbors: predict(x[, k = 1])NearestCentroid
- : predict(x)Perceptron
- : predict(x[, bias = 0])Bernoulli
- : predict(x)Multinomial
- : predict(x)ZeroR
- : predict()OneR
- : predict(x)
Example 1:
`javascript
const { Classify } = require("callidusjs");
var input = [
[1, 1, 1],
[1, 0, 0],
[0, 0, 0],
[0, 1, 1]
];
var output = [1, 1, 0, 0];
var model = new Classify.Weights.Perceptron(input, output);
model.train(0.1); // gamma is 0.1, max iterations is not specified (so 10000)
console.log(model.predict([1, 0, 1])); //=> 1, because the first element of the inputs completely determines output
`
Example 2:
`javascript
const { Classify, Tools } = require("callidusjs");
// aliases for the types (for readability and ease-of-use; the types are only so verbose in an effort to organize)
const Stemmer = Tools.Porter2;
const Classifier = Classify.NaiveBayes.Multinomial;
var input = [
Stemmer.tokenize("This is certainly, very surely an english sentence with plenty of english words, and I hope that the classifier will be able to label it as an english sentence because that's exactly what it is."),
Stemmer.tokenize("C’est certainement, très sûrement, une phrase français avec beaucoup de mots anglais, et j’espère que le classificateur pourra la qualifier de phrase français, car c’est exactement ce que c’est.")
];
var output = [
'english',
'french'
];
var model = new Classifier(input, output);
model.train();
console.log(model.predict(Stemmer.tokenize("Another sentence (guess what language this is in!)"))); //=> english
console.log(model.predict(Stemmer.tokenize("Une autre phrase (devine en quelle langue ceci est!)"))); //=> french
`
These are unsupervised learning techniques that are made for clustering data.
As these algorithms are unsupervised, so you only provide the input (an array of arrays of numbers) to them.
- KMeans: constructor([input = []])KMedians
- : constructor([input = []])
Batch training:
- KMeans: train(k[, maxIterations = 10000])KMedians
- : train(k[, maxIterations = 10000])
Online training (for any not specified here, just use the usual train method):KMeans
- : trainFor(k[, iterations = 1])KMedians
- : trainFor(k[, iterations = 1])
Centroids:
- KMeans: the centroids property, an array of arrays of numbersKMedians
- : the centroids property, an array of arrays of numbers
A classic stemming algorithm. Use the static methods stem(word) to find the stem of a word, or tokenize(text[, punctuation = puncRegex]), where the second optional argument is a regex of items to remove (default is all punctuation).
The newer, revised version of Porter. It has exactly the same interface as Porter for stemming and tokenizing.
All models can be transformed to and from JSON-format strings using these instance methods:
- Saving: exportJSON([jsonFormattingSpaces = 0])importJSON(jsonOb)
- Loading:
Note: the method importJSON will automatically set applicable algorithms' trained` property to true, whether the model that the state was saved from was trained or not.