All files main.js

98.6% Statements 141/143
96.15% Branches 25/26
100% Functions 1/1
98.6% Lines 141/143

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 20x 9x 9x 11x 11x 10x 10x 1x 1x 20x 11x 11x 30x 10x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 30x 10x 10x 1x 1x 10x 30x 8x 8x 3x 1x 1x 3x     30x 1x 1x 30x 1x 1x 11x 30x 10x 10x 11x 11x 11x 30x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' );
var isObject = require( '@stdlib/assert/is-plain-object' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Serializes an SGD trainer params object as a formatted string.
*
* ## Notes
*
* -   Example output:
*
*     ```text
*
*     Stochastic Gradient Descent
*
*         penalty: l2
*         learning rate: constant
*         loss function: hinge
*         lambda: 2.5000
*         eta0: 0.0100
*         fit intercept: true
*         intercept: 0.0000
*         max iterations: 1000
*
*     ```
*
* @param {Object} params - SGD trainer params object
* @param {Options} [opts] - options object
* @param {PositiveInteger} [opts.digits=4] - number of digits to display after decimal points
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {string} serialized params
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var params = {
*     'penaltyParams': new Float64Array( [ 2.5, 0.0 ] ),
*     'learningRateParams': new Float64Array( [ 0.01, 0.0 ] ),
*     'lossFunctionParams': new Float64Array( [ 0.0 ] ),
*     'intercept': 0.0,
*     'maxIter': 1000,
*     'penalty': 'l2',
*     'learningRate': 'constant',
*     'lossFunction': 'hinge',
*     'fitIntercept': true,
*     'method': 'Stochastic Gradient Descent'
* };
*
* var str = toString( params );
* // returns <string>
*/
function toString( params, opts ) { // eslint-disable-line stdlib/no-redeclare
	var fitIntercept;
	var dgts;
	var out;
 
	dgts = 4;
	if ( arguments.length > 1 ) {
		if ( !isObject( opts ) ) {
			throw new TypeError( format( 'invalid argument. Must provide an object. Value: `%s`.', opts ) );
		}
		if ( hasOwnProp( opts, 'digits' ) ) {
			if ( !isPositiveInteger( opts.digits ) ) {
				throw new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'digits', opts.digits ) );
			}
			dgts = opts.digits;
		}
	}
 
	fitIntercept = 'false';
	if ( params.fitIntercept === true ) {
		fitIntercept = 'true';
	}
 
	out = [
		'',
		params.method,
		'',
		format( '    penalty: %s', params.penalty ),
		format( '    learning rate: %s', params.learningRate ),
		format( '    loss function: %s', params.lossFunction )
	];
	if ( params.penalty !== 'none' ) {
		out.push( format( '    lambda: %0.'+dgts+'f', params.penaltyParams[ 0 ] ) );
		if ( params.penalty === 'elasticnet' ) {
			out.push( format( '    l1 ratio: %0.'+dgts+'f', params.penaltyParams[ 1 ] ) );
		}
	}
	if ( params.learningRate === 'constant' ) {
		out.push( format( '    eta0: %0.'+dgts+'f', params.learningRateParams[ 0 ] ) );
	}
	else if ( params.learningRate === 'invscaling' ) {
		out.push( format( '    eta0: %0.'+dgts+'f', params.learningRateParams[ 0 ] ) );
		out.push( format( '    powerT: %0.'+dgts+'f', params.learningRateParams[ 1 ] ) );
	} else if ( params.learningRate === 'pegasos' && params.penalty === 'none' ) {
		out.push( format( '    lambda: %0.'+dgts+'f', params.learningRateParams[ 0 ] ) );
	}
	if ( params.lossFunction === 'epsilon-insensitive' || params.lossFunction === 'squared-epsilon-insensitive' ) {
		out.push( format( '    epsilon: %0.'+dgts+'f', params.lossFunctionParams[ 0 ] ) );
	}
	if ( params.lossFunction === 'huber' ) {
		out.push( format( '    huber threshold: %0.'+dgts+'f', params.lossFunctionParams[ 0 ] ) );
	}
	out.push( format( '    fit intercept: %s', fitIntercept ) );
	if ( params.fitIntercept === true ) {
		out.push( format( '    intercept: %0.'+dgts+'f', params.intercept ) );
	}
	out.push( format( '    max iterations: %d', params.maxIter ) );
	out.push( '' );
	return out.join( '\n' );
}
 
 
// EXPORTS //
 
module.exports = toString;