关于Jupyter Notebook的9件事你不知道

As a Data Scientist, you will use jupyter notebook on an everyday basis. It is crucial that you get as familiar with this tool as possible.

作为数据科学家,您将每天使用jupyter笔记本。 尽可能熟悉此工具至关重要。

In this article, I will share some Jupyter notebook tips that I use when working with this tool. These tips will help you to get more familiar with the notebook itself and will boost your productivity.

在本文中,我将分享一些使用此工具时使用的Jupyter笔记本提示。 这些技巧将帮助您更熟悉笔记本电脑本身,并提高工作效率。

Let’s get started.

让我们开始吧。

1. You can run shell commands from jupyter notebook

1.您可以从jupyter notebook运行shell命令

Many of you will be familiar with running python code in Jupyter notebook and using markdown to create descriptions and text. Some may not know that you can run shell commands from the notebook itself.

你们中的许多人将熟悉在Jupyter笔记本中运行python代码并使用markdown创建描述和文本。 有些人可能不知道您可以从笔记本计算机本身运行shell命令。

To do it, you input the command in a code cell. The format of the command is the same as if it was in the shell but it is additionally proceeded by the exclamation mark (!).

为此,您需要在代码单元格中输入命令。 该命令的格式与它在外壳中的格式相同,但是它另外带有感叹号(!)。

For example, you can use install packages with pip.

例如,您可以将安装软件包与pip一起使用。

!pip install pandas

Or you can check what version of the package you have installed.

或者,您可以检查所安装软件包的版本。

!pip show pandas

You can also use standard shell command such as:

您还可以使用标准的shell命令,例如:

!pwd
!ls

All of these without opening the shell itself. This means no time lost by switching between notebook and terminal!

所有这些都无需打开外壳本身。 这意味着在笔记本电脑和终端之间切换不会浪费时间!

2. Jupyter notebook has magic commands.

2Jupyter笔记本具有魔术命令。

If you play with shell commands you will notice that some of them cannot execute. An example of this occurring is the ‘cd’ command. In order to confirm this, you can run the following code in jupyter notebook.

如果您使用shell命令,您会发现其中一些命令无法执行。 这种情况的一个示例是“ cd”命令。 为了确认这一点,您可以在jupyter notebook中运行以下代码。

!pwd
!cd <directory name>
!pwd

Note that the first and second ‘pwd’ command will yield the same path. The reason for this is the fact that these shell commands are executed in the temporary subshell.

请注意,第一个和第二个“ pwd”命令将产生相同的路径。 这样做的原因是这些Shell命令在临时子Shell中执行。

In order to effectively change the directory, you will need a magic command.

为了有效地更改目录,您将需要一个魔术命令。

%cd <directory>

Magic commands are special commands to help you with running and analyzing data in your notebook. They are either proceeded by % if they are on one line of code or by %% if they are written on several lines.

Magic命令是特殊命令,可帮助您运行和分析笔记本中的数据。 如果在一行代码中,则以%开头;如果在多行代码中,则以%%开头。

One of the most useful magic commands is a command that lists all magic commands.

最有用的魔术命令之一是列出所有魔术命令的命令。

%lsmagic
Image for post

Explaining all magic commands would require the whole new article so let’s limit ourselves to just a few examples.

解释所有魔术命令将需要整篇新文章,因此让我们仅举几个例子。

You can run python file from your jupyter notebook using:

您可以使用以下命令从jupyter笔记本运行python文件:

%run <file name>

Or you can time the execution of the cell with the following code.

或者,您可以使用以下代码来计时单元的执行时间。

%% time

<your code>

You probably have seen this magic command but did not realize what it was.

您可能已经看过这个神奇的命令,但没有意识到它是什么。

%matplotlib inline

The above is indeed a magic command that accompanies almost every notebook. It is used to render your graphs within the notebook itself.

上面的确实是几乎所有笔记本都随附的魔术命令。 它用于在笔记本本身中渲染图形。

3. You can use shortcuts to navigate faster through your notebook.

3.您可以使用快捷方式更快地浏览笔记本。

Have you been navigating thought the notebook using a graphical user interface?

您是否一直在使用图形用户界面浏览笔记本?

Stop!

停止!

It’s time to learn jupyter notebook shortcuts. You can use “Enter” and “Esc” to switch between command and edit mode.

是时候学习jupyter笔记本快捷方式了。 您可以使用“ Enter”和“ Esc”在命令和编辑模式之间切换。

Once you are in command mode you can use the following shortcuts:

