from sklearn.externals.joblib import Memory mem = Memory(location='./my_cache') @mem.cache def my_function(x): # ... some computationally expensive operation ... return result
from sklearn.externals.joblib import Memory import numpy as np mem = Memory(location='./my_cache') def compute_something(x): # ... some computationally expensive operation ... return result my_data = np.random.rand(1000, 1000) func = mem.cache(compute_something) result1 = func(my_data) result2 = func(my_data)In this example, we define a function `compute_something` that takes an argument `x` and uses it to perform some computation. We then create a Memory object with a cache location of `./my_cache` and use its `cache` method to wrap our `compute_something` function. We then call our wrapped function, `func`, twice with the same input data, `my_data`. The first time `func` is called, the result is computed and cached. The second time `func` is called, the cached result is returned immediately. In both examples, the `cache` decorator is used to indicate that the function should be memoized. The `Memory` object provides a cache to store the results. The `location` parameter indicates where to store the cache; it can be a directory or a file.