Do the work once, and be able to prove it
Research repeats itself: the same grid of settings over the same folds, the same feature table rebuilt for every experiment. joblib removes the waste. Parallel(n_jobs=k)(delayed(f)(x) for x in items) runs the independent calls f(x) on k workers and returns the results in the order of the items; n_jobs=-1 uses every core, and n_jobs=1 runs them one after the other, which is what a browser does. The cost of a job must be larger than the cost of sending its data to a worker, so group tiny jobs together. A grid search is a perfect fit: 5 alphas times 5 walk-forward folds is 25 independent fits, and the answer does not depend on how many workers ran them.
Memory(location).cache(f) wraps f in a disk cache. The first call with some arguments runs f and stores the result under a hash of the arguments; the next call with the same arguments reads the file. The cache survives the end of the program, and it notices when f or an argument changes. Hash NumPy arrays rather than DataFrames: the hash of a DataFrame can depend on its internal memory layout, so two frames with equal content may hash differently, and the cache then misses. joblib.hash(array) is a fingerprint of the content: equal for a copy, different after any change, which is how you prove that two experiments used the same data. Finally joblib.dump(model, path, compress=3) writes a fitted model to a compressed file and joblib.load(path) brings back an object that predicts exactly the same. In the lab, 25 jobs give walk-forward rank correlations within 0.06 of zero at every alpha, slow_features runs once for two cached calls, and the reloaded Ridge predicts identically.
- Independent jobs go through
Parallelanddelayed; the answer must not depend onn_jobs. Memory.cachestores results on disk keyed by a hash of the arguments; pass arrays, not frames.joblib.hashproves two runs used the same data;dumpandloadsave a model that predicts identically.
Lab · what's graded
- One fold job15 pts
- The grid of jobs25 pts
- The disk cache15 pts
- The fingerprint10 pts
- Saving a model15 pts
- The study20 pts