📅  最后修改于: 2023-12-03 15:01:23.420000             🧑  作者: Mango
The ImportError
occurs when trying to import the joblib
module from the sklearn.externals
package in Python. This error typically happens when using scikit-learn (sklearn) library version 0.21 or later.
In scikit-learn version 0.21, the joblib
package, which is a dependency of scikit-learn, was moved out from the sklearn.externals
module. It was done to enable better organization and separation of the functionality provided by joblib
and scikit-learn.
To fix the ImportError, you need to update your code to not import joblib
from sklearn.externals
. Instead, you can directly import it from the standalone joblib
package.
Replace the following line:
from sklearn.externals import joblib
with:
import joblib
By doing so, you will be importing joblib
from the separate joblib
package, which is a more up-to-date and recommended approach.
Additionally, ensure that you have the latest version of scikit-learn and joblib packages installed. You can update them using the following commands:
pip install -U scikit-learn
pip install -U joblib
The joblib
package provides tools for saving and loading Python objects efficiently. It is used in scikit-learn to persist models and other objects to disk, allowing you to save time by not retraining the models every time you run your code. 'joblib' also enables you to parallelize your tasks, making it easier to train models on large datasets.
In summary, the ImportError occurs when trying to import joblib
from sklearn.externals
due to changes in scikit-learn's package organization. By updating your code to import joblib
directly and ensuring the latest versions of scikit-learn and joblib
are installed, you can eliminate the error and take advantage of the powerful features provided by 'joblib' in scikit-learn.