新建
8个不易发现的 Notebook 使用技巧
刀刀
推荐镜像 :Basic Image:bohrium-notebook:2023-04-07
推荐机型 :c2_m4_cpu
赞 36
17
目录
8 个不易发现的 Notebook 使用技巧
代码
文本
©️ Copyright 2023 @ Authors
作者:
刀刀 📨
日期:2023-08-01
共享协议:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
代码
文本
Bohrium Notebook 的交互式开发环境由开源项目 Jupyter 提供,Jupyter 有许多有用但可能不那么显而易见的功能。本文将介绍一些实用且不易发现的功能,并通过示例来演示它们的用法。
代码
文本
1. 快捷键
按 ESC
键可以进入命令模式,在命令模式下,Jupyter 提供了许多快捷键来提高工作效率。
- A:在当前单元格上方插入新单元格
- B:在当前单元格下方插入新单元格
- DD:删除当前单元格
- M:将单元格转换为Markdown格式
- Y:将单元格转换为代码格式
- Shift + Enter:运行当前单元格并移动到下一个单元格
代码
文本
还有一些不一定用得到、但一旦需要会非常方便的快捷键:
- Shift + J 或 Shift + Down:向下选择多个单元格
- Shift + K 或 Shift + Up:向上选择多个单元格
- Shift + M:合并选中的多个单元格
代码
文本
2. 魔术命令
魔术命令是以百分号%
开头的特殊命令,可以实现一些有用的功能。
代码
文本
代码
文本
[1]
%run hello.py
Hello, World!
代码
文本
示例2:使用%timeit评估代码执行时间
在Jupyter Notebook中运行以下代码,即可在输出中得到代码的执行时间:
代码
文本
[2]
import numpy as np
%timeit np.random.randn(1000, 1000)
29.8 ms ± 1.38 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
代码
文本
3. 使用?查看帮助
在函数、方法或对象后面加一个?
,然后运行单元格,可以查看关于它的帮助文档。
示例:查看numpy的帮助
在 Notebook 中依次运行以下代码,即可查看 numpy 的帮助文档:
代码
文本
[3]
import numpy as np
代码
文本
[4]
?np
Type: module String form: <module 'numpy' from '/opt/conda/lib/python3.8/site-packages/numpy/__init__.py'> File: /opt/conda/lib/python3.8/site-packages/numpy/__init__.py Docstring: NumPy ===== Provides 1. An array object of arbitrary homogeneous items 2. Fast mathematical operations over arrays 3. Linear Algebra, Fourier Transforms, Random Number Generation How to use the documentation ---------------------------- Documentation is available in two forms: docstrings provided with the code, and a loose standing reference guide, available from `the NumPy homepage <https://www.scipy.org>`_. We recommend exploring the docstrings using `IPython <https://ipython.org>`_, an advanced Python shell with TAB-completion and introspection capabilities. See below for further instructions. The docstring examples assume that `numpy` has been imported as `np`:: >>> import numpy as np Code snippets are indicated by three greater-than signs:: >>> x = 42 >>> x = x + 1 Use the built-in ``help`` function to view a function's docstring:: >>> help(np.sort) ... # doctest: +SKIP For some objects, ``np.info(obj)`` may provide additional help. This is particularly true if you see the line "Help on ufunc object:" at the top of the help() page. Ufuncs are implemented in C, not Python, for speed. The native Python help() does not know how to view their help, but our np.info() function does. To search for documents containing a keyword, do:: >>> np.lookfor('keyword') ... # doctest: +SKIP General-purpose documents like a glossary and help on the basic concepts of numpy are available under the ``doc`` sub-module:: >>> from numpy import doc >>> help(doc) ... # doctest: +SKIP Available subpackages --------------------- doc Topical documentation on broadcasting, indexing, etc. lib Basic functions used by several sub-packages. random Core Random Tools linalg Core Linear Algebra Tools fft Core FFT routines polynomial Polynomial tools testing NumPy testing tools f2py Fortran to Python Interface Generator. distutils Enhancements to distutils with support for Fortran compilers support and more. Utilities --------- test Run numpy unittests show_config Show numpy build configuration dual Overwrite certain functions with high-performance SciPy tools. Note: `numpy.dual` is deprecated. Use the functions from NumPy or Scipy directly instead of importing them from `numpy.dual`. matlib Make everything matrices. __version__ NumPy version string Viewing documentation using IPython ----------------------------------- Start IPython with the NumPy profile (``ipython -p numpy``), which will import `numpy` under the alias `np`. Then, use the ``cpaste`` command to paste examples into the shell. To see which functions are available in `numpy`, type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use ``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow down the list. To view the docstring for a function, use ``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view the source code). Copies vs. in-place operation ----------------------------- Most of the functions in `numpy` return a copy of the array argument (e.g., `np.sort`). In-place versions of these functions are often available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``. Exceptions to this rule are documented.
代码
文本
4. 查看已安装的内核
运行!jupyter kernelspec list
可以查看已安装的内核。
代码
文本
[5]
!jupyter kernelspec list
Available kernels: python3 /opt/conda/share/jupyter/kernels/python3
代码
文本
5. 多行编辑
按住Alt
的同时拖动鼠标,即可实现多行同时编辑:
代码
文本
6. 查看变量
使用魔术命令%whos
可以查看当前 Notebook 中所有的变量:
示例:
代码
文本
[6]
a = 1
b = 2
c = 3
%whos
Variable Type Data/Info -------------------------------- a int 1 b int 2 c int 3 hello function <function hello at 0x7ff6b0630310> np module <module 'numpy' from '/op<...>kages/numpy/__init__.py'>
代码
文本
7. 在同一行显示多个输出
在同一行使用多个display()
函数可以在同一行显示多个输出。
示例:
代码
文本
[7]
from IPython.display import display, Math
display(Math('1+1=2'))
display(Math('2+2=4'))
代码
文本
利用好这些功能,有助于提高编写和运行 Notebook 的效率。你知道哪些 Notebook 不易发现的使用技巧吗?欢迎在评论中交流。
代码
文本
已赞36
本文被以下合集收录
计算材料学学习材料合集
Letian
更新于 2024-09-07
20 篇36 人关注
测试合集文章列表100篇
xingyanshi@dp.tech很长的名字xingyanshi@dp.tech很长的名字很长的
更新于 2024-08-04
104 篇2 人关注
推荐阅读
公开
GROMACS水分子模拟Letian
更新于 2024-09-12
3 赞9 转存文件
公开
Jupyter_Dock | 分子对接liyq@dp.tech
发布于 2023-06-15
1 赞9 转存文件
评论
cool hot hot
MileAway
AnguseZhang