进入命令模式后,可以使用以下快捷方式:

  • a add a new cell above the current one,

    添加高于当前一个新的小区,

  • b add a new cell below the current one,

    b在当前单元格下方添加一个新单元格,

  • d + d (d pressed twice) deleting current cell,

    d + d (按两次d)删除当前单元格,

  • m change cell from code to markdown,

    m将单元格从代码更改为降价,

  • y change cell from markdown to code,

    y将单元格从降价更改为代码,

  • up and down arrows use arrows to change the current cell,

    上下箭头使用箭头更改当前单元格,

  • shift + enter (pressed together) run code in the cell.

    Shift +在单元格中输入(同时按下)运行代码。

Still not sure how to do it? I have written an article in the past that explains it better.

仍然不确定该怎么做? 我过去写过一篇文章,对其进行了更好的解释。

4. You can suppress the output of a final function with a semicolon.

4.您可以使用分号禁止最终函数的输出。

If you have been working with Jupyter notebook for a while you probably have noticed that it automatically renders the output of the last function that was executed by the cell. This is usually the behavior that we want, but not in all cases!

如果您使用Jupyter笔记本已有一段时间,您可能会注意到它会自动呈现单元执行的最后一个函数的输出。 这通常是我们想要的行为,但并非在所有情况下都如此!

It is especially annoying when you render graphs in the notebook. Let’s see the example of plotting sine function.

在笔记本中渲染图形时特别令人讨厌。 让我们看一下绘制正弦函数的例子。

%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Data for plotting sin function
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

plt.plot(t, s);

If you run this in Jupyter notebook you will get an output similar to this:

如果您在Jupyter笔记本中运行此命令,则会得到类似于以下的输出:

Image for post

Note that I have marked in red the output of the plot. This info is usually not relevant for our analysis and we can suppress it by adding a semicolon to the last line of the code block.

请注意,我已将绘图输出标记为红色。 此信息通常与我们的分析无关,我们可以通过在代码块的最后一行添加分号来消除此信息。

plt.plot(t, s);

5. Jupyter notebook has built-in autocomplete functionality.

5. Jupyter笔记本电脑具有内置的自动完成功能。

Some of you may know this already, but for those who do not, this will be a very useful feature. In order to use it, you can start typing the code and press the “Tab” button in order to see the autocomplete suggestions.

你们中有些人可能已经知道这一点,但是对于那些还不知道的人,这将是一个非常有用的功能。 为了使用它,您可以开始输入代码并按“ Tab”按钮以查看自动完成建议。

If you want to learn more details about autocomplete check out this article.

如果您想了解有关自动完成的更多详细信息,请查看本文。

6. You can export jupyter notebook as an HTML file.

6.您可以将jupyter笔记本导出为HTML文件。

You may have noticed that Jupyter notebooks are saved as .ipynb files. If you send this to a fellow Data Scientist or a programmer there will probably be able to open it with no problems.

您可能已经注意到Jupyter笔记本保存为.ipynb文件。 如果将其发送给数据科学家或程序员,则可以毫无问题地打开它。

But what happens if you want to share your notebook with non-technical people? You can send them an HTML version and they will be able to see the rendered version of your notebook.

但是,如果您想与非技术人员共享笔记本怎么办? 您可以向他们发送HTML版本,他们将能够查看笔记本的渲染版本。

It is very simple. Just navigate to File -> Downloas as -> .HTML.

这很简单。 只需导航到File-> Downloas as-> .HTML。

Image for post

7. You can create presentations from Jupyter notebook.

7.您可以从Jupyter笔记本创建演示文稿。

Have you been using Microsoft PowerPoint or Google slides to create presentations? While those are good options there may be better ways to create a technical presentation.

您是否一直在使用Microsoft PowerPoint或Google幻灯片来创建演示文稿? 尽管这些是不错的选择,但可能会有更好的方法来创建技术演示文稿。

You can create a presentation using the Jupyter notebook itself. The advantage of this is that you can reuse the content that you have used for the notebook. It allows you to choose which cells you want to use for the presentation, how to structure them, and which slides to skip.

您可以使用Jupyter笔记本电脑本身创建演示文稿。 这样做的好处是您可以重复使用笔记本中使用的内容。 它使您可以选择要用于演示的单元格,如何组织它们以及跳过哪些幻灯片。

When your notebook is ready you can create a presentation by going to View -> Cell Toolbar -> Slideshow.

准备好笔记本后,您可以转到视图->单元格工具栏->幻灯片放映来创建演示文稿。

Image for post

Once you have done this each cell should have a Slide Type option in the right-hand corner of the cell. You can choose the sell to be slide, sub-slide, fragment, note, or skip decide to skip it.

完成此操作后,每个单元格应该在单元格的右上角有一个“幻灯片类型”选项。 您可以选择卖出为幻灯片,子幻灯片,片段,票据或跳过,决定跳过它。

Image for post

After deciding on each slide type you can render your presentation by running the following command in the terminal window:

