SciPy - clear_cache() Function



The clear_cache() method in SciPy gets rid of any stored data from earlier operations. This helps make sure old or unneeded files aren't taking up memory space.

This method is helpful when you need to free up resources or work with new data. People often use it when they're dealing with big datasets or perform iterative computations.

Getting rid of the cache can also help you avoid problems that might happen if you use old or messed-up cached files. It's an easy way to keep your system running well and stop mistakes in later operations.

Syntax

Following is the syntax of the SciPy clear_cache() method −

.clear_cache(datasets=None)

Parameters

This method accept only a single parameter which is named by datasets − (list of strings, optional)

  • The names of the datasets that need to be removed from the cache.

  • If None, all cached datasets are cleared.

Return Value

The method does not return any value. It performs the clearing operation silently. By default it clears all cached data files.

Example 1: Clearing All Cached Datasets

In the below example all datasets are cleared from the cache as we didn't specify the datasets that need to be deleted.

from scipy.datasets import clear_cache

# Clear all cached datasets
clear_cache()
print("All cached datasets cleared.")

Following is an output of the above code −

All cached datasets cleared.

Example 2: Clearing Cache for Specific Dataset(ascent)

This example shows how to load a dataset, verify its shape, then use clear_cache() to clear the dataset's cache. The code demonstrates how to control cached information and release space when it is no longer required.

from scipy import datasets

# Load the 'ascent' dataset (example dataset)
ascent_array = datasets.ascent()

# Print the shape of the loaded dataset
print("Shape of Ascent Dataset:", ascent_array.shape)

# Clear the cache for the 'ascent' dataset
datasets.clear_cache([datasets.ascent])
print("Cache cleared for the 'ascent' dataset.")

Output of the above code is as follows −

Cleaning the file ascent.dat for dataset ascent
Cache cleared for the 'ascent' dataset.
scipy_reference.htm
Advertisements