Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

JavaScript Machine Learning and Neural Networks with Encog

, 16 Oct 2012 Apache
Use Encog genetic algorithms, simulated annealing, neural networks and more with HTML5 Javascript.
heaton-javascript-ml.zip
encog-encog-javascript-6da0a8b
EncogJavascript.iml
EncogJavascript.ipr
README
encog.ipr
examples
/**
 * This is the top level global variable that this example is stored under.
 * For this example, it is MYAPP.
 *
 * @module MYAPP
 */
var TESTLIB = TESTLIB || {};

/**
 * The namespace function, used to define new namespaces.
 * @param ns_string The namespace that is to be defined.
 * @method namespace
 * @return {Object} The newly created namespace, or existing one.
 */
TESTLIB.namespace = function (ns_string) {
    'use strict';
    var parts = ns_string.split('.'),
        parent = TESTLIB,
        i;

    if (parts[0] === "TESTLIB") {
        parts = parts.slice(1);
    }

    for (i = 0; i < parts.length; i += 1) {
        // create a property if it doesn't exist
        if (parent[parts[i]] === "undefined") {
            parent[parts[i]] = {};
        }
        parent = parent[parts[i]];
    }

    return parent;
};

TESTLIB.namespace('TESTLIB.Vector');
TESTLIB.namespace('TESTLIB.VectorUtil');

/**
 * The vector class.
 * @class Vector
 * @constructor
 */
TESTLIB.Vector = function () {
    'use strict';
};

TESTLIB.Vector.prototype = {


    /**
     * The vector's data.
     *
     * @property data
     * @type Array
     * @default {}
     **/
    data: {},

    /**
     * Sum all of the values in the vector.
     * @method sum
     * @return {Number} The sum of all of the vector values.
     */
    sum: function () {
        'use strict';
        var result = 0, i;
        for (i = 0; i < this.data.length; i += 1) {
            result += this.data[i];
        }
        return result;
    },

    /**
     * The number of elements in this vector.
     * @method sum
     * @return {Number} The number of elements in this vector.
     */
    count: function () {
        'use strict';
        return this.data.length;
    },

    /**
     * The vector length.
     *
     * @method length
     * @return {Number} The vector length.
     */
    length: function () {
        'use strict';
        var i, result;

        result = 0;

        for (i = 0; i < this.data.length; i += 1) {
            result += Math.pow(this.data[i], 2);
        }

        return Math.sqrt(result);
    },

    /**
     * Clear all of the vector elements to zero.
     *
     * @method clear
     * @param len The length, if omitted, length stays the same.
     */
    clear: function (len) {
        'use strict';
        var i, len2;

        this.data = [];
        len2 = len || this.data.length;
        for (i = 0; i < len2; i += 1) {
            this.data[i] = 0;
        }
    },
    /**
     * Format the vector as a string.
     * @method toString
     * @return {String} The string.
     */
    toString: function () {
        'use strict';
        return '[' + this.data.join(',') + ']';
    }
};

/**
 * Create a new vector.
 * @param a If a number, then the length of the vector.  If an array, the initial value of the vector.
 * @method create
 * @static
 * @return {TESTLIB.Vector} The newly created vector.
 */
TESTLIB.Vector.create = function (a) {
    'use strict';
    var result = new TESTLIB.Vector();

    if (a instanceof Array) {
        result.data = a.splice(0);
    } else {
        result.clear(a || 0);
    }


    return result;
};

/**
 * A class that provides utilities for vectors.
 * @class VectorUtil
 * @constructor
 */
TESTLIB.VectorUtil = function () {
    'use strict';
};

/**
 * Calculate the distance between two vectors.
 * @param a The first vector.
 * @param b The second vector.
 * @method distance
 * @return {Number} The distance.
 */
TESTLIB.VectorUtil.distance = function (a, b) {
    'use strict';
    var result = 0, i, d;

    if (!(a instanceof TESTLIB.Vector) || !(b instanceof  TESTLIB.Vector)) {
        throw new Error("Only Vector objects can have have their distance compared.");
    }

    if (a.count() !== b.count()) {
        throw new Error("Only Vector objects of the same size can calculate a distance.");
    }

    for (i = 0; i < a.data.length; i += 1) {
        d = a.data[i] - b.data[i];
        result += d * d;
    }

    return Math.sqrt(result);
};

/**
 * Calculate the distance between two vectors.
 * @param a The first vector to add.
 * @param b The second vector to add.
 * @method distance
 * @return {TESTLIB.Vector} The distance.
 */
TESTLIB.VectorUtil.add = function (a, b) {
    'use strict';
    var result, i;

    if (!(a instanceof TESTLIB.Vector) || !(b instanceof  TESTLIB.Vector)) {
        throw new Error("Only Vector objects can be added.");
    }

    if (a.count() !== b.count()) {
        throw new Error("Only Vector objects of the same size can be added.");
    }

    result = TESTLIB.Vector.create(a.count());

    for (i = 0; i < a.data.length; i += 1) {
        result.data[i] = a.data[i] + b.data[i];
    }

    return result;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

JeffHeaton
Architect
United States United States
Jeff Heaton is a data scientist, PhD student and indy publisher. Jeff works primarily in the programming languages Python, R, C#, Java and C/C++. He is an active technology blogger, open source contributor, and author of more than ten books. His areas of expertise include Data Science, Predictive Modeling, Data Mining, Big Data, Business Intelligence and Artificial Intelligence. He is the lead developer for the Encog Machine Learning Framework open source project. Jeff holds a Masters Degree in Information Management from Washington University, is a Senior Member of the IEEE, a Sun Certified Java Programmer and a Fellow of the Life Management Institute.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 16 Oct 2012
Article Copyright 2012 by JeffHeaton
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid