@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 6 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import e from"./../assert/is-string.js";import t from"./../assert/is-array.js";import r from"./../utils/copy.js";import i from"./../utils/define-property.js";import n from"./../math/base/special/exp.js";import a from"./../math/base/special/max.js";import o from"./../assert/is-positive-integer.js";import s from"./../assert/is-boolean.js";import l from"./../utils/define-nonenumerable-read-only-property.js";import p from"./../math/base/special/pow.js";import u from"./../utils/constant-function.js";import m from"./../assert/is-nonnegative-number.js";import c from"./../assert/is-positive-number.js";import d from"./../assert/is-plain-object.js";import h from"./../assert/has-own-property.js";var v=a,f=1e-7;function b(e,t,r){var i;t>0&&(i=1-r*t,e.scaleTo(v(i,f)))}var g=b;function w(e,t,r,i,n){var a=r*e.innerProduct(t);g(e,n,i),0!==r&&(a<-1?e.add(t,4*i*r):a<1&&e.add(t,i*(r-a*r)))}var y=b;function j(e,t,r,i,n){var a=r*e.innerProduct(t);y(e,n,i),a<1&&0!==r&&e.add(t,i*(r-a*r))}function E(e,t){var r,i=e.length,n=0;for(r=0;r<i;r++)n+=e[r]*t[r];return n}var T=o,P=s,_=l,V=p,R=E,k=1e-11;function O(e){var t;if(this.scale<k){for(t=0;t<this.nWeights;t++)this._data[t]*=this.scale;this.scale=1}if(this.norm*=V(e,2),!(e>0))throw new RangeError("Scaling weight vector by nonpositive value, likely due to too large value of eta * lambda. Value: `"+e+"`");this.scale*=e}function W(e,t){var r,i,n;for(i=0,void 0===t&&(t=1),n=0;n<e.length;n++)r=e[n]*t,i+=this._data[n]*r,this._data[n]=this._data[n]+r/this.scale;this.intercept&&(r=1*t,i+=this._data[n]*r,this._data[n]=this._data[n]+r/this.scale),this.norm+=(R(e,e)+(this.intercept?1:0))*V(t,2)+2*this.scale*i}function F(e){var t,r=0;for(t=0;t<e.length;t++)r+=this._data[t]*e[t];return r+=this.intercept?this._data[t]:0,r*=this.scale}function x(e,t){var r;if(!(this instanceof x))return new x(e,t);if(!T(e))throw new TypeError("invalid argument. First argument `dim` must be a positive integer. Value: `"+e+"`.");if(!P(t))throw new TypeError("invalid argument. Second argument `intercept` must be a boolean primitive. Value: `"+t+"`.");for(this.scale=1,this.norm=0,this.intercept=t,this.nWeights=e+(this.intercept?1:0),this._data=new Array(this.nWeights),r=0;r<this.nWeights;r++)this._data[r]=0}_(x.prototype,"scaleTo",O),_(x.prototype,"add",W),_(x.prototype,"innerProduct",F);var H=b;function S(e,t,r,i,n){var a=e.innerProduct(t);H(e,n,i),r*a<=0&&e.add(t,i*r)}var q=b;function A(e,t,r,i,n){var a=r*e.innerProduct(t);q(e,n,i),a<1&&0!==r&&e.add(t,i*r)}var z=n,B=b;function C(e,t,r,i,n){var a=r/(1+z(r*e.innerProduct(t)));B(e,n,i),e.add(t,i*a)}var D=u;function G(){var e=1;return function(){var t=1e3/(e+1e3);return e+=1,t}}function I(e){var t=1;return function(){var r=1/(e*t);return t+=1,r}}function J(e,t,r){if("basic"===e)return G();if("constant"===e)return D(t);if("pegasos"===e)return I(r);throw new Error("invalid input value. First argument must be a recognized learning rate. Value: `"+e+"`.")}var K=.1,L=.02,M=!0,N=.001,Q="basic",U="log",X={epsilon:K,eta0:L,intercept:M,lambda:N,learningRate:Q,loss:U},Y=m.isPrimitive,Z=c.isPrimitive,$=s.isPrimitive,ee=d,te=e.isPrimitive,re=h;function ie(e,t){return ee(t)?re(t,"epsilon")&&(e.epsilon=t.epsilon,!Z(e.epsilon))?new TypeError("invalid option. `epsilon` option must be a positive number. Option: `"+e.epsilon+"`."):re(t,"eta0")&&(e.eta0=t.eta0,!Z(e.eta0))?new TypeError("invalid option. `eta0` option must be a positive number. Option: `"+e.eta0+"`."):re(t,"lambda")&&(e.lambda=t.lambda,!Y(e.lambda))?new TypeError("invalid option. `lambda` option must be a nonnegative number. Option: `"+e.lambda+"`."):re(t,"learningRate")&&(e.learningRate=t.learningRate,!te(e.learningRate))?new TypeError("invalid option. `learningRate` option must be a primitive string. Option: `"+e.learningRate+"`."):re(t,"loss")&&(e.loss=t.loss,!te(e.loss))?new TypeError("invalid option. `loss` option must be a primitive string. Option: `"+e.loss+"`."):re(t,"intercept")&&(e.intercept=t.intercept,!$(e.intercept))?new TypeError("invalid option. `intercept` option must be a primitive boolean. Option: `"+e.intercept+"`."):null:new TypeError("invalid argument. Options must be an object. Value: `"+t+"`.")}var ne=e.isPrimitive,ae=t,oe=r,se=i,le=n,pe=w,ue=j,me=x,ce=S,de=A,he=C,ve=J,fe=X,be=ie;function ge(e){var t,r,i,n,a,o,s;if(o=oe(fe),arguments.length>0&&(s=be(o,e)))throw s;switch(i=null,o.loss){case"log":r=he;break;case"hinge":r=de;break;case"modifiedHuber":r=pe;break;case"perceptron":r=ce;break;case"squaredHinge":r=ue;break;default:throw Error("invalid input value. `loss` option must be either `hinge`, `log`, `modifiedHuber`, `perceptron` or `squaredHinge`. Value: `"+o.loss+"`")}return n=ve(o.learningRate,o.eta0,o.lambda),se(a={},"coefs",{get:function(){var e,t;for(e=new Array(i.nWeights),t=0;t<e.length;t++)e[t]=i._data[t]*i.scale;return e},configurable:!1,enumerable:!1}),a.update=function(e,a){var s;if(i||(i=new me(e.length,o.intercept),t=o.intercept?i.nWeights-1:i.nWeights),!ae(e)||e.length!==t)throw new TypeError("invalid input value. First argument `x` must be an array of length "+this.nFeatures+". Value: `"+e+"`");if(-1!==a&&1!==a)throw new TypeError("invalid input value. Second argument `y` must be +1 or -1. Value: `"+a+"`");s=n(),r(i,e,a,s,o.lambda)},a.predict=function(e,n){var a;if(!ae(e)||e.length!==t)throw new TypeError("invalid input value. First argument `x` must be an array of length "+this.nFeatures+". Value: `"+e+"`");if(arguments.length>1&&(!ne(n)||"link"!==n&&"probability"!==n))throw new TypeError("invalid input value. Second argument `type` must be `probability` or `link`. Value: `"+e+"`");if(a=i.innerProduct(e),"probability"!==n)return a;if(r!==he&&r!==pe)throw new Error("probability predictions are only supported when `loss` is `log` or `modifiedHuber`. Value: `"+n+"`");return 1/(1+le(-a))},a}var we=ge;export default we;
//# sourceMappingURL=online-binary-classification.js.map