Mohammad
Mohammad

Reputation: 1013

When I install older version of scikit-learn in Google Colab, it still import the newest version

I would like to use scikit-learn version 0.24.1 in Google Colab. First, I uninstall the current installed version (which is 0.24.2) by:

!pip uninstall scikit-learn -y

Then, I install version 0.24.1 by:

!pip install scikit-learn==0.24.1

However, when I import scitkit-learn, it is version 0.22.2.post1 which I think is the newest version not version 0.24.1.

If you have any idea how to solve it, please let me know.

Background: I trained model by scikit-learn 0.24.1, and I want to use the same version to load trained model. Now, when I load trained model, I get warning that the version of scikit-learn is different from 0.24.1.

UserWarning: Trying to unpickle estimator _BinaryGaussianProcessClassifierLaplace from version 0.24.1 when using version 0.22.2.post1. This might lead to breaking code or invalid results. Use at your own risk.

enter image description here

Upvotes: 1

Views: 3663

Answers (2)

bardia delagah
bardia delagah

Reputation: 1

Note that when you install or uninstall a package in Google Colab, the changes you intend to make will not take effect until the current session is restarted. The package will not be removed, and if it is installed, the version you intended to install will not replace the previous version until the session is restarted.

To ensure a specific version of a package is correctly installed in Google Colab, follow these steps:

  1. Uninstall the package you want with this command:

    !pip uninstall tensorflow --yes
    
  2. Install the desired version of the package using this command:

    !pip install <package_name>==<version_number>
    
  3. Restart the current session. You have 3 options:

    3.1. Google Colab Menu: menu -> Runtime -> Restart Session

    3.2. Google Colab Shortcut: Ctrl+M+.

    3.3. Run this code to kill the session:

    import os
    os.kill(os.getpid(), 9)   
    

Upvotes: 0

Hans Musgrave
Hans Musgrave

Reputation: 7141

Make sure to install the correct version before executing any import statements. When you import sklearn you're loading the cached module with the old version.

Alternatively you could patch things in the current notebook session by reloading the module via imp or importlib.

Upvotes: 0

Related Questions