license import * as tf from '@tensorflow/tfjs-core' ; import { Dataset } from './dataset' ; /**
 * The value associated with a given key for a single element.
 *
 * Such a value may not have a batch dimension.  A value may be a scalar or an
 * n-dimensional array.
 */ export type ElementArray = number | number [ ] | tf . Tensor | string ; /**
 * A map from string keys (aka column names) to values for a single element.
 */ export type TabularRecord = { [ key ] : ElementArray ; } ; /** An interface representing numeric statistics of a column. */ export interface NumericColumnStatistics { min : number ; max : number ; mean : number ; variance : number ; stddev : number ; length : number ; } /**
 * An interface representing column level NumericColumnStatistics for a
 * Dataset.
 */ export interface DatasetStatistics { [ key ] : NumericColumnStatistics ; } param min param max return export function scaleTo01 ( min , max ) { const range = max - min ; const minTensor = tf . scalar ( min ) ; const rangeTensor = tf . scalar ( range ) ; return ( value ) : ElementArray => { if ( typeof ( value ) === 'string' ) { throw new Error ( 'Can\'t scale a string.' ) ; } else { if ( value instanceof tf . Tensor ) { const result = value . sub ( minTensor ) . div ( rangeTensor ) ; return result ; } else if ( value instanceof Array ) { return value . map ( v => ( v - min ) / range ) ; } else { return ( value - min ) / range ; } } } ; } param dataset param sampleSize param shuffleWindowSize return export async function computeDatasetStatistics ( dataset , sampleSize ? , shuffleWindowSize ? ) { let sampleDataset = dataset ; if ( shuffleWindowSize != null ) { sampleDataset = sampleDataset . shuffle ( shuffleWindowSize ) ; } if ( sampleSize != null ) { sampleDataset = sampleDataset . take ( sampleSize ) ; } const result = { } ; await sampleDataset . forEachAsync ( e => { for ( const key of Object . keys ( e ) ) { const value = e [ key ] ; if ( typeof ( value ) === 'string' ) { } else { let previousMean = 0 ; let previousLength = 0 ; let previousVariance = 0 ; let columnStats = result [ key ] ; if ( columnStats == null ) { columnStats = { min : Number . POSITIVE_INFINITY , max : Number . NEGATIVE_INFINITY , mean : 0 , variance : 0 , stddev : 0 , length : 0 } ; result [ key ] = columnStats ; } else { previousMean = columnStats . mean ; previousLength = columnStats . length ; previousVariance = columnStats . variance ; } let recordMin ; let recordMax ; let valueLength = 0 ; let valueMean = 0 ; let valueVariance = 0 ; let combinedLength = 0 ; let combinedMean = 0 ; let combinedVariance = 0 ; if ( value instanceof tf . Tensor ) { recordMin = value . min ( ) . dataSync ( ) [ 0 ] ; recordMax = value . max ( ) . dataSync ( ) [ 0 ] ; const valueMoment = tf . moments ( value ) ; valueMean = valueMoment . mean . dataSync ( ) [ 0 ] ; valueVariance = valueMoment . variance . dataSync ( ) [ 0 ] ; valueLength = value . size ; } else if ( value instanceof Array ) { recordMin = value . reduce ( ( a , b ) => Math . min ( a , b ) ) ; recordMax = value . reduce ( ( a , b ) => Math . max ( a , b ) ) ; const valueMoment = tf . moments ( value ) ; valueMean = valueMoment . mean . dataSync ( ) [ 0 ] ; valueVariance = valueMoment . variance . dataSync ( ) [ 0 ] ; valueLength = value . length ; } else if ( ! isNaN ( value ) && isFinite ( value ) ) { recordMin = value ; recordMax = value ; valueMean = value ; valueVariance = 0 ; valueLength = 1 ; } else { columnStats = null ; continue ; } combinedLength = previousLength + valueLength ; combinedMean = previousMean + ( valueLength / combinedLength ) * ( valueMean - previousMean ) ; combinedVariance = previousVariance + ( valueLength / combinedLength ) * ( valueVariance + ( ( valueMean - combinedMean ) * ( valueMean - previousMean ) ) - previousVariance ) ; columnStats . min = Math . min ( columnStats . min , recordMin ) ; columnStats . max = Math . max ( columnStats . max , recordMax ) ; columnStats . length = combinedLength ; columnStats . mean = combinedMean ; columnStats . variance = combinedVariance ; columnStats . stddev = Math . sqrt ( combinedVariance ) ; } } } ) ; for ( const key in result ) { const stat = result [ key ] ; if ( stat . length === 1 ) { stat . variance = NaN ; stat . stddev = NaN ; } } return result ; }