Skip to content

Instantly share code, notes, and snippets.

@petersandersen
petersandersen / TempVars.md
Last active June 23, 2023 09:43 — forked from bskinn/TempVars.md
Python context manager for scrubbing of temporary variables in Jupyter notebook cells

Rationale Spillover of temporary variables can be a significant annoyance when working with Jupyter notebooks. Aesthetically and logistically, it's bothersome to have a del var1, var2, var3 hanging around at the bottom of a notebook cell to take care of cleanup of these temp variables. It prevents a Ctrl+End from going to the end of relevant code, and if an Exception is raised in the course of code execution the del cleanup doesn't happen.

A short content manager class takes care of these various problems: when execution leaves the with suite, even due to a raised exception, the indicated variables are deleted.

@petersandersen
petersandersen / preproc.py
Created January 19, 2016 11:00 — forked from kastnerkyle/preproc.py
General preprocessing transforms in scikit-learn compatible format
# (C) Kyle Kastner, June 2014
# License: BSD 3 clause
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import gen_batches
from scipy.linalg import eigh
from scipy.linalg import svd
import numpy as np
# From sklearn master