让我们从安装 Python 模块 Scikit-learn 开始,它是 Python 中最好且文档最完善的机器学习库之一。

为了开始我们的编码项目,请激活你的 Python 3 编程环境。确保你位于环境所在的目录中,并运行以下命令:

Bash
. my_env/bin/activate

激活编程环境后,检查 Scikit-learn 模块是否已安装:

Bash
(my_env) $ python -c "import sklearn"

如果 sklearn 已安装,此命令将成功完成,不会出现错误。如果未安装,你将看到以下错误消息:

Output
Traceback (most recent call last): File "<string>", line 1, in <module>
ImportError: No module named 'sklearn'

错误消息表明 sklearn 未安装,因此使用 pip 下载该库:

Bash
(my_env) $ pip install scikit-learn[alldeps]

安装完成后,启动 Jupyter Notebook:

Bash
(my_env) $ jupyter notebook

在 Jupyter 中,创建一个名为 ML Tutorial 的新 Python Notebook。在 Notebook 的第一个单元格中,导入 sklearn 模块:

ML Tutorial

Python
import sklearn

你的 Notebook 应该如下图所示:


Jupyter Notebook,包含一个 Python 单元格,导入了 sklearn

现在我们已经在 Notebook 中导入了 sklearn,我们可以开始处理用于机器学习模型的数据集了。


Step 1 — Importing Scikit-learn
Let’s begin by installing the Python module Scikit-learn, one of the best
and most documented machine learning libraries for Python.
To begin our coding project, let’s activate our Python 3 programming
environment. Make sure you’re in the directory where your environment
is located, and run the following command:
. my_env/bin/activate
With our programming environment activated, check to see if the
Sckikit-learn module is already installed:
(my_env) $ python -c "import sklearn"
If sklearn is installed, this command will complete with no error. If it
is not installed, you will see the following error message:
Output
Traceback (most recent call last): File "<string>", line 1, in <module>
ImportError: No module named 'sklearn'
The error message indicates that sklearn
 is
 not installed, so
download the library using pip:
(my_env) $ pip install scikit-learn[alldeps]
Once the installation completes, launch Jupyter Notebook:
(my_env) $ jupyter notebook
In Jupyter, create a new Python Notebook called ML Tutorial. In the
first cell of the Notebook, import the sklearn module:
ML Tutorial
import sklearn
Your notebook should look like the following figure:
Jupyter Notebook with one Python cell, which imports sklearn
Now that we have sklearn imported in our notebook, we can begin
working with the dataset for our machine learning model.

最后修改: 2025年06月25日 星期三 11:32