-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from paucablop/add-scale-by-index
Add scale by index
- Loading branch information
Showing
7 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .min_max_normalize import MinMaxScaler | ||
from .l_normalize import LNormalize | ||
from .index_scaler import IndexScaler | ||
from .min_max_scaler import MinMaxScaler | ||
from .norm_scaler import NormScaler |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
from sklearn.base import BaseEstimator, TransformerMixin | ||
from sklearn.utils.validation import check_is_fitted | ||
|
||
from chemotools.utils.check_inputs import check_input | ||
|
||
|
||
class IndexScaler(BaseEstimator, TransformerMixin): | ||
def __init__(self, index: int = 0): | ||
self.index = index | ||
|
||
|
||
def fit(self, X: np.ndarray, y=None) -> "IndexScaler": | ||
# Check that X is a 2D array and has only finite values | ||
X = check_input(X) | ||
|
||
# Set the number of features | ||
self.n_features_in_ = X.shape[1] | ||
|
||
# Set the fitted attribute to True | ||
self._is_fitted = True | ||
|
||
return self | ||
|
||
def transform(self, X: np.ndarray, y=None) -> np.ndarray: | ||
# Check that the estimator is fitted | ||
check_is_fitted(self, "_is_fitted") | ||
|
||
# Check that X is a 2D array and has only finite values | ||
X = check_input(X) | ||
X_ = X.copy() | ||
|
||
# Check that the number of features is the same as the fitted data | ||
if X_.shape[1] != self.n_features_in_: | ||
raise ValueError(f"Expected {self.n_features_in_} features but got {X_.shape[1]}") | ||
|
||
# Scale the data by index | ||
for i, x in enumerate(X_): | ||
X_[i] = x / x[self.index] | ||
|
||
return X_.reshape(-1, 1) if X_.ndim == 1 else X_ |
File renamed without changes.
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
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
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
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