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 | 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 21x 21x 21x 21x 21x 21x 21x 20x 9x 9x 11x 11x 10x 10x 1x 1x 20x 2x 2x 2x 21x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 21x 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 startcase = require( '@stdlib/string/base/startcase' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var format = require( '@stdlib/string/format' );
// MAIN //
/**
* Serializes a k-means clustering results object as a formatted string.
*
* ## Notes
*
* - Example output:
*
* ```text
*
* K-means clustering using Lloyd algorithm
*
* metric used: squared euclidean
* total replicates: 4
* best replicate: 2
* total iterations: 10
* inertia: 3.2800
*
* Clustering ran for 4 clusters and 10 data samples, each with 3 features
*
* ```
*
* @param {Object} results - k-means clustering results 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 results
*
* @example
* var results = {
* 'replicates': 4,
* 'replicate': 2,
* 'metric': 'sqeuclidean',
* 'iterations': 10,
* 'algorithm': 'lloyd',
* 'inertia': 3.28,
* 'k': 4,
* 'samples': 10,
* 'features': 3,
* 'method': 'K-means clustering'
* };
*
* var str = toString( results );
* // returns <string>
*/
function toString( results, opts ) { // eslint-disable-line stdlib/no-redeclare
var metric;
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;
}
}
if ( results.metric === 'sqeuclidean' ) {
metric = 'squared euclidean';
} else if ( results.metric === 'cityblock' ) {
metric = 'cityblock';
} else if ( results.metric === 'correlation' ) {
metric = 'correlation';
} else {
metric = 'cosine';
}
out = [
'',
format( '%s using %s algorithm', results.method, startcase( results.algorithm ) ),
'',
format( ' metric used: %s', metric ),
format( ' total replicates: %u', results.replicates ),
format( ' best replicate: %u', results.replicate ),
format( ' total iterations: %u', results.iterations ),
format( ' inertia: %0.'+dgts+'f', results.inertia ),
'',
format( 'Clustering ran for %u clusters and %u data samples, each with %u features', results.k, results.samples, results.features )
];
return out.join( '\n' );
}
// EXPORTS //
module.exports = toString;
|