This post is based on a post that originally appeared on Alex Rogozhnikov’s blog, ‘Brilliantly Wrong’.
这篇文章是基于最初出现在Alex Rogozhnikov的博客“ Brilliantly Wrong”上的 。
We have expanded the post and will continue to do so over time – if you have a suggestion please let us know in the comments. Thanks to Alex for graciously letting us republish his work here.
我们已经扩大了该职位,并将随着时间的推移继续这样做–如果您有任何建议,请在评论中告知我们。 感谢Alex慷慨地让我们在这里重新发表他的作品。
Jupyter笔记本 (Jupyter Notebook)
Jupyter notebook, formerly known as the IPython notebook, is a flexible tool that helps you create readable analyses, as you can keep code, images, comments, formulae and plots together.
Jupyter笔记本(以前称为IPython笔记本)是一种灵活的工具,可帮助您创建可读的分析,因为您可以将代码,图像,注释,公式和图表保持在一起。
Jupyter is quite extensible, supports many programming languages and is easily hosted on your computer or on almost any server — you only need to have ssh or http access. Best of all, it’s completely free.
Jupyter具有相当的可扩展性,支持多种编程语言,并且可以轻松地托管在您的计算机或几乎任何服务器上-您只需要ssh或http访问。 最重要的是,它是完全免费的。
The Jupyter interface.
Jupyter界面。
Project Jupyter was born out of the IPython project as the project evolved to become a notebook that could support multiple languages – hence its historical name as the IPython notebook. The name Jupyter is an indirect acronyum of the three core languages it was designed for: JUlia, PYThon, and R and is inspired by the planet Jupiter.
Jupyter项目是从IPython项目诞生的,当时该项目演变为可以支持多种语言的笔记本,因此其历史名称为IPython笔记本。 Jupyter这个名字是它设计用于的三种核心语言的间接称谓: JU lia, PYT hon和R ,它受到木星的启发。
When working with Python in Jupyter, the IPython kernel is used, which gives us some handy access to IPython features from within our Jupyter notebooks (more on that later!)
在Jupyter中使用Python时,将使用IPython内核,这使我们可以从Jupyter笔记本中方便地访问IPython功能(稍后会介绍更多!)
We’re going to show you 28 tips and tricks to make your life working with Jupyter easier.
我们将向您展示28条提示和技巧,以使您与Jupyter的工作更加轻松。
1.键盘快捷键 (1. Keyboard Shortcuts)
As any power user knows, keyboard shortcuts will save you lots of time. Jupyter stores a list of keybord shortcuts under the menu at the top: Help > Keyboard Shortcuts
. It’s worth checking this each time you update Jupyter, as more shortcuts are added all the time.
任何高级用户都知道,键盘快捷键将为您节省大量时间。 Jupyter在顶部菜单下存储键盘快捷键列表: Help > Keyboard Shortcuts
。 每次更新Jupyter时都值得检查一下,因为一直都在增加更多的快捷方式。
Another way to access keyboard shortcuts, and a handy way to learn them is to use the command palette: Cmd + Shift + P
(or Ctrl + Shift + P
on Linux and Windows). This dialog box helps you run any command by name – useful if you don’t know the keyboard shortcut for an action or if what you want to do does not have a keyboard shortcut. The functionality is similar to Spotlight search on a Mac, and once you start using it you’ll wonder how you lived without it!
访问键盘快捷方式的另一种方法,也是一种方便的学习方法,是使用命令面板: Cmd + Shift + P
(在Linux和Windows上为Ctrl + Shift + P
)。 此对话框可帮助您按名称运行任何命令-如果您不知道某个操作的键盘快捷键或要执行的操作没有键盘快捷键,则此对话框很有用。 该功能类似于Mac上的Spotlight搜索,一旦开始使用它,您会想知道没有它会怎样生活!
The command palette.
命令面板。
Some of my favorites:
我的最爱:
Esc
will take you into command mode where you can navigate around your notebook with arrow keys.- While in command mode:
A
to insert a new cell above the current cell,B
to insert a new cell below.M
to change the current cell to Markdown,Y
to change it back to codeD + D
(press the key twice) to delete the current cell
Enter
will take you from command mode back into edit mode for the given cell.Shift = Tab
will show you the Docstring (documentation) for the the object you have just typed in a code cell – you can keep pressing this short cut to cycle through a few modes of documentation.Ctrl + Shift + -
will split the current cell into two from where your cursor is.Esc + F
Find and replace on your code but not the outputs.Esc + O
Toggle cell output.- Select Multiple Cells:
Shift + J
orShift + Down
selects the next sell in a downwards direction. You can also select sells in an upwards direction by usingShift + K
orShift + Up
.- Once cells are selected, you can then delete / copy / cut / paste / run them as a batch. This is helpful when you need to move parts of a notebook.
- You can also use
Shift + M
to merge multiple cells.
-
Esc
将带您进入命令模式,您可以在其中使用箭头键在笔记本上浏览。 - 在命令模式下:
-
A
在当前单元格上方插入一个新单元格,B
在其下方插入新单元格。 -
M
将当前单元格更改为Markdown,Y
将其更改为代码 -
D + D
(按两次键)删除当前单元格
-
-
Enter
将使您从命令模式回到给定单元格的编辑模式。 -
Shift = Tab
将显示您刚刚在代码单元中键入的对象的Docstring(文档)-您可以按住此快捷键以循环浏览几种文档模式。 -
Ctrl + Shift + -
将当前单元格从光标所在位置拆分为两个。 -
Esc + F
查找并替换代码,而不替换输出。 -
Esc + O
切换单元格输出。 - 选择多个单元格:
-
Shift + J
或Shift + Down
选择Shift + Down
的卖出方向。 您也可以使用Shift + K
或Shift + Up
选择向上销售。 - 选定单元格后,即可批量删除/复制/剪切/粘贴/运行它们。 当您需要移动笔记本电脑的部件时,这很有用。
- 您还可以使用
Shift + M
合并多个单元格。
-
Merging multiple cells.
合并多个单元格。
2.漂亮的变量显示 (2. Pretty Display of Variables)
The first part of this is pretty widely known. By finishing a Jupyter cell with the name of a variable or unassigned output of a statement, Jupyter will display that variable without the need for a print statement. This is especially useful when dealing with Pandas DataFrames, as the output is neatly formatted into a table.
第一部分是众所周知的。 通过用变量名或语句的未分配输出完成Jupyter单元格,Jupyter将显示该变量,而无需打印语句。 这在处理Pandas DataFrame时特别有用,因为输出被整齐地格式化为表格。
What is known less, is that you can alter a modify the ast_note_interactivity
kernel option to make jupyter do this for any variable or statement on it’s own line, so you can see the value of multiple statements at once.
鲜为人知的是,您可以更改修改ast_note_interactivity
内核选项,以使jupyter对自己行上的任何变量或语句执行此操作,因此您可以一次查看多个语句的值。
In [1]:
在[1]中:
from from IPython.core.interactiveshell IPython.core.interactiveshell import import InteractiveShell
InteractiveShell
InteractiveShellInteractiveShell .. ast_node_interactivity ast_node_interactivity = = "all"
"all"
In [2]:
在[2]中:
Out[2]:
出[2]:
lat | 拉特 | long | 长 | depth | 深度 | mag | 磁 | stations | 车站 | ||
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 1个 | -20.42 | -20.42 | 181.62 | 181.62 | 562 | 562 | 4.8 | 4.8 | 41 | 41 |
2 | 2 | -20.62 | -20.62 | 181.03 | 181.03 | 650 | 650 | 4.2 | 4.2 | 15 | 15 |
3 | 3 | -26.00 | -26.00 | 184.10 | 184.10 | 42 | 42 | 5.4 | 5.4 | 43 | 43 |
4 | 4 | -17.97 | -17.97 | 181.66 | 181.66 | 626 | 626 | 4.1 | 4.1 | 19 | 19 |
5 | 5 | -20.42 | -20.42 | 181.96 | 181.96 | 649 | 649 | 4.0 | 4.0 | 11 | 11 |
Out[2]:
出[2]:
lat | 拉特 | long | 长 | depth | 深度 | mag | 磁 | stations | 车站 | ||
---|---|---|---|---|---|---|---|---|---|---|---|
996 | 996 | -25.93 | -25.93 | 179.54 | 179.54 | 470 | 470 | 4.4 | 4.4 | 22 | 22 |
997 | 997 | -12.28 | -12.28 | 167.06 | 167.06 | 248 | 248 | 4.7 | 4.7 | 35 | 35 |
998 | 998 | -20.13 | -20.13 | 184.20 | 184.20 | 244 | 244 | 4.5 | 4.5 | 34 | 34 |
999 | 999 | -17.40 | -17.40 | 187.80 | 187.80 | 40 | 40 | 4.5 | 4.5 | 14 | 14 |
1000 | 1000 | -21.59 | -21.59 | 170.56 | 170.56 | 165 | 165 | 6.0 | 6.0 | 119 | 119 |
If you want to set this behaviour for all instances of Jupyter (Notebook and Console), simply create a file ~/.ipython/profile_default/ipython_config.py
with the lines below.
如果要为Jupyter的所有实例(笔记本和控制台)设置此行为,只需使用以下~/.ipython/profile_default/ipython_config.py
行创建文件~/.ipython/profile_default/ipython_config.py
。
c c = = get_configget_config ()
()
# Run all nodes interactively
# Run all nodes interactively
cc .. InteractiveShellInteractiveShell .. ast_node_interactivity ast_node_interactivity = = "all""all"
3.轻松链接到文档 (3. Easy links to documentation)
Inside the Help
menu you’ll find handy links to the online documentation for common libraries including NumPy, Pandas, SciPy and Matplotlib.
在“ Help
菜单中,您将找到指向常见库(包括NumPy,Pandas,SciPy和Matplotlib)的联机文档的联机文档的便捷链接。
Don’t forget also that by prepending a library, method or variable with ?
, you can access the Docstring for quick reference on syntax.
也不要忘记在库,方法或变量前添加?
,您可以访问Docstring以快速获取语法参考。
In [3]:
在[3]中:
Docstring:
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
Type: method_descriptor
4.在笔记本中绘图 (4. Plotting in notebooks)
There are many options for generating plots in your notebooks.
有许多用于在笔记本中生成图的选项。
- matplotlib (the de-facto standard), activated with
%matplotlib inline
– Here’s a Dataquest Matplotlib Tutorial. %matplotlib notebook
provides interactivity but can be a little slow, since rendering is done server-side.- Seaborn is built over Matplotlib and makes building more attractive plots easier. Just by importing Seaborn, your matplotlib plots are made ‘prettier’ without any code modification.
- mpld3 provides alternative renderer (using d3) for matplotlib code. Quite nice, though incomplete.
- bokeh is a better option for building interactive plots.
- plot.ly can generate nice plots – this used to be a paid service only but was recently open sourced.
- Altair is a relatively new declarative visualization library for Python. It’s easy to use and makes great looking plots, however the ability to customize those plots is not nearly as powerful as in Matplotlib.
- matplotlib (事实上的标准),已通过
%matplotlib inline
激活–这是Dataquest Matplotlib教程 。 -
%matplotlib notebook
提供了交互性,但是可能会有点慢,因为渲染是在服务器端完成的。 - Seaborn建立在Matplotlib之上,使构建更具吸引力的地块变得更加容易。 只需导入Seaborn,您的matplotlib图就可以变得“更漂亮”,而无需进行任何代码修改。
- mpld3为matplotlib代码提供了替代渲染器(使用d3)。 很好,尽管不完整。
- 背景虚化是构建交互式地块的更好选择。
- plot.ly可以生成漂亮的图-这曾经只是一种付费服务,但最近才开源。
- Altair是一个相对较新的Python声明式可视化库。 它易于使用且制作精美的图,但是自定义这些图的功能几乎不如Matplotlib强大。
The Jupyter interface.
Jupyter界面。
5. IPython魔术命令 (5. IPython Magic Commands)
The %matplotlib inline
you saw above was an example of a IPython Magic command. Being based on the IPython kernel, Jupyter has access to all the Magics from the IPython kernel, and they can make your life a lot easier!
您在上面看到的%matplotlib inline
是IPython Magic命令的示例。 基于IPython内核,Jupyter可以访问IPython内核中的所有Magics,它们可以使您的生活更加轻松!
In [53]:
在[53]中:
# This will list all magic commands
# This will list all magic commands
%% lsmagic
lsmagic
Out[53]:
出[53]:
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
I recommend browsing the documentation for all IPython Magic commands as you’ll no doubt find some that work for you. A few of my favorites are below:
我建议浏览所有IPython Magic命令的文档,因为无疑会找到一些适合您的命令。 我的一些最爱如下:
6. IPython Magic –%env:设置环境变量 (6. IPython Magic – %env: Set Environment Variables)
You can manage environment variables of your notebook without restarting the jupyter server process. Some libraries (like theano) use environment variables to control behavior, %env is the most convenient way.
您可以在不重新启动jupyter服务器进程的情况下管理笔记本的环境变量。 一些库(例如theano)使用环境变量来控制行为,%env是最方便的方法。
In [55]:
在[55]中:
env: OMP_NUM_THREADS=4
7. IPython Magic –%run:执行python代码 (7. IPython Magic – %run: Execute python code)
%run
can execute python code from .py files – this is well-documented behavior. Lesser known is the fact that it can also execute other jupyter notebooks, which can quite useful.
%run
可以从.py文件执行python代码-这是有据可查的行为。 鲜为人知的是它还可以执行其他jupyter笔记本,这一点非常有用。
Note that using %run
is not the same as importing a python module.
请注意,使用%run
与导入python模块不同。
In [56]:
在[56]中:
# this will execute and show the output from
# this will execute and show the output from
# all code cells of the specified notebook
# all code cells of the specified notebook
%% run ./two-histograms.ipynb
run ./two-histograms.ipynb
8. IPython Magic –%load:从外部脚本插入代码 (8. IPython Magic – %load: Insert the code from an external script)
This will replace the contents of the cell with an external script. You can either use a file on your computer as a source, or alternatively a URL.
这将用外部脚本替换单元格的内容。 您可以使用计算机上的文件作为源,也可以使用URL。
In [ ]:
在[]中:
In [61]:
在[61]中:
# After Running
# After Running
# %load ./hello_world.py
# %load ./hello_world.py
if if __name__ __name__ == == "__main__""__main__" :
:
printprint (( "Hello World!""Hello World!" )
)
Hello World!
9. IPython Magic –%store:在笔记本之间传递变量。 (9. IPython Magic – %store: Pass variables between notebooks.)
The %store
command lets you pass variables between two different notebooks.
使用%store
命令可以在两个不同的笔记本之间传递变量。
In [62]:
在[62]中:
Stored 'data' (str)
Now, in a new notebook…
现在,在新笔记本中…
In [1]:
在[1]中:
%% store -r data
store -r data
printprint (( datadata )
)
this is the string I want to pass to different notebook
10. IPython Magic –%who:列出全局范围的所有变量。 (10. IPython Magic – %who: List all variables of global scope.)
The %who
command without any arguments will list all variables that existing in the global scope. Passing a parameter like str
will list only variables of that type.
不带任何参数的%who
命令将列出全局范围中存在的所有变量。 传递类似str
的参数将仅列出该类型的变量。
In [1]:
在[1]中:
one three two
11. IPython Magic –计时 (11. IPython Magic – Timing)
There are two IPython Magic commands that are useful for timing – %%time
and %timeit
. These are especially handy when you have some slow code and you’re trying to indentify where the issue is.
有两个IPython Magic命令可用于计时- %%time
和%timeit
。 当您有一些慢速代码并试图确定问题出在哪里时,这些功能特别方便。
%%time
will give you information about a single run of the code in your cell.
%%time
将为您提供有关单元格中单次运行代码的信息。
In [4]:
在[4]中:
%%%% time
import time
for _ in range(1000):
time.sleep(0.01)# sleep for 0.01 seconds
time
import time
for _ in range(1000):
time.sleep(0.01)# sleep for 0.01 seconds
CPU times: user 21.5 ms, sys: 14.8 ms, total: 36.3 ms
Wall time: 11.6 s
%%timeit
uses the Python timeit module which runs a statement 100,000 times (by default) and then provides the mean of the fastest three times.
%%timeit
使用Python timeit模块 ,该模块运行一条语句100,000次(默认情况下),然后提供最快三倍的平均值。
In [3]:
在[3]中:
The slowest run took 7.29 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.5 µs per loop
12. IPython Magic – %% writefile和%pycat:导出单元格的内容/显示外部脚本的内容 (12. IPython Magic – %%writefile and %pycat: Export the contents of a cell/Show the contents of an external script)
Using the %%writefile
magic saves the contents of that cell to an external file. %pycat
does the opposite, and shows you (in a popup) the syntax highlighted contents of an external file.
使用%%writefile
魔术将那个单元格的内容保存到一个外部文件中。 %pycat
进行相反的操作,并(在弹出窗口中)向您显示外部文件的语法突出显示的内容。
In [7]:
在[7]中:
%%%% writefile pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
writefile pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
Writing pythoncode.py
In [8]:
在[8]中:
import import numpy
numpy
def def append_if_not_existsappend_if_not_exists (( arrarr , , xx ):
):
if if x x not not in in arrarr :
:
arrarr .. appendappend (( xx )
)
def def some_useless_slow_functionsome_useless_slow_function ():
():
arr arr = = listlist ()
()
for for i i in in rangerange (( 1000010000 ):
):
x x = = numpynumpy .. randomrandom .. randintrandint (( 00 , , 1000010000 )
)
append_if_not_existsappend_if_not_exists (( arrarr , , xx ))
13. IPython Magic –%prun:显示您的程序在每个函数中花费了多少时间。 (13. IPython Magic – %prun: Show how much time your program spent in each function.)
Using %prun statement_name
will give you an ordered table showing you the number of times each internal function was called within the statement, the time each call took as well as the cumulative time of all runs of the function.
使用%prun statement_name
将为您提供一个有序表,向您显示该语句中每个内部函数被调用的次数,每次调用所花费的时间以及该函数所有运行的累积时间。
In [47]:
在[47]中:
26324 function calls in 0.556 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
10000 0.527 0.000 0.528 0.000 <ipython-input-46-b52343f1a2d5>:2(append_if_not_exists)
10000 0.022 0.000 0.022 0.000 {method 'randint' of 'mtrand.RandomState' objects}
1 0.006 0.006 0.556 0.556 <ipython-input-46-b52343f1a2d5>:6(some_useless_slow_function)
6320 0.001 0.000 0.001 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.556 0.556 <string>:1(<module>)
1 0.000 0.000 0.556 0.556 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
14. IPython Magic –使用%pdb进行调试 (14. IPython Magic – Debugging with %pdb)
Jupyter has own interface for The Python Debugger (pdb
). This makes it possible to go inside the function and investigate what happens there.
Jupyter有自己的Python调试器( pdb
)接口。 这样就可以进入函数内部并调查其中发生的情况。
You can view a list of accepted commands for pdb
here.
您可以在此处查看pdb
的可接受命令列表 。
In [ ]:
在[]中:
%% pdb
pdb
def def pick_and_takepick_and_take ():
():
picked picked = = numpynumpy .. randomrandom .. randintrandint (( 00 , , 10001000 )
)
raise raise NotImplementedErrorNotImplementedError ()
()
pick_and_takepick_and_take ()
()
Automatic pdb calling has been turned ON
<ipython-input-24-0f6b26649b2e>(5)<ipython-input-24-0f6b26649b2e> (5) pick_and_takepick_and_take ()
()
3 3 def pick_and_takedef pick_and_take (( )) :
:
4 4 picked picked = numpy= numpy .random. random .randint. randint (( 00 , , 10001000 )
)
----> 5 ----> 5 raise NotImplementedErrorraise NotImplementedError (( )
)
6
6
7 pick_and_take 7 pick_and_take (( )
)
15. IPython Magic –用于Retina笔记本的高分辨率绘图输出 (15. IPython Magic – High-resolution plot outputs for Retina notebooks)
One line of IPython magic will give you double resolution plot output for Retina screens, such as the more recent Macbooks. Note: the example below won’t render on non-retina screens
一行IPython magic将为您提供视网膜屏幕(例如更新的Macbook)的双分辨率绘图输出。 注意:以下示例不会在非视网膜屏幕上呈现
In [ ]:
在[]中:
In [ ]:
在[]中:
%% config InlineBackend.figure_format = 'retina'
config InlineBackend.figure_format = 'retina'
pltplt .. plotplot (( xx ,, yy )
)
pltplt .. showshow ();
();
喜欢这篇文章吗? 使用Dataquest学习数据科学! (Enjoying this post? Learn data science with Dataquest!)
- Learn from the comfort of your browser.
- Work with real-life data sets.
- Build a portfolio of projects.
- 从舒适的浏览器中学习。
- 处理实际数据集。
- 建立项目组合。
16.抑制最终功能的输出。 (16. Suppress the output of a final function.)
Sometimes it’s handy to suppress the output of the function on a final line, for instance when plotting. To do this, you just add a semicolon at the end.
有时在最后一行抑制函数输出非常方便,例如在绘制时。 为此,您只需在末尾添加分号即可。
In [4]:
在[4]中:
In [5]:
在[5]中:
# Here you get the output of the function
# Here you get the output of the function
pltplt .. histhist (( xx )
)
Out[5]:
出[5]:
(array([ 216., 126., 106., 95., 87., 81., 77., 73., 71., 68.]),
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
<a list of 10 Patch objects>)
In [6]:
在[6]中:
17.执行Shell命令 (17. Executing Shell Commands)
It’s easy to execute a shell command from inside your notebook. You can use this to check what datasets are in available in your working folder:
从笔记本内部执行shell命令很容易。 您可以使用它来检查工作文件夹中可用的数据集:
nba_2016.csv titanic.csv
pixar_movies.csv whitehouse_employees.csv
Or to check and manage packages.
或检查和管理软件包。
In [8]:
在[8]中:
!pip install numpy
! pip install numpy
!pip list ! pip list | grep pandas
| grep pandas
Requirement already satisfied (use --upgrade to upgrade): numpy in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
pandas (0.18.1)
18.在论坛上使用LaTeX (18. Using LaTeX for forumlas)
When you write LaTeX in a Markdown cell, it will be rendered as a formula using MathJax.
当您在Markdown单元格中编写LaTeX时,它将使用MathJax呈现为公式。
This:
这个:
Becomes this:
变成这个:
Markdown is an important part of notebooks, so don’t forget to use its expressiveness!
Markdown是笔记本的重要组成部分,因此请不要忘记使用它的表现力!
19.从笔记本中的其他内核运行代码 (19. Run code from a different kernel in a notebook)
If you want to, you can combine code from multiple kernels into one notebook.
如果需要,可以将来自多个内核的代码组合到一个笔记本中。
Just use IPython Magics with the name of your kernel at the start of each cell that you want to use that Kernel for:
只需在要使用该内核的每个单元格的开头使用带有内核名称的IPython Magics:
%%bash
%%HTML
%%python2
%%python3
%%ruby
%%perl
-
%%bash
-
%%HTML
-
%%python2
-
%%python3
-
%%ruby
-
%%perl
In [6]:
在[6]中:
for i in for i in {1..5{ 1..5 }
}
do
do
echo echo "i is "i is $i$i "
"
done
done
i is 1
i is 2
i is 3
i is 4
i is 5
20.为Jupyter安装其他内核 (20. Install other kernels for Jupyter)
One of the nice features about Jupyter is ability to run kernels for different languages. As an example, here is how to get and R kernel running.
Jupyter的一项不错的功能是能够为不同语言运行内核。 例如,这里是如何使R内核运行。
简单的选择:使用Anaconda安装R内核 (Easy Option: Installing the R Kernel Using Anaconda)
If you used Anaconda to set up your environment, getting R working is extremely easy. Just run the below in your terminal:
如果您使用Anaconda来设置环境,则使R正常工作非常容易。 只需在您的终端中运行以下命令:
不太容易的选择:手动安装R内核 (Less Easy Option: Installing the R Kernel Manually)
If you are not using Anaconda, the process is a little more complex. Firstly, you’ll need to install R from CRAN if you haven’t already.
如果您不使用Anaconda,则过程会稍微复杂一些。 首先,如果尚未安装,则需要从CRAN安装R。
Once that’s done, fire up an R console and run the following:
完成后,启动R控制台并运行以下命令:
install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools')) devtools::install_github('IRkernel/IRkernel') IRkernel::installspec() # to register the kernel in the current R installation
install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools')) devtools::install_github('IRkernel/IRkernel') IRkernel::installspec() # to register the kernel in the current R installation
21.在同一笔记本中运行R和Python。 (21. Running R and Python in the same notebook.)
The best solution to this is to install rpy2 (requires a working version of R as well), which can be easily done with pip
:
最好的解决方案是安装rpy2 (也需要R的工作版本),这可以通过pip
轻松完成:
You can then use the two languages together, and even pass variables inbetween:
然后,您可以将两种语言一起使用,甚至可以在它们之间传递变量:
In [1]:
在[1]中:
%% load_ext rpy2.ipython
load_ext rpy2.ipython
In [2]:
在[2]中:
Out[2]:
出[2]:
array([1], dtype=int32)
In [3]:
在[3]中:
import import pandas pandas as as pd
pd
df df = = pdpd .. DataFrameDataFrame ({
({
'Letter''Letter' : : [[ 'a''a' , , 'a''a' , , 'a''a' , , 'b''b' , , 'b''b' , , 'b''b' , , 'c''c' , , 'c''c' , , 'c''c' ],
],
'X''X' : : [[ 44 , , 33 , , 55 , , 22 , , 11 , , 77 , , 77 , , 55 , , 99 ],
],
'Y''Y' : : [[ 00 , , 44 , , 33 , , 66 , , 77 , , 1010 , , 1111 , , 99 , , 1313 ],
],
'Z''Z' : : [[ 11 , , 22 , , 33 , , 11 , , 22 , , 33 , , 11 , , 22 , , 33 ]
]
})
})
In [4]:
在[4]中:
Example courtesy Revolutions Blog
革命博客提供的示例
22.用其他语言编写函数 (22. Writing functions in other languages)
Sometimes the speed of numpy is not enough and I need to write some fast code. In principle, you can compile function in the dynamic library and write python wrappers…
有时numpy的速度还不够,我需要编写一些快速代码。 原则上,您可以在动态库中编译函数并编写python包装器…
But it is much better when this boring part is done for you, right?
但是,为您完成这无聊的部分会更好,对吧?
You can write functions in cython or fortran and use those directly from python code.
您可以使用cython或fortran编写函数,然后直接从python代码中使用这些函数。
First you’ll need to install:
首先,您需要安装:
!pip install cython fortran-magic
!pip install cython fortran-magic
In [ ]:
在[]中:
In [ ]:
在[]中:
%%%% cython
cython
def def myltiply_by_2myltiply_by_2 (( float float xx ):
):
return return 2.0 2.0 * * x
x
In [ ]:
在[]中:
Personally I prefer to use fortran, which I found very convenient for writing number-crunching functions. More details of usage can be found here.
我个人更喜欢使用fortran,我发现它非常适合编写数字运算功能。 使用的更多详细信息可以在这里找到。
In [ ]:
在[]中:
%% load_ext fortranmagic
load_ext fortranmagic
In [ ]:
在[]中:
In [ ]:
在[]中:
compute_fortrancompute_fortran ([([ 11 , , 22 , , 33 ], ], [[ 44 , , 55 , , 66 ])
])
There are also different jitter systems which can speed up your python code. More examples can be found here.
还有不同的抖动系统可以加快python代码的速度。 在这里可以找到更多示例。
23.多光标支持 (23. Multicursor support)
Jupyter supports mutiple cursors, similar to Sublime Text. Simply click and drag your mouse while holding down Alt
.
Jupyter支持多种光标,类似于Sublime Text。 只需按住Alt
并单击并拖动鼠标即可。
Multicursor support.
多光标支持。
24. Jupyter-contrib扩展 (24. Jupyter-contrib extensions)
Jupyter-contrib extensions is a family of extensions which give Jupyter a lot more functionality, including e.g. jupyter spell-checker
and code-formatter
.
Jupyter-contrib扩展是一系列扩展,这些扩展为Jupyter提供了更多功能,例如jupyter spell-checker
和code-formatter
。
The following commands will install the extensions, as well as a menu based configurator that will help you browse and enable the extensions from the main Jupyter notebook screen.
以下命令将安装扩展,以及基于菜单的配置器,该配置器将帮助您从Jupyter笔记本主屏幕浏览并启用扩展。
The nbextension configurator.
nbextension配置器。
25.从Jupyter笔记本创建演示文稿。 (25. Create a presentation from a Jupyter notebook.)
Damian Avila’s RISE allows you to create a powerpoint style presentation from an existing notebook.
达米安·阿维拉(Damian Avila)的RISE允许您从现有笔记本电脑创建PowerPoint样式的演示文稿。
You can install RISE using conda:
您可以使用conda安装RISE:
conda install -c damianavila82 rise
conda install -c damianavila82 rise
Or alternatively pip:
或点子:
And then run the following code to install and enable the extension:
然后运行以下代码以安装并启用扩展:
jupyter-nbextension install rise --py --sys-prefix jupyter-nbextension enable rise --py --sys-prefix
jupyter-nbextension install rise --py --sys-prefix jupyter-nbextension enable rise --py --sys-prefix
26. Jupyter输出系统 (26. The Jupyter output system)
Notebooks are displayed as HTML and the cell output can be HTML, so you can return virtually anything: video/audio/images.
笔记本显示为HTML,单元格输出可以是HTML,因此您几乎可以返回任何内容:视频/音频/图像。
In this example I scan the folder with images in my repository and show thumbnails of the first 5:
在此示例中,我使用存储库中的图像扫描文件夹,并显示前5个缩略图:
In [12]:
在[12]中:
We can create the same list with a bash command, because magics and bash calls return python variables:
我们可以使用bash命令创建相同的列表,因为magic和bash调用返回python变量:
In [10]:
在[10]中:
names names = = !ls ../images/ml_demonstrations/*.png
! ls ../images/ml_demonstrations/*.png
namesnames [:[: 55 ]
]
Out[10]:
出[10]:
['../images/ml_demonstrations/colah_embeddings.png',
'../images/ml_demonstrations/convnetjs.png',
'../images/ml_demonstrations/decision_tree.png',
'../images/ml_demonstrations/decision_tree_in_course.png',
'../images/ml_demonstrations/dream_mnist.png']
27.“大数据”分析 (27. ‘Big data’ analysis)
A number of solutions are available for querying/processing large data samples:
有许多解决方案可用于查询/处理大数据样本:
28.共享笔记本 (28. Sharing notebooks)
The easiest way to share your notebook is simply using the notebook file (.ipynb), but for those who don’t use Jupyter, you have a few options:
共享笔记本的最简单方法是只使用笔记本文件(.ipynb),但是对于不使用Jupyter的用户,您可以选择以下几种:
- Convert notebooks to html file using the
File > Download as > HTML
Menu option. - Share your notebook file with gists or on github, both of which render the notebooks. See this example.
- If you upload your notebook to a github repository, you can use the handy mybinder service to allow someone half an hour of interactive Jupyter access to your repository.
- Setup your own system with jupyterhub, this is very handy when you organize mini-course or workshop and don’t have time to care about students machines.
- Store your notebook e.g. in dropbox and put the link to nbviewer. nbviewer will render the notebook from whichever source you host it.
- Use the
File > Download as > PDF
menu to save your notebook as a PDF. If you’re going this route, I highly recommend reading Julius Schulz’s excellent article Making publication ready Python notebooks. - Create a blog using Pelican from your Jupyter notebooks.
- 使用
File > Download as > HTML
菜单选项将笔记本转换为html文件。 - 与要点或在github上共享您的笔记本文件,这两种方式都会渲染笔记本。 请参阅此示例 。
- 如果将笔记本上载到github存储库,则可以使用方便的mybinder服务,以允许某人半小时通过交互式Jupyter访问您的存储库。
- 使用jupyterhub设置您自己的系统,这在您组织小型课程或讲习班并且没有时间关心学生机器时非常方便。
- 将您的笔记本存放在Dropbox中,然后将链接放置到nbviewer 。 nbviewer将从您托管它的任何来源渲染笔记本。
- 使用
File > Download as > PDF
菜单将笔记本另存为PDF。 如果您要走这条路,我强烈建议您阅读Julius Schulz的出色文章“ 使出版物可用于Python笔记本” 。 - 在Jupyter笔记本上使用Pelican创建博客 。
你最喜欢什么? (What are your favorites?)
翻译自: https://www.pybloggers.com/2016/10/28-jupyter-notebook-tips-tricks-and-shortcuts/