Important note: You should always work on a duplicate of the course notebook. On the page you used to open this, tick the box next to the name of the notebook and click duplicate to easily create a new version of this notebook.

You will get errors each time you try to update your course repository if you don't do this, and your changes will end up being erased by the original course version.

Welcome to Jupyter Notebooks!

If you want to learn how to use this tool you've come to the right place. This article will teach you all you need to know to use Jupyter Notebooks effectively. You only need to go through Section 1 to learn the basics and you can go into Section 2 if you want to further increase your productivity.

You might be reading this tutorial in a web page (maybe Github or the course's webpage). We strongly suggest to read this tutorial in a (yes, you guessed it) Jupyter Notebook. This way you will be able to actually try the different commands we will introduce here.

Section 1: Need to Know

Introduction

Let's build up from the basics, what is a Jupyter Notebook? Well, you are reading one. It is a document made of cells. You can write like I am writing now (markdown cells) or you can perform calculations in Python (code cells) and run them like this:

1+1
2

Cool huh? This combination of prose and code makes Jupyter Notebook ideal for experimentation: we can see the rationale for each experiment, the code and the results in one comprehensive document. In fast.ai, each lesson is documented in a notebook and you can later use that notebook to experiment yourself.

Other renowned institutions in academy and industry use Jupyter Notebook: Google, Microsoft, IBM, Bloomberg, Berkeley and NASA among others. Even Nobel-winning economists use Jupyter Notebooks for their experiments and some suggest that Jupyter Notebooks will be the new format for research papers.

Writing

A type of cell in which you can write like this is called Markdown. Markdown is a very popular markup language. To specify that a cell is Markdown you need to click in the drop-down menu in the toolbar and select Markdown.

my first markdown

Click on the the '+' button on the left and select Markdown from the toolbar.

Now you can type your first Markdown cell. Write 'My first markdown cell' and press run.

You should see something like this:

My first markdown cell

Now try making your first Code cell: follow the same steps as before but don't change the cell type (when you add a cell its default type is Code). Type something like 3/2. You should see '1.5' as output.

3/2
1.5

Modes

If you made a mistake in your Markdown cell and you have already ran it, you will notice that you cannot edit it just by clicking on it. This is because you are in Command Mode. Jupyter Notebooks have two distinct modes:

  1. Edit Mode: Allows you to edit a cell's content.

  2. Command Mode: Allows you to edit the notebook as a whole and use keyboard shortcuts but not edit a cell's content.

You can toggle between these two by either pressing ESC and Enter or clicking outside a cell or inside it (you need to double click if its a Markdown cell). You can always know which mode you're on since the current cell has a green border if in Edit Mode and a blue border in Command Mode. Try it!

Other Important Considerations

  1. Your notebook is autosaved every 120 seconds. If you want to manually save it you can just press the save button on the upper left corner or press s in Command Mode.

  1. To know if your kernel is computing or not you can check the dot in your upper right corner. If the dot is full, it means that the kernel is working. If not, it is idle. You can place the mouse on it and see the state of the kernel be displayed.

  1. There are a couple of shortcuts you must know about which we use all the time (always in Command Mode). These are:

Shift+Enter: Runs the code or markdown on a cell

Up Arrow+Down Arrow: Toggle across cells

b: Create new cell

0+0: Reset Kernel

You can find more shortcuts in the Shortcuts section below.

12/3
4.0
00
  1. You may need to use a terminal in a Jupyter Notebook environment (for example to git pull on a repository). That is very easy to do, just press 'New' in your Home directory and 'Terminal'. Don't know how to use the Terminal? We made a tutorial for that as well. You can find it here.

That's it. This is all you need to know to use Jupyter Notebooks. That said, we have more tips and tricks below ↓↓↓

Section 2: Going deeper

Markdown formatting

Italics, Bold, Strikethrough, Inline, Blockquotes and Links

The five most important concepts to format your code appropriately when using markdown are:

  1. Italics: Surround your text with '_' or '*'
  2. Bold: Surround your text with '__' or '**'
  3. inline: Surround your text with '`'
  4. blockquote: Place '>' before your text.

  5. Links: Surround the text you want to link with '[]' and place the link adjacent to the text, surrounded with '()'

Headings

Notice that including a hashtag before the text in a markdown cell makes the text a heading. The number of hashtags you include will determine the priority of the header ('#' is level one, '##' is level two, '###' is level three and '####' is level four). We will add three new cells with the '+' button on the left to see how every level of heading looks.

Double click on some headings and find out what level they are!

Lists

There are three types of lists in markdown.

Ordered list:

  1. Step 1

    2. Step 1B
  2. Step 3

Unordered list

  • learning rate
  • cycle length
  • weight decay

Task list

  • [x] Learn Jupyter Notebooks

    • [x] Writing
    • [x] Modes
    • [x] Other Considerations
  • [ ] Change the world

Double click on each to see how they are built!

Code Capabilities

Code cells are different than Markdown cells in that they have an output cell. This means that we can keep the results of our code within the notebook and share them. Let's say we want to show a graph that explains the result of an experiment. We can just run the necessary cells and save the notebook. The output will be there when we open it again! Try it out by running the next four cells.

# Import necessary libraries
from fastai.vision import *
import matplotlib.pyplot as plt
from PIL import Image
a = 1
b = a + 1
c = b + a + 1
d = c + b + a + 1
a, b, c ,d
(1, 2, 4, 8)
plt.plot([a,b,c,d])
plt.show()

We can also print images while experimenting. I am watching you.

Image.open('images/notebook_tutorial/cat_example.jpg')

Running the app locally

You may be running Jupyter Notebook from an interactive coding environment like Gradient, Sagemaker or Salamander. You can also run a Jupyter Notebook server from your local computer. What's more, if you have installed Anaconda you don't even need to install Jupyter (if not, just pip install jupyter).

You just need to run jupyter notebook in your terminal. Remember to run it from a folder that contains all the folders/files you will want to access. You will be able to open, view and edit files located within the directory in which you run this command but not files in parent directories.

If a browser tab does not open automatically once you run the command, you should CTRL+CLICK the link starting with 'https://localhost:' and this will open a new tab in your default browser.

Creating a notebook

Click on 'New' in the upper right corner and 'Python 3' in the drop-down list (we are going to use a Python kernel for all our experiments).

Note: You will sometimes hear people talking about the Notebook 'kernel'. The 'kernel' is just the Python engine that performs the computations for you.

Shortcuts and tricks

Command Mode Shortcuts

There are a couple of useful keyboard shortcuts in Command Mode that you can leverage to make Jupyter Notebook faster to use. Remember that to switch back and forth between Command Mode and Edit Mode with Esc and Enter.

m: Convert cell to Markdown

y: Convert cell to Code

D+D: Delete the cell(if it's not the only cell) or delete the content of the cell and reset cell to Code(if only one cell left)

o: Toggle between hide or show output

Shift+Arrow up/Arrow down: Selects multiple cells. Once you have selected them you can operate on them like a batch (run, copy, paste etc).

Shift+M: Merge selected cells.

Shift+Tab: [press these two buttons at the same time, once] Tells you which parameters to pass on a function

Shift+Tab: [press these two buttons at the same time, three times] Gives additional information on the method

Cell Tricks

from fastai import*
from fastai.vision import *

There are also some tricks that you can code into a cell.

?function-name: Shows the definition and docstring for that function

?ImageDataBunch

??function-name: Shows the source code for that function

??ImageDataBunch

doc(function-name): Shows the definition, docstring and links to the documentation of the function

(only works with fastai library imported)

doc(ImageDataBunch)

Line Magics

Line magics are functions that you can run on cells and take as an argument the rest of the line from where they are called. You call them by placing a '%' sign before the command. The most useful ones are:

%matplotlib inline: This command ensures that all matplotlib plots will be plotted in the output cell within the notebook and will be kept in the notebook when saved.

%reload_ext autoreload, %autoreload 2: Reload all modules before executing a new line. If a module is edited, it is not necessary to rerun the import commands, the modules will be reloaded automatically.

These three commands are always called together at the beginning of every notebook.

%matplotlib inline
%reload_ext autoreload
%autoreload 2

%timeit: Runs a line a ten thousand times and displays the average time it took to run it.

%timeit [i+1 for i in range(1000)]
54.4 µs ± 1.37 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%debug: Allows to inspect a function which is showing an error using the Python debugger.

for i in range(1000):
a = i+1
b = 'string'
c = b+1
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-14-8d78ff778454> in <module>()
2 a = i+1
3 b = 'string'
----> 4 c = b+1 TypeError: must be str, not int
%debug
> <ipython-input-14-8d78ff778454>(4)<module>()
1 for i in range(1000):
2 a = i+1
3 b = 'string'
----> 4 c = b+1 ipdb> c

Jupyter Notebooks usage的更多相关文章

  1. jupyter notebooks 中键盘快捷键

    键盘快捷键——节省时间且更有生产力! 快捷方式是 Jupyter Notebooks 最大的优势之一.当你想运行任意代码块时,只需要按 Ctrl+Enter 就行了.Jupyter Notebooks ...

  2. Jupyter Notebooks 是数据科学/机器学习社区内一款非常流行的工具

    Jupyter Notebooks 是数据科学/机器学习社区内一款非常流行的工具.Jupyter Notebooks 允许数据科学家创建和共享他们的文档,从代码到全面的报告都可以.李笑来 相当于拿他来 ...

  3. Running cells requires Jupyter notebooks to be installed

    /******************************************************************************* * Running cells req ...

  4. (数据分析)第02章 Python语法基础,IPython和Jupyter Notebooks.md

    第2章 Python语法基础,IPython和Jupyter Notebooks 当我在2011年和2012年写作本书的第一版时,可用的学习Python数据分析的资源很少.这部分上是一个鸡和蛋的问题: ...

  5. 【精选】Jupyter Notebooks里的TensorFlow图可视化

    [精选]Jupyter Notebooks里的TensorFlow图可视化   https://mp.weixin.qq.com/s?src=11&timestamp=1503060682&a ...

  6. Jupyter Notebooks 配置

    重装了三遍(破音) 一.首先进行Anaconda的下载 然后安装,将环境配置到系统变量上,如下 然后,打开 windows 的终端,检查是否配置成功 conda -V 然后就可以开始 Jupyter ...

  7. Jupyter Notebooks的安装和使用介绍

    最近又开始重新学习Python,学习中使用到了一款编辑器Jupyter Notebooks ,非常想安利给初学python的同学.注:本文内容仅针对windows环境下安装和配置Jupyter Not ...

  8. 开始Jupyter Notebooks

    开始Jupyter Notebooks 安装Anaconda 因为不能有空格,所以没有选C:\Program Files 认识Jupyter Notebooks 修改 jupyter notebook ...

  9. 基于Jupyter Notebooks的C# .NET Interactive安装与使用

    .NET Interactive发布预览版了,可以像Python那样用jupyter notebooks来编辑C#代码.具体可以在GitHub上查看dotnet/interactive项目. 安装步骤 ...

随机推荐

  1. Unity表面着色器

    表面着色器和之前无光照着色器不同,其中没有顶点着色器和片元着色器,而增加了光照函数: 接下写了一个求两个贴图的光照效果 两个贴图做插值运算: Shader "Custom/SurfaceSh ...

  2. C++中函数访问数组的方式

    在书写C++代码时,往往为了令代码更加简洁高效.提高代码可读性,会对定义的函数有一些特殊的要求:比如不传递不必要的参数,以此来让函数的参数列表尽可能简短. 当一个函数需要访问一个数组元素时,出于上述原 ...

  3. Mysql 锁定 读情况

    在一个事务中,标准的SELECT语句是不会加锁,但是有两种情况例外. SELECT ... LOCK IN SHARE MODE SELECT ... FOR UPDATE SELECT ... LO ...

  4. Python连载60-Tkinter布局、按钮以及属性详解

    一.Tkinter​ 1.组件的大致使用步骤 (1)创建总面板 (2)创建面板上的各种组件: i.指定组件的父组件,即依附关系:ii.利用相应的属性对组件进行设置:iii.给组件安排布局. (3)同步 ...

  5. 139、Java内部类之使用this访问外部类属性

    01.代码如下: package TIANPAN; class Outer { // 外部类 private String msg = "Hello World !"; class ...

  6. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

  7. python 爬虫原理

    简单来说互联网是由一个个站点和网络组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什么? 如果 ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-warning" 类的文本样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Python print()函数

    #输出单个数据,会自动输出回车换行 print(1) print(2) #输出 1 2 #输出换行 print('\n') #防止换行 for x in range(0, 5): print(x, e ...

  10. c++ (翁恺浙大公开课)前言、目录

    c++语言比较复杂,学习起来相对难一些,加之特性繁多,很难全部掌握:特别是工作几年之后,每次温故都有很大的收获,之前不懂的地方随着工作的积累和重新的学习,都会慢慢的解开,当然我现在还是很菜... 之所 ...