UNPKG

@jsmlt/jsmlt

Version:

JavaScript Machine Learning

92 lines (68 loc) 3.96 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _base = require('./base'); var _base2 = _interopRequireDefault(_base); var _linalg = require('../math/linalg'); var LinAlg = _interopRequireWildcard(_linalg); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // Internal dependencies /** * The Polynomial kernel. The formula of this kernel is as follows: * (gamma * <x, y> + coef0)^degree */ var PolynomialKernel = function (_Kernel) { _inherits(PolynomialKernel, _Kernel); /** * Initialize the Gaussian kernel with user-specified parameters * * @param {Object} [options] - User-defined options * @param {number} [options.gamma = 1] - Normalization parameter of basic dot product * @param {number} [options.coef0 = 1] - Bias coefficient (not part of the dot product) * @param {number} [options.degree = 2] - Degree of the polynomial */ function PolynomialKernel() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$gamma = _ref.gamma, gamma = _ref$gamma === undefined ? 1 : _ref$gamma, _ref$coef = _ref.coef0, coef0 = _ref$coef === undefined ? 1 : _ref$coef, _ref$degree = _ref.degree, degree = _ref$degree === undefined ? 2 : _ref$degree; _classCallCheck(this, PolynomialKernel); /** * Normalization parameter of basic dot product * @type {number} */ var _this = _possibleConstructorReturn(this, (PolynomialKernel.__proto__ || Object.getPrototypeOf(PolynomialKernel)).call(this)); _this.gamma = gamma; /** * Bias coefficient (not part of the dot product) * @type {number} */ _this.coef0 = coef0; /** * Degree of the polynomial * @type {number} */ _this.degree = degree; return _this; } /** * @see {@link Kernel#apply} */ _createClass(PolynomialKernel, [{ key: 'apply', value: function apply(x, y) { return (this.gamma * LinAlg.dot(x, y) + this.coef0) ** this.degree; } }]); return PolynomialKernel; }(_base2.default); exports.default = PolynomialKernel; module.exports = exports['default'];