scaler.py
AI for Chemistry/code/chemprop_VP-main (1)/chemprop_VP-main/chemprop/data/scaler.py
from typing import Any, List, Optional
import numpy as np
class StandardScaler:
"""A :class:`StandardScaler` normalizes the features of a dataset.
When it is fit on a dataset, the :class:`StandardScaler` learns the mean and standard deviation across the 0th axis.
When transforming a dataset, the :class:`StandardScaler` subtracts the means and divides by the standard deviations.
"""
def __init__(self, means: np.ndarray = None, stds: np.ndarray = None, replace_nan_token: Any = None):
"""
:param means: An optional 1D numpy array of precomputed means.
:param stds: An optional 1D numpy array of precomputed standard deviations.
:param replace_nan_token: A token to use to replace NaN entries in the features.
"""
self.means = means
self.stds = stds
self.replace_nan_token = replace_nan_token
def fit(self, X: List[List[Optional[float]]]) -> 'StandardScaler':
"""
Learns means and standard deviations across the 0th axis of the data :code:`X`.
:param X: A list of lists of floats (or None).
:return: The fitted :class:`StandardScaler` (self).
"""
X = np.array(X).astype(float)
self.means = np.nanmean(X, axis=0)
self.stds = np.nanstd(X, axis=0)
self.means = np.where(np.isnan(self.means),
np.zeros(self.means.shape), self.means)
self.stds = np.where(np.isnan(self.stds),
np.ones(self.stds.shape), self.stds)
self.stds = np.where(self.stds == 0, np.ones(
self.stds.shape), self.stds)
return self
def transform(self, X: List[List[Optional[float]]]) -> np.ndarray:
"""
Transforms the data by subtracting the means and dividing by the standard deviations.
:param X: A list of lists of floats (or None).
:return: The transformed data with NaNs replaced by :code:`self.replace_nan_token`.
"""
X = np.array(X).astype(float)
transformed_with_nan = (X - self.means) / self.stds
transformed_with_none = np.where(
np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
return transformed_with_none
def inverse_transform(self, X: List[List[Optional[float]]]) -> np.ndarray:
"""
Performs the inverse transformation by multiplying by the standard deviations and adding the means.
:param X: A list of lists of floats.
:return: The inverse transformed data with NaNs replaced by :code:`self.replace_nan_token`.
"""
X = np.array(X).astype(float)
transformed_with_nan = X * self.stds + self.means
transformed_with_none = np.where(
np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
return transformed_with_none
class divmaxScaler:
def __init__(self, maxs: np.ndarray = None, replace_nan_token: Any = None):
self.replace_nan_token = replace_nan_token
self.maxs = maxs
def fit(self, X) -> 'divmaxScaler':
""" a=np.array([1,2,3,None],dtype=np.float32)
b=np.nanmean(a)
print(f'a:{a}\tb:{b}')
"""
if type(X) is list:
X = np.array(X).astype(float)
self.means = np.nanmean(X, axis=0)
self.stds = np.nanstd(X, axis=0)
self.maxs = np.nanmax(X, axis=0)
self.means = np.where(np.isnan(self.means), None, self.means)
return self
def transform(self, X) -> np.ndarray:
if type(X) is list:
X = np.array(X).astype(float)
transformed_with_nan = X / self.maxs
transformed_with_none = np.where(
np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
return transformed_with_none
def inverse_transform(self, X) -> np.ndarray:
if type(X) is list:
X = np.array(X).astype(float)
transformed_with_nan = X * self.maxs
transformed_with_none = np.where(
np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
return transformed_with_none
Artigos relacionados
cosmo_to_s_profile_ver_1.1.1.py
cosmo_to_s_profile_ver_1.1.1.py — python source code from the AI for Chemistry learning materials (AI for Chemistry/2025_COSMO/data/s-profiles-all/List-9/cosmo_to_s_profile_ver_1.1.1.py).
Ler artigo →area.py
area.py — python source code from the AI for Chemistry learning materials (AI for Chemistry/2025_COSMO/data/s-profiles-all/area_volume-org/area.py).
Ler artigo →area_volume.py
area_volume.py — python source code from the AI for Chemistry learning materials (AI for Chemistry/2025_COSMO/data/s-profiles-all/area_volume-org/area_volume.py).
Ler artigo →volume.py
volume.py — python source code from the AI for Chemistry learning materials (AI for Chemistry/2025_COSMO/data/s-profiles-all/area_volume-org/volume.py).
Ler artigo →check_final_CID.py
check_final_CID.py — python source code from the AI for Chemistry learning materials (AI for Chemistry/2025_COSMO/data/s-profiles-all/check/check_final_CID.py).
Ler artigo →check_for_CID.py
check_for_CID.py — python source code from the AI for Chemistry learning materials (AI for Chemistry/2025_COSMO/data/s-profiles-all/check/check_for_CID.py).
Ler artigo →