scale

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RangeScaler

type RangeScaler struct {
	// contains filtered or unexported fields
}

RangeScaler implements range-based feature scaling (often known as Min-Max scaling).

It scales numeric values in specified columns to a normalized range, typically [0, 1], using statistics learned during the Fit phase.

The scaler stores per-column minimum and maximum values computed from a Table, and applies the transformation in a non-mutating manner during Transform. It also stores the name of columns being fitted in a slice of string.

Formula:

scaled = (x - min) / (max - min)

RangeScaler is stateful: Fit must be called before Transform.

func NewRangeScaler

func NewRangeScaler() *RangeScaler

NewRangeScaler creates and initializes a new RangeScaler instance.

The returned scaler has empty internal state and must be fitted using Fit before it can be used to transform data.

func (*RangeScaler) Features

func (s *RangeScaler) Features() []string

Features returns the list of column names that were fitted by the scaler.

The returned slice represents the features learned during the Fit step and is used as the default set of columns when Transform is called without explicitly specifying columns.

A copy of the underlying slice is returned, so modifying the result will not affect the scaler's internal state.

func (*RangeScaler) Fit

func (s *RangeScaler) Fit(t *table.Table, columns ...string) error

Fit computes and stores the minimum and maximum values for each specified column.

Fit scans the provided Table and extracts numeric values from the given columns. The computed statistics are stored internally and later used by Transform.

Parameters:

  • t: the input Table used to learn scaling parameters
  • columns: names of columns to be scaled

Returns:

  • error if a column does not exist or contains no numeric values

Notes:

  • Fit does not modify the input Table.
  • Fit must be called before Transform.
  • Calling Fit multiple times overwrites previously stored statistics for the specified columns.

func (*RangeScaler) IsFitted

func (s *RangeScaler) IsFitted() bool

IsFitted reports whether the scaler has been fitted with at least one feature.

It returns true if Fit has been successfully called and at least one column's statistics (min and max) have been stored. Otherwise, it returns false.

func (*RangeScaler) Max

func (s *RangeScaler) Max(column string) (float64, bool)

Max returns the maximum value learned for the specified column during Fit. The boolean return value indicates whether the column was present and fitted.

func (*RangeScaler) Min

func (s *RangeScaler) Min(column string) (float64, bool)

Min returns the minimum value learned for the specified column during Fit. The boolean return value indicates whether the column was present and fitted.

func (*RangeScaler) Reset

func (s *RangeScaler) Reset()

Reset clears all learned statistics and fitted features from the scaler.

After calling Reset, the scaler returns to its initial state and must be fitted again using Fit before Transform can be called.

func (*RangeScaler) Transform

func (s *RangeScaler) Transform(t *table.Table, columns ...string) (*table.Table, error)

Transform applies range-based scaling to the specified columns of a Table.

Transform returns a new Table with scaled values, leaving the original Table unchanged. Only numeric values are transformed; non-numeric values are preserved as-is.

Parameters:

  • t: the input Table to be transformed
  • columns: names of columns to scale

Returns:

  • *table.Table: a new Table containing the transformed data
  • error if a column does not exist, has zero range, or was not fitted

Notes:

  • Transform assumes Fit has already been called for the specified columns.
  • Scaling is performed using the formula: (x - min) / (max - min)
  • Columns with zero range (min == max) result in an error to avoid division by zero.

type Scaler

type Scaler interface {
	Fit(*table.Table, ...string) error
	Transform(*table.Table, ...string) (*table.Table, error)
	Features() []string
	IsFitted() bool
	Reset()
}

type ZScaler

type ZScaler struct {
	// contains filtered or unexported fields
}

ZScaler performs Z-score standardization (also known as standard scaling) on selected numeric columns of a Table.

Z-score standardization transforms each numeric value x using:

z = (x - mean) / std

where mean and std (standard deviation) are computed from the training data during the Fit phase.

ZScaler is stateful: it stores per-column mean and standard deviation learned during Fit, and applies them consistently during Transform. It also stores the name of columns being fitted in a slice of string.

Non-numeric values are left unchanged during transformation.

func NewZScaler

func NewZScaler() *ZScaler

NewZScaler creates and returns a new ZScaler with empty internal state.

The returned scaler must be fitted using Fit before Transform can be called. Each call to Fit overwrites any previously stored statistics.

func (*ZScaler) Features

func (s *ZScaler) Features() []string

Features returns the list of column names that were fitted by the scaler.

The returned slice represents the features learned during the Fit step and is used as the default set of columns when Transform is called without explicitly specifying columns.

A copy of the underlying slice is returned, so modifying the result will not affect the scaler's internal state.

func (*ZScaler) Fit

func (s *ZScaler) Fit(t *table.Table, columns ...string) error

Fit computes the mean and standard deviation for the specified columns from the provided Table.

Only numeric values are considered when computing statistics. If a column contains no numeric values, or has zero standard deviation, Fit returns an error.

Fit stores the computed statistics internally and overwrites any previously fitted values for the same columns.

func (*ZScaler) IsFitted

func (s *ZScaler) IsFitted() bool

IsFitted reports whether the scaler has been fitted with at least one feature.

It returns true if Fit has been successfully called and at least one column's statistics (mean and standard deviation) have been stored. Otherwise, it returns false.

func (*ZScaler) Mean

func (s *ZScaler) Mean(column string) (float64, bool)

Mean returns the mean value learned for the specified column during Fit. The boolean return value indicates whether the column was present and fitted.

func (*ZScaler) Reset

func (s *ZScaler) Reset()

Reset clears all learned statistics and fitted features from the scaler.

After calling Reset, the scaler returns to its initial state and must be fitted again using Fit before Transform can be called.

func (*ZScaler) Std

func (s *ZScaler) Std(column string) (float64, bool)

Std returns the standard deviation learned for the specified column during Fit. The boolean return value indicates whether the column was present and fitted.

func (*ZScaler) Transform

func (s *ZScaler) Transform(t *table.Table, columns ...string) (*table.Table, error)

Transform applies Z-score standardization to the specified columns using statistics computed during Fit.

Transform returns a new Table with transformed column values. The original Table is not modified.

Each numeric value x is transformed as:

(x - mean) / std

where mean and std are the values learned during Fit. Non-numeric values are left unchanged.

Transform returns an error if Fit has not been called for a column or if the stored standard deviation is zero.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL