Skip to content

Instantly share code, notes, and snippets.

@BexTuychiev
Last active June 25, 2023 02:11
Show Gist options
  • Save BexTuychiev/a972507915944e6ba332f35fbc9c8193 to your computer and use it in GitHub Desktop.
Save BexTuychiev/a972507915944e6ba332f35fbc9c8193 to your computer and use it in GitHub Desktop.
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