确定每种幻灯片类型后,可以通过在终端窗口中运行以下命令来呈现演示文稿:

jupyter nbconvert <notebook name> --to slides --post serve

Or by running the following command from the notebook itself.

或者通过从笔记本计算机本身运行以下命令。

!jupyter nbconvert <notebook name> --to slides --post serve

This should render the presentation in the new tab that is run on your localhost.

这应该在您的本地主机上运行的新选项卡中呈现演示文稿。

Image for post

You are able to navigate through the presentation using the arrows at the bottom right corner.

您可以使用右下角的箭头浏览演示文稿。

It takes time to get used to Jupyter notebook presentations so I suggest that you play with it a bit and experiment with different slide types in order to learn how to use them effectively. Once you get a grasp of it, jupyter notebooks may become your favorite tool to create technical presentations.

习惯Jupyter笔记本电脑演示需要花费一些时间,因此我建议您稍作练习,尝试不同的幻灯片类型,以学习如何有效地使用它们。 一旦掌握了它,jupyter笔记本可能会成为您喜爱的用于创建技术演示文稿的工具。

8. You can use LaTeX for formulas in Jupyter notebooks.

8.您可以将LaTeX用于Jupyter笔记本中的公式。

This is a simple tip that allows you to type mathematical formulas in a Jupyter notebook. You can use LaTeX syntax to create a formula and then embed it in the markdown cell.

这是一个简单的技巧,可让您在Jupyter笔记本中键入数学公式。 您可以使用LaTeX语法创建公式,然后将其嵌入markdown单元格中。

Let’s have a look at some examples. There are actually several ways you can do that depending on where you want your formula to be rendered.

让我们看一些例子。 实际上,有几种方法可以执行此操作,具体取决于您希望在何处呈现公式。

If you want the mathematical code to be in line with the text you can use a single dollar sign.

如果您希望数学代码与文本一致,则可以使用一个美元符号。

This is an example of $\sqrt{2}$
Image for post

Or if you want the formula to be rendered in the middle of the cell you can use two dollar signs ($$) as shown below.

或者,如果您希望公式在单元格的中间呈现,则可以使用两个美元符号($$),如下所示。

$$\sqrt{2}$$
Image for post

Have a look at this more complicated formula and see how beautifully it is rendered in the notebook!

看看这个更复杂的公式,看看它在笔记本中的渲染效果如何!

$$\sum_{i=1}^{n}i=\frac{n(n+1)}{2}$$
Image for post

9. You can install extensions to further enhance Jupyter notebook capabilities.

9.您可以安装扩展程序以进一步增强Jupyter笔记本电脑的功能。

Do you think Jupyter notebook’s capabilities are not enough? You can actually install extensions in order to get access to some additional features.

您认为Jupyter笔记本电脑的功能还不够吗? 实际上,您可以安装扩展程序以访问某些附加功能。

A while ago I wrote an article where I have presented my favorite jupyter notebook extensions.

前一段时间,我写了一篇文章,介绍了我最喜欢的jupyter笔记本扩展。

Read it if you want to learn how to enable extensions in your notebook and how to use the following ones:

如果您想了解如何在笔记本中启用扩展以及如何使用以下扩展,请阅读本手册:

  • Spellchecker,

    拼写检查器,
  • Table of contents,

    目录,
  • Collapsible headings,

    可折叠的标题
  • Autopep8,

    Autopep8,
  • ExecuteTime,

    ExecuteTime,
  • Toggle all line numbers,

    切换所有行号,
  • Variable Inspector,

    变量检查器
  • Hide code,

    隐藏代码
  • Skip-traceback.

    跳过回溯。

Summary

概要

Those were my favorite tips in respect to the practical use of Jupyter notebooks. It is an amazing tool and it is a shame that sometimes we do not use its all capabilities.

关于Jupyter笔记本的实际使用,这些是我最喜欢的技巧。 这是一个了不起的工具,但有时我们不使用其所有功能也很可惜。

I hope you have learned something new and you will be able to replicate some of the tips while you lunch jupyter notebook next time.

希望您学到了一些新知识,下次下次给jupyter笔记本午餐时,您将能够复制一些技巧。

Originally published at https://www.aboutdatablog.com on September 23, 2020.

最初于2020年9月23日https://www.aboutdatablog.com发布

PS: I am writing articles that explain basic Data Science concepts in a simple and comprehensible manner on aboutdatablog.com. If you liked this article there are some other ones you may enjoy:

PS:我在aboutdatablog.com上以简单易懂的方式撰写了一些文章,以解释基本的数据科学概念如果您喜欢这篇文章,您可能还会喜欢其他一些文章:

翻译自: https://towardsdatascience.com/9-things-you-did-not-know-about-jupyter-notebook-d0d995a8efb3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值