Python虚拟环境指南2023版

3,365 阅读4分钟

Python在数据科学、机器学习领域也是重要利器(yyds)。这是一篇Python环境指南,告诉你如何配置Python环境,并安装相关的库,进行数据科学和机器学习研究。

在数据科学和机器学习领域,我们可以使用Python的标准环境,也可以使用Anaconda。因为Anaconda不仅仅支持Python语言,还支持其它的数据科学工具,比如Matlab,R语言,Fortran语言。Anaconda同时也是一个工具库的分发平台,可以从中下载和安装库。在Anaconda环境中,我们可以使用 conda 命令进行库的安装。如果我们不需要其它语言,我们可以使用其简洁版本Miniconda。另外数据科学家更习惯使用Jupyter进行研究,Jupyter是一个Web化的开发工具,可以单步交互式的执行称为Notebook的代码。JupyterLab则是下一代的Jupter。以上是我们在开始之前,需要了解的基础概念, 总结下来就是下面一张表:

名称描述
Anaconda/Miniconda一种数据科学环境和开发平台,可以理解为Python的PyPi源。
condaAnaconda的命令行工具,可以理解为Pip命令
Jupyter/JupterLab一种Web化开发工具
Notebook混合了代码,注释文档及执行结果的文件

jupyter的界面大概是这样: jupyter

大家也可以直接体验jupyter.org/try-jupyter…

环境安装

可以使用下面命令安装miniconda:

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh | sh

安装完成miniconda后可以这样创建和使用虚拟环境:

# Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
# If you want to install from conda-forge
conda config --env --add channels conda-forge
# The actual install command
conda install numpy

这使用起来和Python的虚拟环境类似:

python3 -m venv .venv
source .venv/bin/activate
pip install numpy

当然,你可以在conda环境中直接使用pip:

(my-env) [game404@y ~]$ pip list
Package    Version
---------- -------
numpy      1.24.1
pip        22.3.1
setuptools 65.6.3
wheel      0.38.4

我们可以使用下面两个命令之一安装jupyterlab:

conda install jupyterlab
或者
pip install jupyterlab
  • conda命令是从anaconda源安装;pip是用PyPi源安装,两个命令异曲同工,就看谁网速快

启动jupyter-lab

安装完jupyter-lab后,可以使用下面命令打开它:

(my-env) [game404@y ~]$jupyter-lab
[I 2023-01-08 21:26:50.250 ServerApp] jupyter_server_terminals | extension was successfully linked.
[I 2023-01-08 21:26:50.257 ServerApp] jupyterlab | extension was successfully linked.
[I 2023-01-08 21:26:50.262 ServerApp] nbclassic | extension was successfully linked.
...
[I 2023-01-08 21:26:50.842 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 2023-01-08 21:26:50.847 ServerApp] No web browser found: could not locate runnable browser.
[C 2023-01-08 21:26:50.848 ServerApp]

    To access the server, open this file in a browser:
        file:///home/yuanzuxiang/.local/share/jupyter/runtime/jpserver-3540-open.html
    Or copy and paste one of these URLs:
        http://localhost:8889/lab?token=f5028b1978baa74512cec56cff7c4f9e2dbbc4592cdf5b69
     or http://127.0.0.1:8889/lab?token=f5028b1978baa74512cec56cff7c4f9e2dbbc4592cdf5b69
  • 注意这里的token,是权限访问的token, 初次访问首页需要使用

然后我们通过浏览器访问jupyter-lab,创建Notebook,直接测试python环境:

  • 红色的Notebook Icon和引导界面一致
  • 使用顶部工具栏【->】执行代码
  • notebook主要是可以按照cell执行代码

安装常用库

安装好Python环境和Jupyter-lab工具后,接下来我们开始安装常用库,主要涉及下面7个库:

  • numpy The fundamental package for scientific computing with Python
  • pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.
  • matplotlib Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.
  • seaborn is a Python data visualization library based on matplotlib.
  • scipy Fundamental algorithms for scientific computing in Python
  • statsmodels statistical models, hypothesis tests, and data exploration
  • sklearn Machine Learning in Python

这些库互相也有依赖关系,numpy是最基础的矩阵实现,pandas是最核心的数据表操作,seaborn又基于matplotlib,它们负责数据可视化,scipy和statsmodels提供一些统计方法,sklearn进行机器学习和线性回归。我们可以按照这样的顺序去安装:

conda install numpy
conda install pandas
conda install matplotlib
conda install seaborn
conda install scipy
conda install statsmodels
conda install scikit-learn

也可以直接使用pip命令安装:

pip install numpy
pip install pandas
pip install matplotlib
pip install seaborn
pip install scipy
pip install statsmodels
pip install scikit-learn

一般我们这样导入它们:

import numpy as np
import pandas as pd

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.objects as so

from scipy import stats
from sklearn import linear_model
import statsmodels.api as sm

jupyter-lab中也可以使用pip命令安装库:

  • 注意前面的 ! 是必须的

参考链接