Sklearn Installation
To learn Sklearn, installation is the first step. Since Sklearn depends on several other libraries (such as NumPy, SciPy, and matplotlib), we need to ensure that these dependent libraries are also installed.
System environment requirements:
- Python version: scikit-learn supports Python 3.7 and above.
- Operating system: scikit-learn can run on major operating systems such as Windows, macOS, and Linux.
- Package management tool: You can use
piporcondato install scikit-learn.
In this chapter, we use pip to install scikit-learn.
Before installation, make sure Python and pip are installed.
Check whether Python is installed:
python --version
Check whether pip is installed:
pip --version
If Python and pip are not installed, you can refer to our:Python InstallationandPip Installation。
Note:The latest Python version currently comes with pip pre-installed.
Note:Python 2.7.9+ or Python 3.4+ versions come with the pip tool.
Install Sklearn
Sklearn stands for scikit-learn.
Use pip to install the latest version of scikit-learn:
pip install scikit-learn
If you wish to install a specific version of scikit-learn, you can specify the version number:
pip install scikit-learn==1.2.0
Check if the installation was successful
After installation, we can check whether scikit-learn was installed successfully with the following code:
Example
print(sklearn.__version__)
If the scikit-learn version number is displayed successfully, similar to the following, then the installation was successful:
1.5.2
Install scikit-learn using conda
If you are using the Anaconda environment, it is recommended to use conda to install scikit-learn.
Anaconda is a Python distribution for scientific computing that includes many data science and machine learning libraries, making it convenient for developers.
If you are not yet familiar with Anaconda, you can refer to:Anaconda Tutorial。
Create a new conda environment (optional)
You can choose to create a new virtual environment for scikit-learn to avoid conflicts with other projects:
conda create -n sklearn-env python=3.9 conda activate sklearn-env
Install scikit-learn
Use conda to install scikit-learn:
conda install scikit-learn
If you want to install a specific version, you can specify the version number:
conda install scikit-learn=1.2.0
Verify installation
In the conda environment, you can verify the installation using the Python shell or Jupyter Notebook:
Example
print(sklearn.__version__)
If the scikit-learn version number is displayed successfully, similar to the following, then the installation was successful:
1.5.2
Install other dependencies
scikit-learn depends on some other libraries, especially:
- NumPy: used for handling arrays and numerical computation
- SciPy: provides more advanced mathematical computing tools
- matplotlib(optional): used for data visualization
- joblib(optional): used for model persistence (saving and loading)
If you install with pip, scikit-learn will automatically install these dependencies, but if you want to manually install or update them, you can use the following commands:
pip install numpy scipy matplotlib joblib
If you install with conda, all dependent libraries will be installed automatically.
Other extensions