Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/ Callidus Public archive

A collection of machine learning algorithms for Typescript.

License

Notifications You must be signed in to change notification settings

haydenhigg/Callidus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Callidus

A collection of machine-learning algorithms.

Notes:

The object that you require has the objects Regress, Classify, Cluster, and Tools.  

Regress has

Classify has

Cluster has

Tools has

Regress

These are algorithms for finding the best fit line between arrays of inputs/outputs based on a variety of functions.

Initializing

Input and output must both be arrays of numbers.

  • Polynomial: constructor([input = [], output = [], degree = 2])
  • Others: constructor([input = [], output = []])

Training

Training:

  • All: train()

Predicting

Predicting:

  • All: predict(x)

Getting error and correlation

Error:

  • All: findStandardError()

Correlation:

  • All: findCorrelation()

Putting it all together

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

Classify

These are algorithms for categorizing arrays of input data into different classes based on a variety of methods.

Initializing

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 rate
  • Bernoulli: constructor([input = []], output = []])
  • Multinomial: constructor([input = []], output = []])
  • ZeroR: constructor([output = []])
  • OneR: constructor([input = [], output = []])

Training

Batch training:

  • KNearestNeighbors: doesn't need to be trained
  • NearestCentroid: train()
  • Perceptron: train(gamma[, maxIterations = 10000])
  • Bernoulli: train()
  • Multinomial: train()
  • ZeroR: doesn't need to be trained
  • OneR: train()

Online training (for any not specified here, just use the usual train method):

  • Perceptron: trainFor([iterations = 1])

Predicting

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)

Putting it all together

Example 1:

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:

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

Cluster

These are unsupervised learning techniques that are made for clustering data.

Initializing

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 = []])

Training

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])

Getting the output

Centroids:

  • KMeans: the centroids property, an array of arrays of numbers
  • KMedians: the centroids property, an array of arrays of numbers

Tools

Porter

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).

Porter2

The newer, revised version of Porter. It has exactly the same interface as Porter for stemming and tokenizing.

Saving and Loading Models

All models can be transformed to and from JSON-format strings using these instance methods:

  • Saving: exportJSON([jsonFormattingSpaces = 0])
  • Loading: importJSON(jsonOb)

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.

Releases

No releases published

Packages

No packages published