python jupyter笔记本中数据科学的最佳实践

The Jupyter Notebook is a different type of IDE. It provides a lightweight interface that allows a user to create and interact with code in a neat and tidy way: the goal here being a report-style format, rather than core functionality.

Jupyter Notebook是另一种类型的IDE 。 它提供了一个轻量级的界面,允许用户以整洁的方式创建代码并与之交互:这里的目标是报告样式的格式,而不是核心功能。

Now traditional IDE’s would utilise some text file or some form of script (in its rawest form) and a compiler would run through the script all at once but the goal of a Notebook is to get results and visualise things as you go along. This work is generally synonymous with research projects.

现在,传统的IDE's将利用某些文本文件或某种形式的script (以其最原始的形式),并且compiler将一次运行所有script ,但是Notebook的目标是在运行过程中获得结果并可视化事物。 这项工作通常是研究项目的代名词。

When you download and install Python, it’s accompanied with a cutesy and simple IDE called IDLE. However, it’s functionalities lack and you go in one of two directions — to a more production ready type of IDE like PyCharm or VSCode, or, you go in a more research direction like Notebooks.

当您下载并安装Python ,它会附带一个名为IDLE的可爱,简单的IDE 。 但是,它缺乏功能,您会选择两个方向之一-转向更易于生产的IDE类型(例如PyCharmVSCode ,或者,您会转向更研究的方向(例如Notebooks

If you use Python regularly then I don’t need to sell you the reasons behind wanting to use such a great piece of kit, but instead below, below I’ll give a quick tutorial on how to use it.

如果您定期使用Python ,那么我不需要向您出售希望使用如此出色的工具包的原因,但是在下面的下面,我将为您提供有关如何使用它的快速教程。

The goal of a Jupyter Notebook is to create an open source document where the code and its output can be displayed together. You can add formulas, figures, graphs, charts etc in the same document. Each Notebook can also be shared with other people to make it easier to collaborate and explain any research.

Jupyter Notebook的目标是创建一个开放源代码文档,其中的代码及其输出可以一起显示。 您可以在同一文档中添加公式,图形,图形,图表等。 每个Notebook也可以与其他人共享,以简化协作和解释任何研究。

Let’s go through Notebooks using an Anaconda Installation.

我们来看看使用Anaconda安装的Notebooks

Image for post
Photo by Andras Vas on Unsplash
照片由 Andras Vas Unsplash

启动笔记本 (Starting the Notebook)

Open your Anaconda interface and select on Jupyter Notebooks. A new tab in your browser will open and the screen will look like the below. This page is going to be a sight that you should get used to — a place that’s your central location to all your files and projects.

打开Anaconda界面,然后在Jupyter Notebooks上选择。 浏览器中的新标签页将打开,屏幕如下图所示。 您应该习惯了该页面的外观-这个位置是所有文件和项目的中心位置。

Now, when you look at that the right hand corner of the screen and click on ‘New’, a list of items come out in the menu (as below) and to open a new Notebook, you just click ‘Python 3’ or whatever is your most recent version.

现在,当您查看屏幕的右上角并单击“ New ”时,菜单中会出现一个项目列表(如下所示),并打开一个新的Notebook ,您只需单击“ Python 3 ”或其他按钮即可是您的最新版本。

Image for post
Click ‘New’ on the top right of the screen to reveal a drop down menu. Then click Python 3 which will open up a new notebook. [Image by Author]
点击屏幕右上角的“新建”以显示一个下拉菜单。 然后单击Python 3,这将打开一个新的笔记本。 [作者提供的图片]

The new opened notebook will look something like the below screenshot.

新打开的notebook将如下图所示。

Image for post
Image by Author
图片作者

The first thing you should do is click ‘Untitled’ at the top of the page and this allows you to rename your notebook. Remember, as Jupyter Notebooks is built on top of iPython so all the Notebook files will end with .ipynb.

您应该做的第一件事是单击页面顶部的“无标题”,这使您可以重命名notebook 。 记住, Jupyter Notebooks是建立在之上iPython ,这样所有的笔记本电脑文件将结束.ipynb

了解笔记本 (Understanding the Notebook)

Now that we’re in a position where the notebook is open we can focus on two key terms: Kernels and Cells.

现在我们处于打开notebook的位置,我们可以集中讨论两个关键术语: KernelsCells

A kernel can be considered as the ‘computational engine’ in the system. It’s the backend system which executes the notebooks: think of it as the engine that drives the car. In this instance, the notebooks is the car (so the interior, the steering wheel, the radio), but the ‘kernel’ is the engine driving the whole thing. Note that the kernel exists in the background and doesn’t really interfere with you as a programmer. If you want to re-start your notebook (if it crashes or something), then you’d need to restart your kernel.

kernel可以视为系统中的“计算引擎”。 它是执行笔记本电脑的后端系统:将其视为驱动汽车的引擎。 在这种情况下,笔记本是汽车(因此是内部,方向盘,收音机),但“内核”是驱动整个事情的引擎。 请注意,内核存在于后台,并且实际上不会干扰您作为程序员。 如果您想重新启动笔记本电脑(如果笔记本电脑崩溃或其他原因),则需要重新启动内核。

On the other hand, a cell is an area that you see on the notebook. It’s that grey’ed out area and can be considered a container of text or code. The easiest way to get to grips with it is to try it out. In the very first cell, type in the code:

另一方面,单元格是您在笔记本电脑上看到的区域。 它是灰色的区域,可以视为文本或代码的容器。 掌握它的最简单方法是尝试一下。 在第一个单元格中,输入代码:

print(“Hello World!!!!”)

and then press the shortcut shift+enter. Your result should be similar to the below screenshot.

然后按快捷键shift + enter 。 您的结果应类似于以下屏幕截图。

Image for post

After we write code in the code cell, we can also click the play button which executes the code in that cell and the output is shown in the markdown cell beneath it, indicated with Black tick mark in the screenshot above.

在代码单元格中编写代码后,我们还可以单击执行该单元格中的代码的play按钮,输出将显示在其下方的markdown单元格中,在上面的屏幕快照中以黑色对勾表示。

The ln [1]: indicates nothing but the line number in the screenshot. If we run another code in the next code cell, it will be In [2].

ln [1]:表示屏幕截图中的行号。 如果我们在下一个代码单元中运行另一个代码,它将在In [2]

When running a code, you may notice that the border of the executing cell turns to Blue. This is because the cell is in “Command Mode” at that time. When not executing, the cell colour is Green which denotes “Edit Mode”.

运行代码时,您可能会注意到执行单元的边框变为蓝色。 这是因为该单元当时处于“命令模式”。 不执行时,单元格颜色为绿色,表示“编辑模式”。

Generally, the flow of a notebook is from top to bottom but if we need to make a change in a previous cell or re-order the execution or reset things, then the kernel menu is very useful. There are options like Restart and Clear Output, Restart and Run All, Interrupt etc.

通常, notebook的流程是从上到下的,但是如果我们需要在上一个单元格中进行更改或重新排列执行顺序或重置内容,则kernel菜单将非常有用。 有诸如Restart and Clear OutputRestart and Run AllInterrupt等选项。

Image for post
Image by Author
图片作者

There is also an option to Change Kernel whereby the coder can switch the kernel being used. Now as Python is installed locally, my notebooks can only choose a Python Kernel but if you had other languages installed, you would get the option to use them here (instead of Python).

还有一个“ Change Kernel选项,编码人员可以通过该选项切换正在使用的kernel 。 现在,由于Python是本地安装的,所以我的笔记本只能选择一个Python Kernel但是如果您安装了其他languages ,则可以选择在此处使用它们(而不是Python )。

在单元格中单击时的快捷方式 (Shortcuts when you’re click in a cell)

  • Alt + Enter run the current cell, insert below

    Alt + Enter运行当前单元格,在下面插入

  • Ctrl + S save and checkpoint

    Ctrl + S保存并检查点

  • Shift + Enter run the current cell, select below

    Shift + Enter运行当前单元格,在下面选择

  • Ctrl + Enter run selected cells

    Ctrl + Enter运行选定的单元格

在命令模式下(单击单元格,然后按Esc激活): (While in command mode (click on a cell, then press Esc to activate):)

  • Enter take you into edit mode

    Enter将您带入编辑模式

  • H show all shortcuts

    H显示所有快捷方式

  • Up select cell above

    Up选择上方的单元格

  • Down select cell below

    Down下面选择单元格

  • Shift + Up extend selected cells above

    Shift + Up将所选单元格扩展到上方

  • Shift + Down extend selected cells below

    Shift + Down扩展下面的选定单元格

  • A insert cell above

    上方A插入单元格

  • B insert cell below

    B下方的插入单元格

  • X cut selected cells

    X剪切选定的单元格

  • S Save and Checkpoint

    S保存和检查点

  • Y change the cell type to Code

    Y将单元格类型更改为Code

  • M change the cell type to Markdown

    M将单元格类型更改为Markdown

  • P open the command palette.

    P打开命令面板。

  • C copy selected cells

    C复制选定的单元格

  • V paste cells below

    V在下面粘贴单元格

  • Shift + V paste cells above

    Shift + V粘贴上方的单元格

  • D, D (press the key twice) delete selected cells

    D, D (press the key twice)删除所选单元格

  • Z undo cell deletion

    Z undo细胞缺失

And there you have it! The above article goes over a basic introduction on how to use Jupyter Notebooks for absolute beginners, with a few tricks to keep you going.Notebooks has been a lifeline to me and my research has been able to accelerate exponentially by virtue of having such a great tool. I really recommend to people who want to research to use Jupyter Notebooks: you’ll never look back!

在那里,您拥有了! 上面的文章介绍了如何为绝对初学者使用Jupyter Notebooks的基本介绍,并提供了一些技巧来帮助您继续前进。 Notebooks一直是我的命脉,而拥有如此出色的工具使我的研究得以成倍地加速。 我真的向想要研究使用Jupyter Notebooks人推荐:您永远不会回头!

Thanks for reading, and please let me know if you have any questions!

感谢您的阅读,如果您有任何疑问,请告诉我!

Keep up to date with my latest articles here!

在这里了解我的最新文章!

翻译自: https://medium.com/code-python/the-best-ide-for-data-science-in-python-jupyter-notebooks-32da0d86f939

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值