2026年4月27日 研究日志¶

今日主题:整理目前收集到的样本,绘制样本统计表。本周的主要内容就是这个,目前暂未成型,本周五再进行展示吧。今天来总结一下Jupyter的magic吧!

先和不熟悉的朋友讲一声,Jupyter 的 Magic 指令(Magics)是 IPython 提供的“增强语法”,用于执行系统命令、计时、调试、运行脚本、管理环境等。
它们不是 Python 语法,而是 Notebook 环境的“扩展能力”。所以它们是只有jupyter notebook和相当于进化版的jupyter lab环境中可以使用的魔法哦!

Magic 分两类:

  • 行魔法(Line Magics):以 % 开头,只作用于当前行
  • 单元魔法(Cell Magics):以 %% 开头,作用于整个 cell

常用 Magic 指令与示例¶

1. %time — 计时单行代码¶

用于测量一行代码的执行时间。

2. %%time — 计时整个 cell¶

用于测量整个 cell的执行时间。

In [1]:
%time sum([i*i for i in range(10_0000)])
CPU times: total: 15.6 ms
Wall time: 7.55 ms
Out[1]:
333328333350000
In [2]:
%%time
total = 0
for i in range(10_0000):
    total += i*i
total
CPU times: total: 15.6 ms
Wall time: 12.3 ms
Out[2]:
333328333350000

3. %timeit — 多次运行取平均¶

适合测量短代码的平均性能。

4. %%timeit — 多次运行整个 cell¶

对整个 cell 做重复计时。

In [3]:
%timeit [i*i for i in range(1000)]
36.1 μs ± 4.73 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [4]:
%%timeit
x = []
for i in range(1000):
    x.append(i*i)
36.8 μs ± 11.5 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

5. %pwd — 查看当前工作目录¶

In [5]:
%pwd
Out[5]:
'C:\\Users\\Admin\\TechBlog'

6. %cd — 切换目录¶

In [10]:
%cd ./data
C:\Users\Admin\TechBlog\data

7. %ls — 列出当前目录文件¶

In [11]:
%ls
 Volume in drive C has no label.
 Volume Serial Number is 12B8-C7B5

 Directory of C:\Users\Admin\TechBlog\data

27/04/2026  17:08    <DIR>          .
27/04/2026  17:08    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  518,734,532,608 bytes free

8. ! — 执行系统命令¶

在 Notebook 中运行 shell 命令。 (其实!不是 Magic,但也是IPython的机制,和magic一样用,就放在这里了)

