

Jupyter Notebook 十个提升体验的高级技巧
source link: https://www.51cto.com/article/755617.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Jupyter Notebook 十个提升体验的高级技巧
Jupyter 笔记本是数据科学家和分析师用于交互式计算、数据可视化和协作的工具。Jupyter 笔记本的基本功能大家都已经很熟悉了,但还有一些鲜为人知的技巧可以大大提高生产力和效率。在这篇文章中,我将介绍10个可以提升体验的高级技巧。
Jupyter 笔记本是数据科学家和分析师用于交互式计算、数据可视化和协作的工具。Jupyter 笔记本的基本功能大家都已经很熟悉了,但还有一些鲜为人知的技巧可以大大提高生产力和效率。在这篇文章中,我将介绍10个可以提升体验的高级技巧。
改变注释的颜色
颜色使事物脱颖而出。我们可以使用不同的颜色来突出需要突出的重要内容。所以我们可以使用html来对我们的文本进行高亮显示,有4种类型可以直接使用:
<div class="alert alert-block alert-info">
<b>Tip:</b> Use blue boxes (alert-info) for tips and notes.
</div>
Warning
<div class="alert alert-block alert-warning">
Warning: Use Yellow for a warning that might need attention.
</div>
Success
<div class="alert alert-block alert-success">
Green box can be used to show some positive such as the successful execution of a test or code.
</div>
Danger
<div class="alert alert-block alert-danger">
Red boxes can be used to alert users to not delete some important part of code etc.
</div>
代码折叠(隐藏代码单元)
代码太多的话会影响我们查看Notebook 的内容中,如果只想显示结果/图表,可以将以下 HTML 代码粘贴到笔记本的顶部单元格中,然后运行该单元格。
%%html
<style id=hide>div.input{display:none;}</style>
<button type="button"
notallow="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">
Show inputs</button>
结果如下:

Jupyter notebooks 包含很多的“魔法命令”,可以使用“%timeit”魔术命令:

交互式部件
Notebook 支持交互式部件,可以为数据分析构建自定义用户界面。例如可以创建一个滑块小部件来调整代码中的参数:
from ipywidgets import interact
@interact(x=(0, 10))
def square(x):
print(x**2)
结果如下:

可以使用反引号 (`) 在 Jupyter Notebook 的 Markdown 单元格中嵌入代码。
`x = 10`
```Python
str = "This is block level code"
print(str)
```

转换成PPT
Jupyter 笔记本可以使用“rise”扩展转换为交互式PPT。首先要安装扩展,请运行以下命令:
!pip install RISE
!jupyter-nbextension install rise --py --sys-prefix
!jupyter-nbextension enable rise --py --sys-prefix
安装后,转到“View ”并选择“Slideshow ”(如果没有看到此选项,请刷新)。这样就可以为每个单元格选择幻灯片类型。

幻灯片类型可以是以下类型之一:
Slide — 幻灯片的基本类型。
Sub-slide——“Slide ”的片段。
Fragment —幻灯片上的信息。
Skip — 在演示过程中跳过此单元格。
Notes ——演讲者视图中出现的,类似提词器。
结果如下:

自定义 Matplotlib 图
Jupyter Notebook 默认使用 Matplotlib 进行数据可视化,所以我们可以设置一些默认的参数,例如可以使用“rcParams”字典更改绘图标签的字体大小,这会将设置应用于所有 matplotlib 绘图:
import matplotlib.pyplot as plt
## setting global settings
plt.rcParams.update({'font.size': 10,'lines.linewidth': 3})

自定义主题
Jupyter Notebooks 带有默认主题,我们可以通过安装和应用自定义主题自定义外观。要安装主题,可以使用 jupyterthemes 包:
!pip install jupyterthemes
列出所有可用的主题:
!jt -l
然后,可以使用命令来安装主题,例如:
!jt -t grade3
安装完后,需要重启 Jupyter notebook。

如果想重置默认主题,可以使用以下命令(记得重启):
!jt -r
LaTeX 支持
如果需要包含数学方程式,您可以在 IPython 的显示模块中使用 LaTeX 语法。
例如,以下代码将显示 2 个分数相加的数学表示及其输出。
from IPython.display import display, Math
display(Math('\\frac{2}{3} + \\frac{4}{5} = \\frac{22}{15}'))
单元格中显示多个输出
Jupyter Notebook 中工作时,默认仅显示单元格中的最后一个输出。但是可以使用 IPython.core.interactiveshell 中的 InteractiveShell 模块在单个单元格中显示多个输出。
要显示单元格的所有输出,可以在单元格的开头使用以下代码:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

但是记住,这样如果变量多的话会很乱
本文总结了一些充分利用 Jupyter Notebooks 的技巧。这些技巧可以帮助您简化工作流程并提高工作效率。无论是数据分析师、数据科学家还是机器学习工程师,Jupyter Notebooks 都可以成为工作的强大工具。通过掌握这些提技巧,可以将数据分析提升到一个新的水平,并使你的工作更加高效。
Recommend
-
171
yunabe/lgo master ...
-
160
Page not found – 不正經的社長 Skip to content
-
93
mapboxgl-jupyter - Use Mapbox GL JS to visualize data in a Python Jupyter notebook
-
131
README.rst EIN -- Emacs IPython Notebook
-
93
GitHub is where people build software. More than 27 million people use GitHub to discover, fork, and contribute to over 80 million projects.
-
84
README.md Ansible Jupyter Kernel
-
82
README.md fast_progress A fast and simple progress bar for Jupyter Notebook and console. Created by Sylvain Gugger for fast.ai. Copyright 2017 o...
-
7
Adicionando kernels do C# e F# no Jupyter Notebook/Lab. (#csharp #fsharp #dotnet #jupyter) Posted on
-
6
十个高级 TypeScript 开发技巧 作者:杨小爱 2022-11-07 16:06:15 在使用了一段时间的 Typescript 之后,我深深地感受到了 Typescript 在大中型项目中的必要性。 可以提前避免很多编译期的bug,比如烦人的拼写问题...
-
4
提升 Java 应用程序的十个优化技巧 作者:迷路的架构师 2023-03-09 16:42:00 程序性能优化是一个复杂的话题。往往需要结合具体场景进行性能分析,找出瓶颈提出优化建议。但是,假设我们平时很少关注细节的性能,那...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK