Perceptron

Multi-class Averaged Perceptron class.

Methods

defineConfig

defineConfig(config) → {object}

Defines the hyperparameters for perceptron.

Example
// Enable random shuffling of examples!
myPerceptron.defineConfig( { shuffleData: true } );
// -> { shuffleData: true, maxIterations: 9, featureExtractor: null }
Parameters
Name Type Description
config object

table below details the properties of config object.

An empty config object restores the default configuration.

Properties
Name Type Attributes Default Description
shuffleData boolean <optional>
false

determines whether or not the training examples should be randomly shuffled after each iteration a.k.a epoch.

maxIterations number <optional>
9

number of passes that must be made over the examples in order to complete the learning.

featureExtractor function <optional>
null

extracts feature(s) along with the corresponding class label(s) from example prior to each iteration. This is useful when raw examples need to be passed to learn() instead of features & labels. If it extracts >1 features then each of the extracted feature/label pair is processed sequentially during learning. Note shuffleData value will only control the shuffling of input examples and not of the extracted features with this function.

Returns

A copy of configuration defined.

Type
object

exportJSON

exportJSON() → {string}

Exports the learning as a JSON, which may be saved as a text file for later use via importJSON().

Example
// Assuming that learn() method has been already succesful.
myPerceptron.exportJSON();
// -> JSON string.
Throws

Error if export is attempted without learning.

Returns

Learning in JSON format.

Type
string

importJSON

importJSON(json) → {boolean}

Imports an existing JSON learning for prediction purpose only; it cannot be used for further learning.

Example
// Assuming that `json` already has a valid JSON string.
myPerceptron.importJSON( json );
Parameters
Name Type Description
json JSON

containing learnings in as exported by exportJSON.

Throws

Error if json is invalid.

Returns

Always true.

Type
boolean

learn

learn(examples) → {number}

Learns from the examples. The hyperparameters, defined via defineConfig, control learning process.

Example
myPerceptron.learn( examples );
Parameters
Name Type Description
examples Array.<array>

each example is a 2-element array. The first element describes example's features and the second one defines its class label. Both of these are expressed in form of an object. The features object contains name/numeric-value pairs for every feature, whereas the class label contains single name/string-value pair as { label: <class> }.

Throws

Error if all examples belong to only one class OR if attempted after importJSON().

Returns

Number of examples passed.

Type
number

predict

predict(features) → {string}

Predicts the label for the input features. If it is unable to predict then it returns a value unknown.

Example
myPerceptron.predict( features );
Parameters
Name Type Description
features object

object that contains name/value pairs for every feature.

Throws

Error if prediction is attempted without learning or import.

Returns

Predicted class label for the input features.

Type
string

reset

reset() → {boolean}

It completely resets the perceptron by re-initializing all the learning related variables but does not touch the existing configuration.

Since it does not reset the existing configuration, user must define it again prior to learning if required.

Example
myPerceptron.reset();
// -> true
Returns

Always true.

Type
boolean