In [1]:
!pip list
Package                           Version
--------------------------------- --------------------
accelerate                        1.13.0
aiobotocore                       2.25.0
aiodns                            3.5.0
aiofiles                          24.1.0
aiohappyeyeballs                  2.6.1
aiohttp                           3.13.2
aioitertools                      0.12.0
aiosignal                         1.4.0
alabaster                         0.7.16
altair                            5.5.0
anaconda-anon-usage               0.7.1
anaconda-auth                     0.8.6
anaconda-catalogs                 0.2.0
anaconda-cli-base                 0.6.0
anaconda-client                   1.13.0
anaconda-navigator                2.6.6
anaconda-project                  0.11.1
annotated-doc                     0.0.4
annotated-types                   0.6.0
anyio                             4.10.0
appdirs                           1.4.4
archspec                          0.2.5
argon2-cffi                       21.3.0
argon2-cffi-bindings              25.1.0
arrow                             1.4.0
astroid                           3.3.11
astropy                           7.1.1
astropy-iers-data                 0.2025.11.10.0.38.31
asttokens                         3.0.0
async-lru                         2.0.5
asyncssh                          2.17.0
atomicwrites                      1.4.0
attrs                             25.4.0
audioop-lts                       0.2.2
Automat                           24.8.1
autopep8                          2.0.4
babel                             2.16.0
bcrypt                            5.0.0
beautifulsoup4                    4.13.5
binaryornot                       0.4.4
biopython                         1.86
black                             25.9.0
bleach                            6.2.0
blinker                           1.9.0
bokeh                             3.8.0
boltons                           25.0.0
botocore                          1.40.46
Bottleneck                        1.4.2
brotli                            1.2.0
brotlicffi                        1.1.0.0
cachetools                        5.5.1
certifi                           2025.10.5
cffi                              2.0.0
chardet                           5.2.0
charset-normalizer                3.4.4
click                             8.1.8
cloudpickle                       3.1.1
colorama                          0.4.6
colorcet                          3.1.0
colored                           2.3.2
comm                              0.2.1
conda                             25.9.1
conda-anaconda-telemetry          0.3.0
conda-anaconda-tos                0.2.2
conda-build                       25.9.0
conda-content-trust               0.2.0
conda_index                       0.7.0
conda-libmamba-solver             25.4.0
conda-pack                        0.7.1
conda-package-handling            2.4.0
conda_package_streaming           0.11.0
conda-repo-cli                    1.0.173
conda-token                       0.6.0
constantly                        23.10.4
contourpy                         1.3.1
cookiecutter                      2.6.0
cryptography                      46.0.3
cssselect                         1.2.0
cycler                            0.11.0
dask                              2025.10.0
datashader                        0.18.2
debugpy                           1.8.16
decorator                         5.2.1
defusedxml                        0.7.1
diff-match-patch                  20200713
diffusers                         0.38.0.dev0
dill                              0.4.0
distributed                       2025.10.0
distro                            1.9.0
docstring-to-markdown             0.17
docutils                          0.21.2
et_xmlfile                        2.0.0
evalidate                         2.0.3
executing                         2.2.1
fastapi                           0.135.1
fastjsonschema                    2.20.0
ffmpy                             1.0.0
filelock                          3.20.0
flake8                            7.1.1
Flask                             3.1.2
fonttools                         4.60.1
fqdn                              1.5.1
frozendict                        2.4.6
frozenlist                        1.8.0
fsspec                            2025.7.0
gitdb                             4.0.12
GitPython                         3.1.45
gmpy2                             2.2.1
gradio                            6.8.0
gradio_client                     2.2.0
greenlet                          3.2.4
groovy                            0.1.2
h11                               0.16.0
h5py                              3.14.0
HeapDict                          1.0.1
hf-xet                            1.3.2
holoviews                         1.21.0
httpcore                          1.0.9
httpx                             0.28.1
huggingface_hub                   1.5.0
hvplot                            0.12.1
hyperlink                         21.0.0
idna                              3.11
ImageIO                           2.37.2
imagesize                         1.4.1
imbalanced-learn                  0.14.0
importlib_metadata                8.5.0
incremental                       24.7.2
inflection                        0.5.1
iniconfig                         2.1.0
intake                            2.0.8
intervaltree                      3.1.0
ipykernel                         6.31.0
ipython                           8.30.0
ipython-genutils                  0.2.0
ipywidgets                        8.1.7
isoduration                       20.11.0
isort                             6.1.0
itemadapter                       0.12.1
itemloaders                       1.3.2
itsdangerous                      2.2.0
jaraco.classes                    3.4.0
jaraco.context                    0.0.0
jaraco.functools                  4.1.0
jedi                              0.19.2
jellyfish                         1.1.3
Jinja2                            3.1.6
jiter                             0.14.0
jmespath                          1.0.1
joblib                            1.5.2
json5                             0.9.25
jsonpatch                         1.33
jsonpointer                       3.0.0
jsonschema                        4.25.0
jsonschema-specifications         2025.9.1
jupyter                           1.1.1
jupyter_client                    8.6.3
jupyter-console                   6.6.3
jupyter_contrib_core              0.4.2
jupyter_core                      5.8.1
jupyter-events                    0.12.0
jupyter-highlight-selected-word   0.2.0
jupyter-lsp                       2.2.5
jupyter_server                    2.16.0
jupyter_server_terminals          0.5.3
jupyterlab                        4.3.4
jupyterlab_pygments               0.3.0
jupyterlab_server                 2.28.0
jupyterlab_widgets                3.0.15
keyring                           25.6.0
kiwisolver                        1.4.8
lark                              1.3.1
lazy_loader                       0.4
lckr_jupyterlab_variableinspector 3.2.4
Levenshtein                       0.27.3
libarchive-c                      5.1
libmambapy                        2.3.2
lief                              0.16.4
linkify-it-py                     2.0.3
llvmlite                          0.45.1
lmdb                              1.6.2
locket                            1.0.0
lxml                              5.3.0
lz4                               4.4.5
Markdown                          3.8
markdown-it-py                    2.2.0
MarkupSafe                        3.0.2
matlabengine                      26.1
matplotlib                        3.10.6
matplotlib-inline                 0.2.1
mccabe                            0.7.0
mdit-py-plugins                   0.5.0
mdurl                             0.1.2
menuinst                          2.4.1
metakernel                        1.0.0
mistune                           3.1.2
mkl_fft                           2.1.1
mkl_random                        1.3.0
mkl-service                       2.5.2
more-itertools                    10.8.0
mpi4py                            4.0.3
mpmath                            1.3.0
msgpack                           1.1.1
multidict                         6.7.0
multipledispatch                  1.0.0
mypy                              1.17.1
mypy_extensions                   1.0.0
narwhals                          2.7.0
navigator-updater                 0.5.1
nbclient                          0.10.2
nbconvert                         7.16.6
nbformat                          5.10.4
nest_asyncio                      1.6.0
networkx                          3.5
nltk                              3.9.2
notebook                          7.3.2
notebook_shim                     0.2.4
numba                             0.62.1
numexpr                           2.14.1
numpy                             2.3.4
numpydoc                          1.9.0
openai                            2.32.0
openpyxl                          3.1.5
orjson                            3.11.7
overrides                         7.7.0
packaging                         25.0
pandas                            2.3.3
pandocfilters                     1.5.1
panel                             1.8.3
param                             2.2.1
parsel                            1.10.0
parso                             0.8.5
partd                             1.4.2
pathspec                          0.12.1
patsy                             1.0.1
pexpect                           4.9.0
pickleshare                       0.7.5
pillow                            12.0.0
pip                               25.1
pkce                              1.0.3
pkginfo                           1.12.0
platformdirs                      4.5.0
plotly                            6.3.0
pluggy                            1.5.0
ply                               3.11
portalocker                       3.2.0
prometheus_client                 0.21.1
prompt_toolkit                    3.0.52
propcache                         0.3.1
Protego                           0.4.0
protobuf                          5.29.3
psutil                            7.0.0
ptyprocess                        0.7.0
pure_eval                         0.2.3
py-cpuinfo                        9.0.0
pyarrow                           21.0.0
pyasn1                            0.6.1
pyasn1_modules                    0.4.2
pycares                           4.10.0
pycodestyle                       2.12.1
pycosat                           0.6.6
pycparser                         2.23
pyct                              0.6.0
pycurl                            7.45.7
pydantic                          2.12.3
pydantic_core                     2.41.4
pydantic-settings                 2.10.1
PyDispatcher                      2.0.7
pydocstyle                        6.3.0
pydub                             0.25.1
pyerfa                            2.0.1.5
pyflakes                          3.2.0
PyGithub                          2.8.1
Pygments                          2.19.2
PyJWT                             2.10.1
pylint                            3.3.8
pylint-venv                       3.0.3
pyls-spyder                       0.4.0
PyNaCl                            1.6.0
pyodbc                            5.3.0
pyOpenSSL                         25.3.0
pyparsing                         3.2.0
PyQt5                             5.15.10
PyQt5_sip                         12.13.0
PyQtWebEngine                     5.15.6
PySocks                           1.7.1
pytest                            8.4.2
python-dateutil                   2.9.0.post0
python-dotenv                     1.1.0
python-json-logger                3.2.1
python-Levenshtein                0.27.3
python-lsp-black                  2.0.0
python-lsp-jsonrpc                1.1.2
python-lsp-server                 1.12.2
python-multipart                  0.0.22
python-slugify                    8.0.4
pytokens                          0.2.0
pytoolconfig                      1.2.6
pytz                              2025.2
pyuca                             1.2
pyviz_comms                       3.0.6
PyWavelets                        1.9.0
pywin32                           311
pywin32-ctypes                    0.2.2
pywinpty                          2.0.15
PyYAML                            6.0.2
pyzmq                             27.1.0
QDarkStyle                        3.2.3
qstylizer                         0.2.2
QtAwesome                         1.4.0
qtconsole                         5.6.1
QtPy                              2.4.3
quast                             5.2.0
queuelib                          1.8.0
RapidFuzz                         3.14.3
readchar                          4.2.1
referencing                       0.37.0
regex                             2026.4.4
requests                          2.32.5
requests-file                     2.1.0
requests-toolbelt                 1.0.0
rfc3339_validator                 0.1.4
rfc3986_validator                 0.1.1
rfc3987-syntax                    1.1.0
rich                              14.2.0
roman-numerals-py                 3.1.0
rope                              1.14.0
rpds-py                           0.28.0
rpy2                              3.6.7
rpy2-rinterface                   3.6.6
rpy2-robjects                     3.6.5
rtree                             1.4.1
ruamel.yaml                       0.18.16
ruamel.yaml.clib                  0.2.14
ruamel_yaml_conda                 0.17.21
s3fs                              2025.7.0
safehttpx                         0.1.7
safetensors                       0.8.0rc0
scikit-image                      0.25.2
scikit-learn                      1.7.2
scipy                             1.16.3
Scrapy                            2.13.3
seaborn                           0.13.2
semantic-version                  2.10.0
semver                            3.0.4
Send2Trash                        1.8.2
service-identity                  24.2.0
setuptools                        72.1.0
shellingham                       1.5.4
simplejson                        3.20.2
sip                               6.7.12
six                               1.17.0
smmap                             4.0.0
sniffio                           1.3.0
snowballstemmer                   3.0.1
sortedcontainers                  2.4.0
soupsieve                         2.5
Sphinx                            8.2.3
sphinxcontrib-applehelp           2.0.0
sphinxcontrib-devhelp             2.0.0
sphinxcontrib-htmlhelp            2.1.0
sphinxcontrib-jsmath              1.0.1
sphinxcontrib-qthelp              2.0.0
sphinxcontrib-serializinghtml     2.0.0
spyder                            6.0.7
spyder-kernels                    3.0.5
SQLAlchemy                        2.0.43
stack_data                        0.6.3
starlette                         0.52.1
statsmodels                       0.14.5
streamlit                         1.51.0
superqt                           0.7.6
sympy                             1.14.0
tables                            3.10.2
tabulate                          0.9.0
tblib                             3.1.0
tenacity                          9.1.2
terminado                         0.18.1
text-unidecode                    1.3
textdistance                      4.6.3
threadpoolctl                     3.5.0
three-merge                       0.1.1
tifffile                          2025.10.4
tinycss2                          1.4.0
tldextract                        5.1.2
tokenizers                        0.22.2
toml                              0.10.2
tomli                             2.2.1
tomlkit                           0.13.3
toolz                             1.0.0
torch                             2.11.0
torchvision                       0.24.1+cu130
tornado                           6.5.1
tqdm                              4.67.1
traitlets                         5.14.3
transformers                      5.6.0
truststore                        0.10.1
Twisted                           25.5.0
twisted-iocpsupport               1.0.2
typer                             0.17.4
typing_extensions                 4.15.0
typing-inspection                 0.4.2
tzdata                            2025.2
tzlocal                           5.3.1
uc-micro-py                       1.0.3
ujson                             5.10.0
uri-template                      1.3.0
urllib3                           2.5.0
uvicorn                           0.41.0
w3lib                             2.1.2
watchdog                          6.0.0
wcwidth                           0.2.13
webcolors                         25.10.0
webencodings                      0.5.1
websocket-client                  1.8.0
Werkzeug                          3.1.3
whatthepatch                      1.0.7
wheel                             0.45.1
widgetsnbextension                4.0.14
win_inet_pton                     1.1.0
winloop                           0.3.1
wrapt                             1.17.0
xarray                            2025.10.1
xformers                          0.0.35
xlwings                           0.33.15
xyzservices                       2025.4.0
yapf                              0.43.0
yarl                              1.22.0
zict                              3.0.0
zipp                              3.21.0
zope.interface                    8.0.1
zstandard                         0.23.0

9. %%writefile — 将 cell 内容写入文件¶

In [14]:
%%writefile hello.py
print("Hello, file!")
Writing hello.py

10. %run — 运行 Python 脚本¶

In [15]:
%run hello.py
Hello, file!

11. %%cmd — 在 cell 中书写并运行 cmd 脚本¶

怎么?我用Windows作为我的主要编程环境不行吗?需要用到Linux系统的场合我都是靠WSL解决的(在Linux中这个命令是%%bash,我用Windows所以是这个)

In [18]:
%%cmd
echo Hello from Windows CMD
dir
Microsoft Windows [Version 10.0.26200.8246]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Admin\TechBlog\data>echo Hello from Windows CMD
Hello from Windows CMD

C:\Users\Admin\TechBlog\data>dir
 Volume in drive C has no label.
 Volume Serial Number is 12B8-C7B5

 Directory of C:\Users\Admin\TechBlog\data

27/04/2026  17:21    <DIR>          .
27/04/2026  17:25    <DIR>          ..
27/04/2026  17:21    <DIR>          .ipynb_checkpoints
27/04/2026  17:16                23 hello.py
               1 File(s)             23 bytes
               3 Dir(s)  518,740,402,176 bytes free

C:\Users\Admin\TechBlog\data>

12. %matplotlib inline — 在 Notebook 内显示图像¶

这个是Python kernel 专用的,R kernel之类的没法用。(不过一般情况下,除了我这个用jupyter lab写博客的,也没什么人会用R kernel的notebook了吧)

In [19]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

# 图像大小
width, height = 800, 600

# 生成复平面网格
x = np.linspace(-2.0, 1.0, width)
y = np.linspace(-1.2, 1.2, height)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y

# 初始化
Z = np.zeros_like(C)
iterations = np.zeros(C.shape, dtype=int)

max_iter = 100

# 迭代计算 Mandelbrot 集合
for i in range(max_iter):
    mask = np.abs(Z) <= 2
    Z[mask] = Z[mask] * Z[mask] + C[mask]
    iterations[mask] = i

# 绘图
plt.figure(figsize=(10, 7))
plt.imshow(iterations, cmap="magma", extent=[-2, 1, -1.2, 1.2])
plt.colorbar(label="Iterations")
plt.title("Mandelbrot Fractal (Matplotlib)")
plt.xlabel("Re")
plt.ylabel("Im")
plt.show()
No description has been provided for this image

📊 Magic 常用指令功能汇总表¶

(执行系统命令的指令也算进来了,反正都一样用,都一样都一样)

指令 类型 功能
%time 行魔法 测量单行代码执行时间
%%time 单元魔法 测量整个 cell 执行时间
%timeit 行魔法 多次运行取平均时间
%%timeit 单元魔法 多次运行整个 cell
%pwd 行魔法 显示当前工作目录
%ls 行魔法 列出目录内容
%cd 行魔法 切换目录
!command Shell 执行系统命令
%run 行魔法 运行 Python 脚本
%%bash 单元魔法 在 cell 中运行 bash
%matplotlib inline 行魔法 Notebook 内显示图像
%%writefile 单元魔法 将 cell 内容写入文件

要是不嫌麻烦,用下面这个指令可以查看所有magic指令

In [13]:
%lsmagic
Out[13]:
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %code_wrap  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %mamba  %matplotlib  %micromamba  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %uv  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%code_wrap  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.