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 these dependencies are also installed.
System requirements:
- Python version: scikit-learn supports Python 3.7 and above.
- Operating system: scikit-learn can run on mainstream operating systems such as Windows, macOS, and Linux.
- Package management tools: you can use
piporcondato install scikit-learn.
In this chapter we use pip to install scikit-learn.
Before installing, 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 Installation The difference between Pip Installation.
Note:The latest Python versions come with pip preinstalled.
Note:Python 2.7.9+ or Python 3.4+ and above come with the pip tool.
Install Sklearn
Sklearn stands for scikit-learn.
Install the latest version of scikit-learn using pip:
pip install scikit-learn
If you want to install a specific version of scikit-learn, you can specify the version number:
pip install scikit-learn==1.2.0
Check whether the installation was successful
After installation, we can check whether scikit-learn was installed successfully with the following code:
Example
import
sklearn
(
sklearn.version
)
If the scikit-learn version number is displayed successfully, similar to the following, the installation is 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 comes with 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
Install scikit-learn using conda:
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 the installation
In the conda environment, you can verify the installation via the Python shell or Jupyter Notebook:
Example
import
sklearn
(
sklearn.version
)
If the scikit-learn version number is displayed successfully, similar to the following, the installation is successful:
1.5.2
Install other dependencies
scikit-learn depends on some other libraries, in particular:
- NumPy: for handling arrays and numerical computation
- SciPy: provides more advanced mathematical computation tools
- matplotlib(optional): for data visualization
- joblib(optional): for model persistence (saving and loading)
If you install with pip, scikit-learn will automatically install these dependencies, but if you want to install or update them manually, you can use the following commands:
pip install numpy scipy matplotlib joblib
If you install with conda, all dependency libraries will be installed automatically.