Last active
June 25, 2023 02:11
-
-
Save BexTuychiev/a972507915944e6ba332f35fbc9c8193 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
from sklearn.pipeline import make_pipeline | |
from sklearn.preprocessing import FunctionTransformer | |
def reduce_memory(X: pd.DataFrame, y=None): | |
"""Simple function to reduce memory usage by casting numeric columns to float32.""" | |
num_cols = X.select_dtypes(incluce=np.number).columns | |
for col in num_cols: | |
X[col] = X.astype("float32") | |
return X, y | |
ReduceMemoryTransformer = FunctionTransformer(reduce_memory) | |
# Plug into a pipeline | |
>>> make_pipeline(SimpleImputer(), ReduceMemoryTransformer) | |
Pipeline(steps=[('simpleimputer', SimpleImputer()), | |
('functiontransformer', ReduceMemoryTransformer()]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment