Commit 407ba04f authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[COORDINATE][STANDARD NORMALIZATION] add standard normalization. and add test.

parent 8874ec26
No related merge requests found
Showing with 12 additions and 8 deletions
+12 -8
import pandas as pd import pandas as pd
import numpy as np import numpy as np
from sklearn.preprocessing import Normalizer from sklearn.preprocessing import Normalizer, StandardScaler
class AbstractTransformation(object): class AbstractTransformation(object):
...@@ -37,9 +37,9 @@ class CenteredScaledNormalization(AbstractTransformation): ...@@ -37,9 +37,9 @@ class CenteredScaledNormalization(AbstractTransformation):
def __init__(self, df_coordinates): def __init__(self, df_coordinates):
super().__init__(df_coordinates) super().__init__(df_coordinates)
assert self.nb_dimensions == 1 assert self.nb_dimensions == 1
self.transformer = Normalizer().fit(df_coordinates.transpose().values.reshape(-1, 1)) self.scaler = StandardScaler().fit(df_coordinates.transpose().values.reshape(-1, 1))
def transform_array(self, coordinate: np.ndarray): def transform_array(self, coordinate: np.ndarray):
return self.transformer.transform(np.array([coordinate]))[0] return self.scaler.transform(np.array([coordinate]))[0]
...@@ -6,6 +6,8 @@ from spatio_temporal_dataset.coordinates.spatio_temporal_coordinates.generated_s ...@@ -6,6 +6,8 @@ from spatio_temporal_dataset.coordinates.spatio_temporal_coordinates.generated_s
GeneratedSpatioTemporalCoordinates, UniformSpatioTemporalCoordinates GeneratedSpatioTemporalCoordinates, UniformSpatioTemporalCoordinates
from spatio_temporal_dataset.coordinates.temporal_coordinates.generated_temporal_coordinates import \ from spatio_temporal_dataset.coordinates.temporal_coordinates.generated_temporal_coordinates import \
ConsecutiveTemporalCoordinates ConsecutiveTemporalCoordinates
from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.abstract_transformation import \
CenteredScaledNormalization
from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.uniform_normalization import \ from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.uniform_normalization import \
BetweenZeroAndOneNormalization, BetweenMinusOneAndOneNormalization BetweenZeroAndOneNormalization, BetweenMinusOneAndOneNormalization
...@@ -15,17 +17,19 @@ class TestTransformation(unittest.TestCase): ...@@ -15,17 +17,19 @@ class TestTransformation(unittest.TestCase):
def test_temporal_normalization(self): def test_temporal_normalization(self):
nb_steps = 3 nb_steps = 3
start = 1950 start = 1950
transformation_class_to_expected = {BetweenZeroAndOneNormalization: [0.0, 0.5, 1.0], transformation_class_to_expected = {
BetweenMinusOneAndOneNormalization: [-1.0, 0.0, 1.0]} BetweenZeroAndOneNormalization: [0.0, 0.5, 1.0],
BetweenMinusOneAndOneNormalization: [-1.0, 0.0, 1.0],
CenteredScaledNormalization: [-1.22474487, 0., 1.22474487],
}
for transformation_class, expected in transformation_class_to_expected.items(): for transformation_class, expected in transformation_class_to_expected.items():
temporal_coordinates = ConsecutiveTemporalCoordinates.from_nb_temporal_steps(nb_temporal_steps=nb_steps, temporal_coordinates = ConsecutiveTemporalCoordinates.from_nb_temporal_steps(nb_temporal_steps=nb_steps,
start=start, start=start,
transformation_class=transformation_class) transformation_class=transformation_class)
normalized_coordinates = temporal_coordinates.df_coordinates().iloc[:, 0].values normalized_coordinates = temporal_coordinates.df_coordinates().iloc[:, 0].values
expected_coordinates = np.array(expected) expected_coordinates = np.array(expected)
equals = normalized_coordinates == expected_coordinates equal = np.allclose(normalized_coordinates , expected_coordinates)
self.assertTrue(equals.all(), self.assertTrue(equal, msg="expected: {}, res:{}".format(expected_coordinates, normalized_coordinates))
msg="expected: {}, res:{}".format(expected_coordinates, normalized_coordinates))
def test_spatio_temporal_normalization(self): def test_spatio_temporal_normalization(self):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment