Last active
July 18, 2022 04:17
-
-
Save p-geon/c8ddc4c7941d51f5a35f98848041bcf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import itertools | |
from typing import List, Callable | |
import numpy as np; np.random.seed(seed=42) | |
from sklearn.preprocessing import StandardScaler, minmax_scale | |
# https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html#sklearn.preprocessing.StandardScaler | |
# https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.minmax_scale.html#sklearn.preprocessing.minmax_scale | |
def check_result(y1, y2, eps=1e-11): | |
print(f'[y1] max: {np.round(np.max(y1), 4)}, min: {np.round(np.min(y1), 4)}, mean: {np.round(np.mean(y1), 4)}, std: {np.round(np.std(y1), 4)}') | |
print(f'[y2] max: {np.round(np.max(y2), 4)}, min: {np.round(np.min(y2), 4)}, mean: {np.round(np.mean(y2), 4)}, std: {np.round(np.std(y2), 4)}') | |
assert np.sum(np.linalg.norm(y1 - y2, ord=2)) < eps, f'Arrays inconsistency in normalizing. {y1}, {y2}' | |
def test_preprocessing(normalize: List[Callable], _y: np.ndarray) -> None: | |
y = copy.deepcopy(_y) | |
y1 = normalize[0](y) | |
y2 = normalize[1](y) | |
check_result(y1, y2) | |
ys = [np.identity(5)] + [np.random.rand(10, 10) for _ in range(5)] | |
methods = { | |
'normalize_std': [ | |
lambda x: StandardScaler().fit(x).transform(x), | |
lambda x: (x - np.mean(x, axis=0)) / np.std(x, axis=0) | |
], | |
'normalize_minmax': [ | |
lambda x: minmax_scale(x), | |
lambda x: (x - np.min(x, axis=0))/(np.max(x, axis=0) - np.min(x, axis=0)) | |
], | |
} | |
for i, (m, y) in enumerate(itertools.product(methods.keys(), ys)): | |
print(f"> {m}-{1+i%ys.__len__()}") | |
test_preprocessing(methods[m], y) |
Author
p-geon
commented
Jul 18, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment