|
| 1 | +/** |
| 2 | +* @license |
| 3 | +* Copyright 2021, JsData. All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +
|
| 8 | +* Unless required by applicable law or agreed to in writing, software |
| 9 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +* See the License for the specific language governing permissions and |
| 12 | +* limitations under the License. |
| 13 | +* ========================================================================== |
| 14 | +*/ |
| 15 | + |
| 16 | +import { tensor1d, Tensor, tensor2d, moments } from "@tensorflow/tfjs-node" |
| 17 | +import { DataFrame, Series } from "danfojs-node" |
| 18 | +import { is1DArray } from "../../utils" |
| 19 | + |
| 20 | +/** |
| 21 | + * Standardize features by removing the mean and scaling to unit variance. |
| 22 | + * The standard score of a sample x is calculated as: `z = (x - u) / s`, |
| 23 | + * where `u` is the mean of the training samples, and `s` is the standard deviation of the training samples. |
| 24 | + */ |
| 25 | +export default class StandardScaler { |
| 26 | + private $std: Tensor |
| 27 | + private $mean: Tensor |
| 28 | + |
| 29 | + constructor() { |
| 30 | + this.$std = tensor1d([]) |
| 31 | + this.$mean = tensor1d([]) |
| 32 | + } |
| 33 | + |
| 34 | + private $getTensor(data: number[] | number[][] | Tensor | DataFrame | Series) { |
| 35 | + let $tensorArray; |
| 36 | + |
| 37 | + if (data instanceof Array) { |
| 38 | + if (is1DArray(data)) { |
| 39 | + $tensorArray = tensor1d(data as number[]) |
| 40 | + } else { |
| 41 | + $tensorArray = tensor2d(data) |
| 42 | + } |
| 43 | + } else if (data instanceof DataFrame) { |
| 44 | + $tensorArray = tensor2d(data.values as number[][]) |
| 45 | + } else if (data instanceof Series) { |
| 46 | + $tensorArray = tensor1d(data.values as number[]) |
| 47 | + } else if (data instanceof Tensor) { |
| 48 | + $tensorArray = data |
| 49 | + } else { |
| 50 | + throw new Error("ParamError: data must be one of Array, DataFrame or Series") |
| 51 | + } |
| 52 | + return $tensorArray |
| 53 | + } |
| 54 | + /** |
| 55 | + * Fit a StandardScaler to the data. |
| 56 | + * @param data Array, Tensor, DataFrame or Series object |
| 57 | + * @returns StandardScaler |
| 58 | + * @example |
| 59 | + * const scaler = new StandardScaler() |
| 60 | + * scaler.fit([1, 2, 3, 4, 5]) |
| 61 | + */ |
| 62 | + public fit(data: number[] | number[][] | Tensor | DataFrame | Series) { |
| 63 | + const tensorArray = this.$getTensor(data) |
| 64 | + this.$std = moments(tensorArray, 0).variance.sqrt(); |
| 65 | + this.$mean = tensorArray.mean(0); |
| 66 | + return this |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Transform the data using the fitted scaler |
| 71 | + * @param data Array, Tensor, DataFrame or Series object |
| 72 | + * @returns Array, Tensor, DataFrame or Series object |
| 73 | + * @example |
| 74 | + * const scaler = new StandardScaler() |
| 75 | + * scaler.fit([1, 2, 3, 4, 5]) |
| 76 | + * scaler.transform([1, 2, 3, 4, 5]) |
| 77 | + * // [0.0, 0.0, 0.0, 0.0, 0.0] |
| 78 | + * */ |
| 79 | + public transform(data: number[] | number[][] | Tensor | DataFrame | Series) { |
| 80 | + const tensorArray = this.$getTensor(data) |
| 81 | + const outputData = tensorArray.sub(this.$mean).div(this.$std) |
| 82 | + |
| 83 | + if (Array.isArray(data)) { |
| 84 | + return outputData.arraySync() |
| 85 | + |
| 86 | + } else if (data instanceof Series) { |
| 87 | + return new Series(outputData, { |
| 88 | + index: data.index, |
| 89 | + }); |
| 90 | + |
| 91 | + } else if (data instanceof DataFrame) { |
| 92 | + return new DataFrame(outputData, { |
| 93 | + index: data.index, |
| 94 | + columns: data.columns, |
| 95 | + }); |
| 96 | + } else { |
| 97 | + return outputData |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Fit and transform the data using the fitted scaler |
| 103 | + * @param data Array, Tensor, DataFrame or Series object |
| 104 | + * @returns Array, Tensor, DataFrame or Series object |
| 105 | + * @example |
| 106 | + * const scaler = new StandardScaler() |
| 107 | + * scaler.fit([1, 2, 3, 4, 5]) |
| 108 | + * scaler.fitTransform([1, 2, 3, 4, 5]) |
| 109 | + * // [0.0, 0.0, 0.0, 0.0, 0.0] |
| 110 | + * */ |
| 111 | + public fitTransform(data: number[] | number[][] | Tensor | DataFrame | Series) { |
| 112 | + this.fit(data) |
| 113 | + return this.transform(data) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Inverse transform the data using the fitted scaler |
| 118 | + * @param data Array, Tensor, DataFrame or Series object |
| 119 | + * @returns Array, Tensor, DataFrame or Series object |
| 120 | + * @example |
| 121 | + * const scaler = new StandardScaler() |
| 122 | + * scaler.fit([1, 2, 3, 4, 5]) |
| 123 | + * scaler.transform([1, 2, 3, 4, 5]) |
| 124 | + * // [0.0, 0.0, 0.0, 0.0, 0.0] |
| 125 | + * scaler.inverseTransform([0.0, 0.0, 0.0, 0.0, 0.0]) |
| 126 | + * // [1, 2, 3, 4, 5] |
| 127 | + * */ |
| 128 | + public inverseTransform(data: number[] | number[][] | Tensor | DataFrame | Series) { |
| 129 | + const tensorArray = this.$getTensor(data) |
| 130 | + const outputData = tensorArray.mul(this.$std).add(this.$mean) |
| 131 | + |
| 132 | + if (Array.isArray(data)) { |
| 133 | + return outputData.arraySync() |
| 134 | + |
| 135 | + } else if (data instanceof Series) { |
| 136 | + return new Series(outputData, { |
| 137 | + index: data.index, |
| 138 | + }); |
| 139 | + |
| 140 | + } else if (data instanceof DataFrame) { |
| 141 | + return new DataFrame(outputData, { |
| 142 | + index: data.index, |
| 143 | + columns: data.columns, |
| 144 | + }); |
| 145 | + } else { |
| 146 | + return outputData |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | + |
0 commit comments