= np.random.randn(100, )
xs = MinMaxScaler()
scaler = scaler.fit_transform(xs)
transformed_xs assert transformed_xs.shape == (100, )
assert np.allclose(xs, scaler.inverse_transform(transformed_xs))
# Test correctness
assert np.allclose(
transformed_xs, 100, 1)).reshape(100,)
skp.MinMaxScaler().fit_transform(xs.reshape(
)# Also work with 2D array
= xs.reshape(100, 1)
xs = MinMaxScaler()
scaler = scaler.fit_transform(xs)
transformed_xs assert np.allclose(xs, scaler.inverse_transform(transformed_xs))
assert np.allclose(
transformed_xs, 100, 1))
skp.MinMaxScaler().fit_transform(xs.reshape( )
Data Preprocessors
relax.data_utils.preprocessing.DataPreprocessor
class relax.data_utils.preprocessing.DataPreprocessor (name=None)
Base class for data preprocessors.
Parameters:
- name (
<class 'str'>
, default=None) – The name of the preprocessor. If None, the class name will be used.
Methods
fit (xs, y=None)
Fit the preprocessor with xs
and y
.
transform (xs)
Transform xs
.
fit_transform (xs, y=None)
Fit the preprocessor with xs
and y
, then transform xs
.
inverse_transform (xs)
Inverse transform xs
.
to_dict ()
Convert the preprocessor to a dictionary.
from_dict (params)
Load the attributes of the preprocessor from a dictionary.
relax.data_utils.preprocessing.MinMaxScaler
class relax.data_utils.preprocessing.MinMaxScaler ()
Base class for data preprocessors.
Methods
fit (xs, y=None)
Fit the preprocessor with xs
and y
.
transform (xs)
Transform xs
.
fit_transform (xs, y=None)
Fit the preprocessor with xs
and y
, then transform xs
.
inverse_transform (xs)
Inverse transform xs
.
to_dict ()
Convert the preprocessor to a dictionary.
from_dict (params)
Load the attributes of the preprocessor from a dictionary.
MinMaxScaler
only supports scaling a single feature.
= xs.reshape(50, 2)
xs = MinMaxScaler()
scaler lambda: scaler.fit_transform(xs),
test_fail(="`MinMaxScaler` only supports array with a single feature") contains
Convert to a dictionary (or the pytree representations).
= xs.reshape(-1, 1)
xs = MinMaxScaler().fit(xs)
scaler = MinMaxScaler().from_dict(scaler.to_dict())
scaler_1 assert np.allclose(scaler.transform(xs), scaler_1.transform(xs))
relax.data_utils.preprocessing.EncoderPreprocessor
class relax.data_utils.preprocessing.EncoderPreprocessor (name=None)
Encode categorical features as an integer array.
Parameters:
- name (
<class 'str'>
, default=None) – The name of the preprocessor. If None, the class name will be used.
Methods
fit (xs, y=None)
Fit the preprocessor with xs
and y
.
transform (xs)
Transform xs
.
fit_transform (xs, y=None)
Fit the preprocessor with xs
and y
, then transform xs
.
inverse_transform (xs)
Inverse transform xs
.
to_dict ()
Convert the preprocessor to a dictionary.
from_dict (params)
Load the attributes of the preprocessor from a dictionary.
relax.data_utils.preprocessing.OrdinalPreprocessor
class relax.data_utils.preprocessing.OrdinalPreprocessor (name=None)
Ordinal encoder for a single feature.
Parameters:
- name (
<class 'str'>
, default=None) – The name of the preprocessor. If None, the class name will be used.
Methods
fit (xs, y=None)
Fit the preprocessor with xs
and y
.
transform (xs)
Transform xs
.
fit_transform (xs, y=None)
Fit the preprocessor with xs
and y
, then transform xs
.
inverse_transform (xs)
Inverse transform xs
.
to_dict ()
Convert the preprocessor to a dictionary.
from_dict (params)
Load the attributes of the preprocessor from a dictionary.
= np.random.choice(['a', 'b', 'c'], size=(100, 1))
xs = OrdinalPreprocessor().fit(xs)
enc = enc.transform(xs)
transformed_xs assert np.all(enc.inverse_transform(transformed_xs) == xs)
# Test from_dict and to_dict
= OrdinalPreprocessor().from_dict(enc.to_dict())
enc_1 assert np.all(enc.transform(xs) == enc_1.transform(xs))
= np.array(['a', 'b', 'c', np.nan, 'a', 'b', 'c', np.nan], dtype=object).reshape(-1, 1)
xs = OrdinalPreprocessor().fit(xs)
enc # Check categories_
assert np.array_equiv(enc.categories_, np.array(['a', 'b', 'c', np.nan], dtype=str))
= enc.transform(xs)
transformed_xs assert transformed_xs.shape == (8, 1)
= enc.inverse_transform(transformed_xs)
inverse_transformed_xs assert np.all(inverse_transformed_xs == xs.astype(str))
# Test from_dict and to_dict
= OrdinalPreprocessor().from_dict(enc.to_dict())
enc_1 assert np.all(enc.transform(xs) == enc_1.transform(xs))
assert np.array_equal(enc.categories_, enc_1.categories_)
= np.random.choice(['a', 'b', 'c'], size=(100, ))
xs lambda: OrdinalPreprocessor().fit_transform(xs),
test_fail(="OrdinalPreprocessor only supports 2D array with a single feature") contains
relax.data_utils.preprocessing.OneHotEncoder
class relax.data_utils.preprocessing.OneHotEncoder (name=None)
One-hot encoder for a single categorical feature.
Parameters:
- name (
<class 'str'>
, default=None) – The name of the preprocessor. If None, the class name will be used.
Methods
fit (xs, y=None)
Fit the preprocessor with xs
and y
.
transform (xs)
Transform xs
.
fit_transform (xs, y=None)
Fit the preprocessor with xs
and y
, then transform xs
.
inverse_transform (xs)
Inverse transform xs
.
to_dict ()
Convert the preprocessor to a dictionary.
from_dict (params)
Load the attributes of the preprocessor from a dictionary.
= np.random.choice(['a', 'b', 'c'], size=(100, 1))
xs = OneHotEncoder().fit(xs)
enc = enc.transform(xs)
transformed_xs assert np.all(enc.inverse_transform(transformed_xs) == xs)
# Test from_dict and to_dict
= OneHotEncoder().from_dict(enc.to_dict())
enc_1 assert np.all(enc.transform(xs) == enc_1.transform(xs))
= np.array(['a', 'b', 'c', np.nan, 'a', 'b', 'c', np.nan], dtype=object).reshape(-1, 1)
xs = OneHotEncoder().fit(xs)
enc # Check categories_
assert np.array_equiv(enc.categories_, np.array(['a', 'b', 'c', np.nan], dtype=str))
= enc.transform(xs)
transformed_xs assert np.all(enc.inverse_transform(transformed_xs) == xs.astype(str))
assert np.array_equal(
=False).fit_transform(xs)
transformed_xs, skp.OneHotEncoder(sparse_output
) # Test from_dict and to_dict
= OneHotEncoder().from_dict(enc.to_dict())
enc_1 = OneHotEncoder()
enc_2
enc_2.from_dict(enc_1.to_dict())assert np.all(enc.transform(xs) == enc_1.transform(xs))
assert np.all(enc.transform(xs) == enc_2.transform(xs))
= np.random.choice(['a', 'b', 'c'], size=(100, ))
xs lambda: OneHotEncoder().fit_transform(xs),
test_fail(="OneHotEncoder only supports 2D array with a single feature") contains