标签归档:ipython-notebook

熊猫:设置编号。最大行数

问题:熊猫:设置编号。最大行数

我在查看以下内容时遇到问题DataFrame

n = 100
foo = DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)
foo

问题是它不会在ipython笔记本中默认情况下不打印所有行,但是我必须切片才能查看结果行。甚至以下选项也不会更改输出:

pd.set_option('display.max_rows', 500)

有谁知道如何显示整个数组?

I have a problem viewing the following DataFrame:

n = 100
foo = DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)
foo

The problem is that it does not print all rows per default in ipython notebook, but I have to slice to view the resulting rows. Even the following option does not change the output:

pd.set_option('display.max_rows', 500)

Does anyone know how to display the whole array?


回答 0

设置display.max_rows

pd.set_option('display.max_rows', 500)

对于较早版本的熊猫(<= 0.11.0),您需要同时更改display.heightdisplay.max_rows

pd.set_option('display.height', 500)
pd.set_option('display.max_rows', 500)

另请参阅pd.describe_option('display')

您只能一次临时设置一个选项,如下所示:

from IPython.display import display
with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    display(df) #need display to show the dataframe when using with in jupyter
    #some pandas stuff

您还可以将选项重置为默认值,如下所示:

pd.reset_option('display.max_rows')

然后将它们全部重置:

pd.reset_option('all')

Set display.max_rows:

pd.set_option('display.max_rows', 500)

For older versions of pandas (<=0.11.0) you need to change both display.height and display.max_rows.

pd.set_option('display.height', 500)
pd.set_option('display.max_rows', 500)

See also pd.describe_option('display').

You can set an option only temporarily for this one time like this:

from IPython.display import display
with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    display(df) #need display to show the dataframe when using with in jupyter
    #some pandas stuff

You can also reset an option back to its default value like this:

pd.reset_option('display.max_rows')

And reset all of them back:

pd.reset_option('all')


回答 1

就个人而言,我喜欢直接使用赋值语句设置选项,因为iPython使得通过制表符补全很容易找到。我很难记住确切的选项名称是什么,因此此方法对我有用。

例如,我要记住的是,它始于 pd.options

pd.options.<TAB>

大多数选项在 display

pd.options.display.<TAB>

从这里,我通常输出如下所示的当前值:

pd.options.display.max_rows
60

然后,将其设置为我想要的样子:

pd.options.display.max_rows = 100

另外,您应该注意用于选项的上下文管理器,它可以在代码块内临时设置选项。将选项名称作为字符串传递,后跟所需的值。您可以在同一行中传递任意数量的选项:

with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    some pandas stuff

您还可以将选项重置为默认值,如下所示:

pd.reset_option('display.max_rows')

然后将它们全部重置:

pd.reset_option('all')

通过设置选项仍然非常好pd.set_option。我只是发现直接使用属性更容易,并且对get_option和的需求也更少set_option

Personally, I like setting the options directly with an assignment statement as it is easy to find via tab completion thanks to iPython. I find it hard to remember what the exact option names are, so this method works for me.

For instance, all I have to remember is that it begins with pd.options

pd.options.<TAB>

Most of the options are available under display

pd.options.display.<TAB>

From here, I usually output what the current value is like this:

pd.options.display.max_rows
60

I then set it to what I want it to be:

pd.options.display.max_rows = 100

Also, you should be aware of the context manager for options, which temporarily sets the options inside of a block of code. Pass in the option name as a string followed by the value you want it to be. You may pass in any number of options in the same line:

with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    some pandas stuff

You can also reset an option back to its default value like this:

pd.reset_option('display.max_rows')

And reset all of them back:

pd.reset_option('all')

It is still perfectly good to set options via pd.set_option. I just find using the attributes directly is easier and there is less need for get_option and set_option.


回答 2

此注释此答案中已经指出了一点,但是我将尝试对该问题给出更直接的答案:

from IPython.display import display
import numpy as np
import pandas as pd

n = 100
foo = pd.DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)

with pd.option_context("display.max_rows", foo.shape[0]):
    display(foo)

从pandas 0.13.1(pandas 0.13.1发行说明)开始,pandas.option_context可用。根据

[it]允许您执行带有一组选项的代码块,当您退出with块时,这些选项会还原为先前的设置。

It was already pointed in this comment and in this answer, but I’ll try to give a more direct answer to the question:

from IPython.display import display
import numpy as np
import pandas as pd

n = 100
foo = pd.DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)

with pd.option_context("display.max_rows", foo.shape[0]):
    display(foo)

pandas.option_context is available since pandas 0.13.1 (pandas 0.13.1 release notes). According to this,

[it] allow[s] you to execute a codeblock with a set of options that revert to prior settings when you exit the with block.


回答 3

正如@hanleyhansen在评论中指出的那样,从0.18.1版本开始,该display.height选项已被弃用,并说“使用display.max_rows代替”。因此,您只需要像这样配置它:

pd.set_option('display.max_rows', 500)

请参阅发行说明-pandas 0.18.1文档

现在已弃用的display.height,display.width仅是一个格式选项,无法控制摘要的触发,类似于<0.11.0。

As @hanleyhansen noted in a comment, as of version 0.18.1, the display.height option is deprecated, and says “use display.max_rows instead”. So you just have to configure it like this:

pd.set_option('display.max_rows', 500)

See the Release Notes — pandas 0.18.1 documentation:

Deprecated display.height, display.width is now only a formatting option does not control triggering of summary, similar to < 0.11.0.


回答 4

pd.set_option('display.max_rows', 500)
df

不工作的Jupyter!
而是使用:

pd.set_option('display.max_rows', 500)
df.head(500)
pd.set_option('display.max_rows', 500)
df

Does not work in Jupyter!
Instead use:

pd.set_option('display.max_rows', 500)
df.head(500)

回答 5

正如这个答案类似的问题,不存在需要破解的设置。编写起来要简单得多:

print(foo.to_string())

As in this answer to a similar question, there is no need to hack settings. It is much simpler to write:

print(foo.to_string())

iPython Notebook清除代码中的单元格输出

问题:iPython Notebook清除代码中的单元格输出

在iPython笔记本中,我有一个while循环,可print实时侦听串行端口和接收到的数据。

我想要实现的仅显示最新接收到的数据(即,仅一行显示最新数据。在单元格输出区域中不滚动)

我需要的是(我认为)当我收到新数据时清除旧的单元格输出,然后打印新数据。我想知道如何以编程方式清除旧数据?

In a iPython notebook, I have a while loop that listens to a Serial port and print the received data in real time.

What I want to achieve to only show the latest received data (i.e only one line showing the most recent data. no scrolling in the cell output area)

What I need(i think) is to clear the old cell output when I receives new data, and then prints the new data. I am wondering how can I clear old data programmatically ?


回答 0

您可以IPython.display.clear_output用来清除单元格的输出。

from IPython.display import clear_output

for i in range(10):
    clear_output(wait=True)
    print("Hello World!")

在此循环结束时,您只会看到一个Hello World!

没有代码示例,给您工作代码并不容易。最好缓冲最近的n个事件是一个好策略。每当缓冲区更改时,您都可以清除单元格的输出并再次打印缓冲区。

You can use IPython.display.clear_output to clear the output of a cell.

from IPython.display import clear_output

for i in range(10):
    clear_output(wait=True)
    print("Hello World!")

At the end of this loop you will only see one Hello World!.

Without a code example it’s not easy to give you working code. Probably buffering the latest n events is a good strategy. Whenever the buffer changes you can clear the cell’s output and print the buffer again.


回答 1

如果您像我一样来到这里,希望使用Jupitter在Jupyter的Julia笔记本中对地块做同样的事情,则可以使用:

    IJulia.clear_output(true)

所以对于多次运行的动画情节

    if nrun==1  
      display(plot(x,y))         # first plot
    else 
      IJulia.clear_output(true)  # clear the window (as above)
      display(plot!(x,y))        # plot! overlays the plot
    end

没有clear_output调用,所有图将单独显示。

And in case you come here, like I did, looking to do the same thing for plots in a Julia notebook in Jupyter, using Plots, you can use:

    IJulia.clear_output(true)

so for a kind of animated plot of multiple runs

    if nrun==1  
      display(plot(x,y))         # first plot
    else 
      IJulia.clear_output(true)  # clear the window (as above)
      display(plot!(x,y))        # plot! overlays the plot
    end

Without the clear_output call, all plots appear separately.


回答 2

您可以使用IPython.display.clear_output清除输出,如cel的答案所述。我要补充一点,对我而言,最好的解决方案是使用以下参数组合进行打印,而不会出现笔记本的“晃动”:

from IPython.display import clear_output

for i in range(10):
    clear_output(wait=True)
    print(i, flush=True)

You can use the IPython.display.clear_output to clear the output as mentioned in cel’s answer. I would add that for me the best solution was to use this combination of parameters to print without any “shakiness” of the notebook:

from IPython.display import clear_output

for i in range(10):
    clear_output(wait=True)
    print(i, flush=True)

如何正确关闭IPython Notebook?

问题:如何正确关闭IPython Notebook?

如何正确关闭IPython Notebook?

目前,我只是关闭浏览器选项卡,然后Ctrl+C在终端中使用。
不幸的是,exit()滴答和滴答都Kill kernel upon exit没有帮助(它们确实杀死了它们的内核,但是没有退出iPython)。

How to close IPython Notebook properly?

Currently, I just close the browser tabs and then use Ctrl+C in the terminal.
Unfortunately, neither exit() nor ticking Kill kernel upon exit does help (they do kill the kernel they but don’t exit the iPython).


回答 0

当前没有比终端中的Ctrl + C更好的方法了。

我们正在考虑如何进行显式关机,但是笔记本作为单用户应用程序(用户可以自由停止它)和作为多用户服务器(只能由管理员使用)之间存在一定的压力。阻止它。我们还没有弄清楚如何处理差异。

(对于将来的读者来说,就是0.12已发布而0.13正在开发中的情况。)

2017年12月更新

IPython Notebook已成为Jupyter Notebook。最新版本添加了jupyter notebook stopshell命令,该命令将关闭在该系统上运行的服务器。如果不是默认端口8888,则可以在命令行中传递端口号。

您还可以使用nbmanager,它是一个桌面应用程序,可以显示正在运行的服务器并关闭它们。

最后,我们正在努力添加:

  • 一个配置选项,如果您在指定时间内不使用服务器,则会自动关闭服务器。
  • 用户界面中的一个按钮,用于关闭服务器。(我们知道花了这么长时间真是太疯狂了。更改UI引发了争议。)

There isn’t currently a better way to do it than Ctrl+C in the terminal.

We’re thinking about how to have an explicit shutdown, but there’s some tension between the notebook as a single-user application, where the user is free to stop it, and as a multi-user server, where only an admin should be able to stop it. We haven’t quite worked out how to handle the differences yet.

(For future readers, this is the situation with 0.12 released and 0.13 in development.)

Update December 2017

The IPython Notebook has become the Jupyter Notebook. A recent version has added a jupyter notebook stop shell command which will shut down a server running on that system. You can pass the port number at the command line if it’s not the default port 8888.

You can also use nbmanager, a desktop application which can show running servers and shut them down.

Finally, we are working on adding:

  • A config option to automatically shut down the server if you don’t use it for a specified time.
  • A button in the user interface to shut the server down. (We know it’s a bit crazy that it has taken this long. Changing UI is controversial.)

回答 1

如果您像我一样在后台运行jupyter:

jupyter notebook &> /dev/null &

然后要完全退出jupyter,而不是Ctl-C,请使用别名命令:

echo 'alias quitjupyter="kill $(pgrep jupyter)"' >> ~/.bashrc

重新启动终端。杀死所有jupyter实例:

quitjupyter

注意:如上所示,在单引号内使用双引号。另一种方法是在将表达式写入您的.bashrc之前先对表达式求值(您要编写的命令本身不是’kill 1430’或可能与当前jupyter实例相关的任何进程号)。当然,您可以使用任何所需的别名。我实际上使用’qjup’:

echo 'alias qjup="kill $(pgrep jupyter)"' >> ~/.bashrc

重新启动终端。杀死所有jupyter实例:

qjup

If you run jupyter in the background like me:

jupyter notebook &> /dev/null &

Then to exit jupyter completely, instead of Ctl-C, make an alias command:

echo 'alias quitjupyter="kill $(pgrep jupyter)"' >> ~/.bashrc

Restart your terminal. Kill all jupyter instances:

quitjupyter

Note: use double quotes inside of single quotes as shown above. The other way around will evaluate the expression before writing it to your .bashrc (you want to write the command itself not ‘kill 1430’ or whatever process number may be associated with a current jupyter instance). Of course you can use any alias you wish. I actually use ‘qjup’:

echo 'alias qjup="kill $(pgrep jupyter)"' >> ~/.bashrc

Restart your terminal. Kill all jupyter instances:

qjup

回答 2

我认为已接受的答案已过时,不再有效。

您可以从文件菜单项上的Web界面终止Jupyter Notebook。

将鼠标光标移至“关闭并停止”位置时,将看到以下说明。

当您单击“关闭并停止”时,您将在终端屏幕上看到以下消息。

I think accepted answer outdated and is not valid anymore.

You can terminate jupyter notebook from web interface on file menü item.

When you move Mouse cursor on “close and halt”, you will see following explanation.

And when you click “close and halt”, you will see following message on terminal screen.


回答 3

第一步是保存所有打开的笔记本。然后考虑关闭正在运行的Jupyter Notebook。您可以使用以下简单命令:

$ jupyter notebook stop 
Shutting down server on port 8888 ...

它也将端口号作为参数,您可以正常关闭jupyter笔记本。

例如:

jupyter notebook stop 8889 
Shutting down server on port 8889 ...

此外,要了解您当前的juypter实例正在运行,请检查以下命令:

shell> juypter notebook list 
Currently running servers:
http://localhost:8888/?token=ef12021898c435f865ec706d7c9af8607a7ba58bbee98632 :: /Users/username/jupyter-notebooks [/code]

First step is to save all open notebooks. And then think about shutting down your running Jupyter Notebook. You can use this simple command:

$ jupyter notebook stop 
Shutting down server on port 8888 ...

Which also takes the port number as argument and you can shut down the jupyter notebook gracefully.

For eg:

jupyter notebook stop 8889 
Shutting down server on port 8889 ...

Additionally to know your current juypter instance running, check below command:

shell> juypter notebook list 
Currently running servers:
http://localhost:8888/?token=ef12021898c435f865ec706d7c9af8607a7ba58bbee98632 :: /Users/username/jupyter-notebooks [/code]

回答 4

这些命令对我有用:

jupyter notebook list # shows the running notebooks and their port-numbers
                      # (for instance: 8080)
lsof -n -i4TCP:[port-number] # shows PID.
kill -9 [PID] # kill the process.

这个答案改编自这里

These commands worked for me:

jupyter notebook list # shows the running notebooks and their port-numbers
                      # (for instance: 8080)
lsof -n -i4TCP:[port-number] # shows PID.
kill -9 [PID] # kill the process.

This answer was adapted from here.


回答 5

如果没有其他作用,请尝试从任务管理器(如果是Windows)中终止pythonw进程。

Try killing the pythonw process from the Task Manager (if Windows) if nothing else works.


回答 6

Linux (Ubuntu 14.04)

如前所述,尝试通过先转到ipynb / jupyter浏览器会话中的“运行”选项卡来正确终止ipython Notebook进程,然后检查控制台上打开的终端并使用ctrl-c关闭。如果可能,应避免后者。

如果您运行,ipython notebook list并继续看到在不同端口上运行ipython服务器,请记下现有笔记本计算机将服务于哪些端口。然后关闭您的TCP端口:

fuser -k 'port#'/tcp 

我不确定这样做是否还会涉及其他风险。如果是这样,请告诉我。

Linux (Ubuntu 14.04)

As mentioned, try to kill ipython notebook processes properly by first going to the “running” tab in your ipynb/jupyter browser session, and then check open terminals on your console and shut down with ctrl-c. The latter should be avoided if possible.

If you run an ipython notebook list and continue to see running ipython servers at different ports, make note of which ports the existing notebooks are being served to. Then shut down your TCP ports:

fuser -k 'port#'/tcp 

I’m not sure if there are other risks involved with doing this. If so, let me know.


回答 7

环境


我的操作系统是Ubuntu 16.04,jupyter是4.3.0。

方法


首先,我在浏览器的主页上注销了jupyter(注销按钮在右上角)

第二,输入Ctrl + C您的终端,它显示:

[I 15:59:48.407 NotebookApp]从本地目录中断服务笔记本:/ home / Username 0活动内核

Jupyter Notebook在以下位置运行: http:// localhost:8888 /?token = a572c743dfb73eee28538f9a181bf4d9ad412b19fbb96c82

关闭此笔记本服务器(y / [n])?

最后一步,y在5秒钟内键入,如果显示:

[C 15:59:50.407 NotebookApp]已确认关闭
[I 15:59:50.408 NotebookApp]正在关闭内核

恭喜!您成功关闭了jupyter。

Environment


My OS is Ubuntu 16.04 and jupyter is 4.3.0.

Method


First, i logged out jupyter at its homepage on browser(the logout button is at top-right)

Second, type in Ctrl + C in your terminal and it shows:

[I 15:59:48.407 NotebookApp]interrupted Serving notebooks from local directory: /home/Username 0 active kernels

The Jupyter Notebook is running at: http://localhost:8888/?token=a572c743dfb73eee28538f9a181bf4d9ad412b19fbb96c82

Shutdown this notebook server (y/[n])?

Last step, type in y within 5 sec, and if it shows:

[C 15:59:50.407 NotebookApp] Shutdown confirmed
[I 15:59:50.408 NotebookApp] Shutting down kernels

Congrats! You close your jupyter successfully.


回答 8

实际上,我相信有一种比使用kill或task manager杀死进程更干净的方法。

在Jupyter Notebook仪表板(首次启动“ Jupyter Notebook”时会看到的浏览器界面)中,浏览到浏览器中已关闭但其内核可能仍在运行的笔记本文件的位置。

iPython Notebook文件带有书本图标,如果它具有正在运行的内核,则显示为绿色,如果内核未运行,则显示为灰色。

只需选中正在运行的文件旁边的复选框,然后单击出现在文件上方的“关机”按钮即可。

这将正确关闭与该特定笔记本电脑关联的内核。

Actually, I believe there’s a cleaner way than killing the process(es) using kill or task manager.

In the Jupyter Notebook Dashboard (the browser interface you see when you first launch ‘jupyter notebook’), browse to the location of notebook files you have closed in the browser, but whose kernels may still be running.

iPython Notebook files appear with a book icon, shown in green if it has a running kernel, or gray if the kernel is not running.

Just select the tick box next to the running file, then click on the Shutdown button that appears above it.

This will properly shut down the kernel associated with that specific notebook.


回答 9

在浏览器会话中,您也可以转到Kernel,然后单击Restart and Clear Output

In the browser session you can also go to Kernel and then click Restart and Clear Output.


回答 10

我复制从Jupyter / IPython的笔记本电脑快速入门指南文档粘贴,发布了2月13日,2018年 http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html

1.3.3关闭笔记本:内核关闭打开笔记本时,将自动启动其“计算引擎”(称为内核)。关闭笔记本浏览器选项卡将不会关闭内核,而是内核将一直运行直到被明确关闭。要关闭内核,请转到关联的笔记本,然后单击菜单文件->关闭并暂停。另外,笔记本仪表板有一个名为“运行”的选项卡,其中显示了所有正在运行的笔记本(即内核),并允许将其关闭(通过单击“关机”按钮)。

摘要:首先关闭并停止笔记本计算机的运行。

1.3.2关闭Jupyter Notebook应用程序关闭浏览器(或选项卡)将不会关闭Jupyter Notebook应用程序。要完全关闭它,您需要关闭关联的终端。更详细地说,Jupyter Notebook应用程序是一台服务器,显示在浏览器中的默认地址(http:// localhost:8888)。关闭浏览器不会关闭服务器。您可以重新打开以前的地址,然后Jupyter Notebook应用将重新显示。您可以运行Jupyter Notebook应用程序的许多副本,它们将显示在相似的地址上(只有端口“:”之后的数字才会为每个新副本递增)。由于只有一个Jupyter Notebook App,您已经可以打开许多笔记本,因此我们不建议您运行Jupyter Notebook App的多个副本。

摘要:其次,退出您从其发射Jupyter的终端。

I am copy pasting from the Jupyter/IPython Notebook Quick Start Guide Documentation, released on Feb 13, 2018. http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html

1.3.3 Close a notebook: kernel shut down When a notebook is opened, its “computational engine” (called the kernel) is automatically started. Closing the notebook browser tab, will not shut down the kernel, instead the kernel will keep running until is explicitly shut down. To shut down a kernel, go to the associated notebook and click on menu File -> Close and Halt. Alternatively, the Notebook Dashboard has a tab named Running that shows all the running notebooks (i.e. kernels) and allows shutting them down (by clicking on a Shutdown button).

Summary: First close and halt the notebooks running.

1.3.2 Shut down the Jupyter Notebook App Closing the browser (or the tab) will not close the Jupyter Notebook App. To completely shut it down you need to close the associated terminal. In more detail, the Jupyter Notebook App is a server that appears in your browser at a default address (http://localhost:8888). Closing the browser will not shut down the server. You can reopen the previous address and the Jupyter Notebook App will be redisplayed. You can run many copies of the Jupyter Notebook App and they will show up at a similar address (only the number after “:”, which is the port, will increment for each new copy). Since with a single Jupyter Notebook App you can already open many notebooks, we do not recommend running multiple copies of Jupyter Notebook App.

Summary: Second, quit the terminal from which you fired Jupyter.


回答 11

步骤1-在shell上,只需执行control + z(control + c)步骤2 _关闭Web浏览器

Step 1 – On shell just do control+z (control+c) Step 2 _ close the web browser


如何在ipython笔记本中显示PIL图像

问题:如何在ipython笔记本中显示PIL图像

这是我的代码

from PIL import Image
pil_im = Image.open('data/empire.jpg')

我想对其进行一些图像处理,然后在屏幕上显示它。
我在python笔记本中显示PIL图像时遇到问题。

我努力了:

print pil_im

而只是

pil_im

但是两者都给我:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=569x800 at 0x10ECA0710>

This is my code

from PIL import Image
pil_im = Image.open('data/empire.jpg')

I would like to do some image manipulation on it, and then show it on screen.
I am having problem with showing PIL Image in python notebook.

I have tried:

print pil_im

And just

pil_im

But both just give me:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=569x800 at 0x10ECA0710>

回答 0

您可以使用IPython Module: display加载图像。您可以从Doc阅读更多内容。

from IPython.display import Image 
pil_img = Image(filename='data/empire.jpg')
display(pil_img)

更新

由于OP的要求是使用PIL,如果要显示嵌入式图像,也可以使用matplotlib.pyplot.imshownumpy.asarray例如:

from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image

%matplotlib inline
pil_im = Image.open('data/empire.jpg', 'r')
imshow(np.asarray(pil_im))

如果只需要预览而不是内联,则可以这样使用show

pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()

You can use IPython’s Module: display to load the image. You can read more from the Doc.

from IPython.display import Image 
pil_img = Image(filename='data/empire.jpg')
display(pil_img)

updated

As OP’s requirement is to use PIL, if you want to show inline image, you can use matplotlib.pyplot.imshow with numpy.asarray like this too:

from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image

%matplotlib inline
pil_im = Image.open('data/empire.jpg', 'r')
imshow(np.asarray(pil_im))

If you only require a preview rather than an inline, you may just use show like this:

pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()

回答 1

使用IPython显示器在笔记本中渲染PIL图像。

from PIL import Image               # to load images
from IPython.display import display # to display images

pil_im = Image.open('path/to/image.jpg')
display(pil_im)

Use IPython display to render PIL images in a notebook.

from PIL import Image               # to load images
from IPython.display import display # to display images

pil_im = Image.open('path/to/image.jpg')
display(pil_im)

回答 2

我发现这有效

# source: http://nbviewer.ipython.org/gist/deeplook/5162445
from io import BytesIO

from IPython import display
from PIL import Image


def display_pil_image(im):
   """Displayhook function for PIL Images, rendered as PNG."""

   b = BytesIO()
   im.save(b, format='png')
   data = b.getvalue()

   ip_img = display.Image(data=data, format='png', embed=True)
   return ip_img._repr_png_()


# register display func with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png']
dpi = png_formatter.for_type(Image.Image, display_pil_image)

在此之后,我可以做:

pil_im

但这必须是单元格中的最后一行,print之后没有

I found that this is working

# source: http://nbviewer.ipython.org/gist/deeplook/5162445
from io import BytesIO

from IPython import display
from PIL import Image


def display_pil_image(im):
   """Displayhook function for PIL Images, rendered as PNG."""

   b = BytesIO()
   im.save(b, format='png')
   data = b.getvalue()

   ip_img = display.Image(data=data, format='png', embed=True)
   return ip_img._repr_png_()


# register display func with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png']
dpi = png_formatter.for_type(Image.Image, display_pil_image)

After this I can just do:

pil_im

But this must be last line in cell, with no print after it


回答 3

案例python3

from PIL import Image
from IPython.display import HTML
from io import BytesIO
from base64 import b64encode

pil_im = Image.open('data/empire.jpg')
b = BytesIO()  
pil_im.save(b, format='png')
HTML("<img src='data:image/png;base64,{0}'/>".format(b64encode(b.getvalue()).decode('utf-8')))

case python3

from PIL import Image
from IPython.display import HTML
from io import BytesIO
from base64 import b64encode

pil_im = Image.open('data/empire.jpg')
b = BytesIO()  
pil_im.save(b, format='png')
HTML("<img src='data:image/png;base64,{0}'/>".format(b64encode(b.getvalue()).decode('utf-8')))

回答 4

使用枕头在jupyter中简单得多。

from PIL import Image
image0=Image.open('image.png')
image0

much simpler in jupyter using pillow.

from PIL import Image
image0=Image.open('image.png')
image0

回答 5

您可以使用PIL包中的Image类打开图像,并直接使用plt.imshow显示它。

# First import libraries.
from PIL import Image
import matplotlib.pyplot as plt

# The folliwing line is useful in Jupyter notebook
%matplotlib inline

# Open your file image using the path
img = Image.open(<path_to_image>)

# Since plt knows how to handle instance of the Image class, just input your loaded image to imshow method
plt.imshow(img)

You can open an image using the Image class from the package PIL and display it with plt.imshow directly.

# First import libraries.
from PIL import Image
import matplotlib.pyplot as plt

# The folliwing line is useful in Jupyter notebook
%matplotlib inline

# Open your file image using the path
img = Image.open(<path_to_image>)

# Since plt knows how to handle instance of the Image class, just input your loaded image to imshow method
plt.imshow(img)

回答 6

如果使用pylab扩展,则可以将图像转换为numpy数组,并使用matplotlib的imshow。

%pylab # only if not started with the --pylab option
imshow(array(pil_im))

编辑:如评论中所述,不推荐使用pylab模块,因此请改用matplotlib magic并显式导入函数:

%matplotlib
from matplotlib.pyplot import imshow 
imshow(array(pil_im))

If you are using the pylab extension, you could convert the image to a numpy array and use matplotlib’s imshow.

%pylab # only if not started with the --pylab option
imshow(array(pil_im))

EDIT: As mentioned in the comments, the pylab module is deprecated, so use the matplotlib magic instead and import the function explicitly:

%matplotlib
from matplotlib.pyplot import imshow 
imshow(array(pil_im))

回答 7

根据其他答案和我的尝试,最好的经验是,首先安装,枕头和scipy,然后在jupyter笔记本上使用以下起始代码:

%matplotlib inline
from matplotlib.pyplot import imshow
from scipy.misc import imread

imshow(imread('image.jpg', 1))

Based on other answers and my tries, best experience would be first installing, pillow and scipy, then using the following starting code on your jupyter notebook:

%matplotlib inline
from matplotlib.pyplot import imshow
from scipy.misc import imread

imshow(imread('image.jpg', 1))

回答 8

使用标准numpy,matplotlib和PIL的更干净的Python3版本。合并从URL打开的答案。

import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

pil_im = Image.open('image.jpg')
## Uncomment to open from URL
#import requests
#r = requests.get('https://www.vegvesen.no/public/webkamera/kamera?id=131206')
#pil_im = Image.open(BytesIO(r.content))
im_array = np.asarray(pil_im)
plt.imshow(im_array)
plt.show()

A cleaner Python3 version that use standard numpy, matplotlib and PIL. Merging the answer for opening from URL.

import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

pil_im = Image.open('image.jpg')
## Uncomment to open from URL
#import requests
#r = requests.get('https://www.vegvesen.no/public/webkamera/kamera?id=131206')
#pil_im = Image.open(BytesIO(r.content))
im_array = np.asarray(pil_im)
plt.imshow(im_array)
plt.show()

回答 9

我建议以下安装时不要显示任何图像show img.show()(来自PIL导入图像)

$ sudo apt-get install imagemagick

I suggest following installation by no image show img.show() (from PIL import Image)

$ sudo apt-get install imagemagick


回答 10

只需使用

from IPython.display import Image 
Image('image.png')

Just use

from IPython.display import Image 
Image('image.png')

如何使用iPython中的pandas库读取.xlsx文件?

问题:如何使用iPython中的pandas库读取.xlsx文件?

我想使用python的Pandas库读取.xlsx文件,并将数据移植到postgreSQL表中。

到目前为止,我所能做的就是:

import pandas as pd
data = pd.ExcelFile("*File Name*")

现在,我知道该步骤已成功执行,但是我想知道如何解析已读取的excel文件,以便可以了解excel中的数据如何映射到变量数据中的数据。
我了解到,如果我没有记错的话,数据就是一个Dataframe对象。因此,我如何解析此dataframe对象以逐行提取每一行。

I want to read a .xlsx file using the Pandas Library of python and port the data to a postgreSQL table.

All I could do up until now is:

import pandas as pd
data = pd.ExcelFile("*File Name*")

Now I know that the step got executed successfully, but I want to know how i can parse the excel file that has been read so that I can understand how the data in the excel maps to the data in the variable data.
I learnt that data is a Dataframe object if I’m not wrong. So How do i parse this dataframe object to extract each line row by row.


回答 0

我通常会DataFrame为每个工作表创建一个包含的字典:

xl_file = pd.ExcelFile(file_name)

dfs = {sheet_name: xl_file.parse(sheet_name) 
          for sheet_name in xl_file.sheet_names}

更新:在pandas 0.21.0+版本中,您可以通过传递sheet_name=Noneread_excel

dfs = pd.read_excel(file_name, sheet_name=None)

在0.20及sheetname更低版本中,它是而不是sheet_name(现在已弃用,而转而支持上述内容):

dfs = pd.read_excel(file_name, sheetname=None)

I usually create a dictionary containing a DataFrame for every sheet:

xl_file = pd.ExcelFile(file_name)

dfs = {sheet_name: xl_file.parse(sheet_name) 
          for sheet_name in xl_file.sheet_names}

Update: In pandas version 0.21.0+ you will get this behavior more cleanly by passing sheet_name=None to read_excel:

dfs = pd.read_excel(file_name, sheet_name=None)

In 0.20 and prior, this was sheetname rather than sheet_name (this is now deprecated in favor of the above):

dfs = pd.read_excel(file_name, sheetname=None)

回答 1

from pandas import read_excel
# find your sheet name at the bottom left of your excel file and assign 
# it to my_sheet 
my_sheet = 'Sheet1' # change it to your sheet name
file_name = 'products_and_categories.xlsx' # change it to the name of your excel file
df = read_excel(file_name, sheet_name = my_sheet)
print(df.head()) # shows headers with top 5 rows
from pandas import read_excel
# find your sheet name at the bottom left of your excel file and assign 
# it to my_sheet 
my_sheet = 'Sheet1' # change it to your sheet name
file_name = 'products_and_categories.xlsx' # change it to the name of your excel file
df = read_excel(file_name, sheet_name = my_sheet)
print(df.head()) # shows headers with top 5 rows

回答 2

DataFrame的read_excel方法类似于read_csv方法:

dfs = pd.read_excel(xlsx_file, sheetname="sheet1")


Help on function read_excel in module pandas.io.excel:

read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0, index_col=None, names=None, parse_cols=None, parse_dates=False, date_parser=None, na_values=None, thousands=None, convert_float=True, has_index_names=None, converters=None, true_values=None, false_values=None, engine=None, squeeze=False, **kwds)
    Read an Excel table into a pandas DataFrame

    Parameters
    ----------
    io : string, path object (pathlib.Path or py._path.local.LocalPath),
        file-like object, pandas ExcelFile, or xlrd workbook.
        The string could be a URL. Valid URL schemes include http, ftp, s3,
        and file. For file URLs, a host is expected. For instance, a local
        file could be file://localhost/path/to/workbook.xlsx
    sheetname : string, int, mixed list of strings/ints, or None, default 0

        Strings are used for sheet names, Integers are used in zero-indexed
        sheet positions.

        Lists of strings/integers are used to request multiple sheets.

        Specify None to get all sheets.

        str|int -> DataFrame is returned.
        list|None -> Dict of DataFrames is returned, with keys representing
        sheets.

        Available Cases

        * Defaults to 0 -> 1st sheet as a DataFrame
        * 1 -> 2nd sheet as a DataFrame
        * "Sheet1" -> 1st sheet as a DataFrame
        * [0,1,"Sheet5"] -> 1st, 2nd & 5th sheet as a dictionary of DataFrames
        * None -> All sheets as a dictionary of DataFrames

    header : int, list of ints, default 0
        Row (0-indexed) to use for the column labels of the parsed
        DataFrame. If a list of integers is passed those row positions will
        be combined into a ``MultiIndex``
    skiprows : list-like
        Rows to skip at the beginning (0-indexed)
    skip_footer : int, default 0
        Rows at the end to skip (0-indexed)
    index_col : int, list of ints, default None
        Column (0-indexed) to use as the row labels of the DataFrame.
        Pass None if there is no such column.  If a list is passed,
        those columns will be combined into a ``MultiIndex``
    names : array-like, default None
        List of column names to use. If file contains no header row,
        then you should explicitly pass header=None
    converters : dict, default None
        Dict of functions for converting values in certain columns. Keys can
        either be integers or column labels, values are functions that take one
        input argument, the Excel cell content, and return the transformed
        content.
    true_values : list, default None
        Values to consider as True

        .. versionadded:: 0.19.0

    false_values : list, default None
        Values to consider as False

        .. versionadded:: 0.19.0

    parse_cols : int or list, default None
        * If None then parse all columns,
        * If int then indicates last column to be parsed
        * If list of ints then indicates list of column numbers to be parsed
        * If string then indicates comma separated list of column names and
          column ranges (e.g. "A:E" or "A,C,E:F")
    squeeze : boolean, default False
        If the parsed data only contains one column then return a Series
    na_values : scalar, str, list-like, or dict, default None
        Additional strings to recognize as NA/NaN. If dict passed, specific
        per-column NA values. By default the following values are interpreted
        as NaN: '', '#N/A', '#N/A N/A', '#NA', '-1.#IND', '-1.#QNAN', '-NaN', '-nan',
    '1.#IND', '1.#QNAN', 'N/A', 'NA', 'NULL', 'NaN', 'nan'.
    thousands : str, default None
        Thousands separator for parsing string columns to numeric.  Note that
        this parameter is only necessary for columns stored as TEXT in Excel,
        any numeric columns will automatically be parsed, regardless of display
        format.
    keep_default_na : bool, default True
        If na_values are specified and keep_default_na is False the default NaN
        values are overridden, otherwise they're appended to.
    verbose : boolean, default False
        Indicate number of NA values placed in non-numeric columns
    engine: string, default None
        If io is not a buffer or path, this must be set to identify io.
        Acceptable values are None or xlrd
    convert_float : boolean, default True
        convert integral floats to int (i.e., 1.0 --> 1). If False, all numeric
        data will be read in as floats: Excel stores all numbers as floats
        internally
    has_index_names : boolean, default None
        DEPRECATED: for version 0.17+ index names will be automatically
        inferred based on index_col.  To read Excel output from 0.16.2 and
        prior that had saved index names, use True.

    Returns
    -------
    parsed : DataFrame or Dict of DataFrames
        DataFrame from the passed in Excel file.  See notes in sheetname
        argument for more information on when a Dict of Dataframes is returned.

DataFrame’s read_excel method is like read_csv method:

dfs = pd.read_excel(xlsx_file, sheetname="sheet1")


Help on function read_excel in module pandas.io.excel:

read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0, index_col=None, names=None, parse_cols=None, parse_dates=False, date_parser=None, na_values=None, thousands=None, convert_float=True, has_index_names=None, converters=None, true_values=None, false_values=None, engine=None, squeeze=False, **kwds)
    Read an Excel table into a pandas DataFrame

    Parameters
    ----------
    io : string, path object (pathlib.Path or py._path.local.LocalPath),
        file-like object, pandas ExcelFile, or xlrd workbook.
        The string could be a URL. Valid URL schemes include http, ftp, s3,
        and file. For file URLs, a host is expected. For instance, a local
        file could be file://localhost/path/to/workbook.xlsx
    sheetname : string, int, mixed list of strings/ints, or None, default 0

        Strings are used for sheet names, Integers are used in zero-indexed
        sheet positions.

        Lists of strings/integers are used to request multiple sheets.

        Specify None to get all sheets.

        str|int -> DataFrame is returned.
        list|None -> Dict of DataFrames is returned, with keys representing
        sheets.

        Available Cases

        * Defaults to 0 -> 1st sheet as a DataFrame
        * 1 -> 2nd sheet as a DataFrame
        * "Sheet1" -> 1st sheet as a DataFrame
        * [0,1,"Sheet5"] -> 1st, 2nd & 5th sheet as a dictionary of DataFrames
        * None -> All sheets as a dictionary of DataFrames

    header : int, list of ints, default 0
        Row (0-indexed) to use for the column labels of the parsed
        DataFrame. If a list of integers is passed those row positions will
        be combined into a ``MultiIndex``
    skiprows : list-like
        Rows to skip at the beginning (0-indexed)
    skip_footer : int, default 0
        Rows at the end to skip (0-indexed)
    index_col : int, list of ints, default None
        Column (0-indexed) to use as the row labels of the DataFrame.
        Pass None if there is no such column.  If a list is passed,
        those columns will be combined into a ``MultiIndex``
    names : array-like, default None
        List of column names to use. If file contains no header row,
        then you should explicitly pass header=None
    converters : dict, default None
        Dict of functions for converting values in certain columns. Keys can
        either be integers or column labels, values are functions that take one
        input argument, the Excel cell content, and return the transformed
        content.
    true_values : list, default None
        Values to consider as True

        .. versionadded:: 0.19.0

    false_values : list, default None
        Values to consider as False

        .. versionadded:: 0.19.0

    parse_cols : int or list, default None
        * If None then parse all columns,
        * If int then indicates last column to be parsed
        * If list of ints then indicates list of column numbers to be parsed
        * If string then indicates comma separated list of column names and
          column ranges (e.g. "A:E" or "A,C,E:F")
    squeeze : boolean, default False
        If the parsed data only contains one column then return a Series
    na_values : scalar, str, list-like, or dict, default None
        Additional strings to recognize as NA/NaN. If dict passed, specific
        per-column NA values. By default the following values are interpreted
        as NaN: '', '#N/A', '#N/A N/A', '#NA', '-1.#IND', '-1.#QNAN', '-NaN', '-nan',
    '1.#IND', '1.#QNAN', 'N/A', 'NA', 'NULL', 'NaN', 'nan'.
    thousands : str, default None
        Thousands separator for parsing string columns to numeric.  Note that
        this parameter is only necessary for columns stored as TEXT in Excel,
        any numeric columns will automatically be parsed, regardless of display
        format.
    keep_default_na : bool, default True
        If na_values are specified and keep_default_na is False the default NaN
        values are overridden, otherwise they're appended to.
    verbose : boolean, default False
        Indicate number of NA values placed in non-numeric columns
    engine: string, default None
        If io is not a buffer or path, this must be set to identify io.
        Acceptable values are None or xlrd
    convert_float : boolean, default True
        convert integral floats to int (i.e., 1.0 --> 1). If False, all numeric
        data will be read in as floats: Excel stores all numbers as floats
        internally
    has_index_names : boolean, default None
        DEPRECATED: for version 0.17+ index names will be automatically
        inferred based on index_col.  To read Excel output from 0.16.2 and
        prior that had saved index names, use True.

    Returns
    -------
    parsed : DataFrame or Dict of DataFrames
        DataFrame from the passed in Excel file.  See notes in sheetname
        argument for more information on when a Dict of Dataframes is returned.

回答 3

如果您不知道或无法打开excel文件以签入ubuntu(在我的情况下为Python 3.6.7,ubuntu 18.04),则可以使用参数index_col(index_col = 0)来代替工作表名称。第一张)

import pandas as pd
file_name = 'some_data_file.xlsx' 
df = pd.read_excel(file_name, index_col=0)
print(df.head()) # print the first 5 rows

Instead of using a sheet name, in case you don’t know or can’t open the excel file to check in ubuntu (in my case, Python 3.6.7, ubuntu 18.04), I use the parameter index_col (index_col=0 for the first sheet)

import pandas as pd
file_name = 'some_data_file.xlsx' 
df = pd.read_excel(file_name, index_col=0)
print(df.head()) # print the first 5 rows

回答 4

将电子表格文件名分配给 file

加载电子表格

打印工作表名称

通过名称将表加载到DataFrame中:df1

file = 'example.xlsx'
xl = pd.ExcelFile(file)
print(xl.sheet_names)
df1 = xl.parse('Sheet1')

Assign spreadsheet filename to file

Load spreadsheet

Print the sheet names

Load a sheet into a DataFrame by name: df1

file = 'example.xlsx'
xl = pd.ExcelFile(file)
print(xl.sheet_names)
df1 = xl.parse('Sheet1')

回答 5

如果在使用read_excel()函数打开的文件上使用open(),请确保将其添加rb到打开函数中,以避免编码错误

If you use read_excel() on a file opened using the function open(), make sure to add rb to the open function to avoid encoding errors


如何使IPython Notebook运行Python 3?

问题:如何使IPython Notebook运行Python 3?

我是Python的新手。

  1. 我安装了Anaconda,效果很好。
  2. 我按照Anaconda cmd行说明设置了Python 3环境,效果很好。
  3. 我将Anaconda的Python 3环境设置为Pycharm的解释器,效果很好。
  4. 我启动了Anaconda“ launcher.app”,并启动了IPython Notebook。但是,iPython Notebook正在运行Python 2而不是3。

经过三个多小时的Google搜索,我无法弄清楚如何将IPython Notebook设置为运行Python 3而不是2。

I am new to Python to bear with me.

  1. I installed Anaconda, works great.
  2. I setup a Python 3 environment following the Anaconda cmd line instructions, works great.
  3. I setup Anaconda’s Python 3 environment as Pycharm’s interpreter, works great.
  4. I launched the Anaconda “launcher.app” and launched IPython Notebook. However, iPython Notebook is running Python 2 not 3.

Over three hours of Googling later, I cannot figure out how to set IPython Notebook to run Python 3 instead of 2.


回答 0

要将IPython Notebook设置为在MAC 10.9上运行Python 3而不是2,我执行了以下步骤

$ sudo pip3 install ipython[all]

然后

$ ipython3 notebook

To set IPython Notebook to run Python 3 instead of 2 on my MAC 10.9, I did the following steps

$ sudo pip3 install ipython[all]

Then

$ ipython3 notebook


回答 1

对于Linux 16.04 Ubuntu,您可以使用

sudo apt-get install ipython3

然后使用

ipython3 notebook

在浏览器中打开笔记本。如果您有任何笔记本用python 2保存,则打开笔记本后它将自动将其转换为Python 3。

For linux 16.04 Ubuntu you can use

sudo apt-get install ipython3

and then use

ipython3 notebook

to open the notebook in the browser. If you have any notebooks saved with python 2 then it will automatically convert them to Python 3 once you open the notebook.


回答 2

要在带有Anaconda的Windows 10上将jupyter与python 3而不是python 2一起使用,我在anaconda提示符下执行了以下步骤:

pip3 install ipython[all]

然后,

ipython3 notebook

To use jupyter with python 3 instead of python 2 on my Windows 10 with Anaconda, I did the following steps on anaconda prompt:

pip3 install ipython[all]

Then,

ipython3 notebook

回答 3

您的发行版中有包装吗?如果您使用的是ubuntu,则必须安装ipython3-notebook软件包。如果没有,也许您必须使用python3安装ipython。

如果您已经运行(因为默认情况下是python2)

python setup.py

您必须改为跑步

python3 setup.py install

使用python3而不是python2安装软件包。这将是ipython3的新安装。

Is there a package from your distro? If you’re using ubuntu you must to install the ipython3-notebook package. If not, maybe you must to install ipython with python3.

If you’ve run (because it’s python2 by default)

python setup.py

you must to run instead

python3 setup.py install

to install a package with python3 instead python2. This will be a new instalation of ipython3.


回答 4

在Anaconda的“ launcher.app”中,有“环境:”下拉菜单。默认环境称为“根”。为了使用其他环境启动应用程序,只需从列表中选择所需的环境以使其处于活动状态即可。

In Anaconda “launcher.app” there is “Environment:” pull down menu. The default environment is called “root”. In order to launch application using another environment, just select the desired environment from the list, to make it active.


回答 5

如果您正在运行anaconda,则安装笔记本/ jupyter的首选方法是使用conda:

conda install jupyter

If you are running anaconda, then the preferred way to install notebook/jupyter is using conda:

conda install jupyter

回答 6

如果在jupyter笔记本电脑上都有这两个版本,则可以从菜单中更改内核。

If you have both version available on jupyter notebook, you can change the kernel from menu.


回答 7

适当切换此答案中2和3的角色。

假设您已经安装了带有python 2内核的jupyter设置和带有python 3的anaconda环境。激活python 3环境,然后运行

conda install ipykernel

之后,您可以在创建新笔记本或从内核菜单中选择正在运行的笔记本中同时选择2和3内核。

Switch the role of 2 and 3 in this answer as appropriate.

Say you already have jupyter setup with a python 2 kernel and an anaconda environment with python 3. Activate the python 3 enviroment and then run

conda install ipykernel

After that you can select both a 2 and 3 kernel when creating a new notebook, or in a running notebook from the kernels menu.


回答 8

另一个解决方案是使用python3 创建virtualenv

在此环境中,在此处安装tensorflow(您喜欢的版本):

pip install tensorflow

从那里运行您的jupyter!

Another solution would be to create a virtualenv with python3:

From this environment, install tensorflow (the version you prefer) there:

pip install tensorflow

Run your jupyter from there !


如何腌制或存储Jupyter(IPython)笔记本会话以供以后使用

问题:如何腌制或存储Jupyter(IPython)笔记本会话以供以后使用

假设我正在Jupyter / Ipython笔记本中进行较大的数据分析,并且完成了大量耗时的计算。然后,由于某种原因,我必须关闭jupyter本地服务器I,但是我想稍后再进行分析,而不必再次进行所有耗时的计算。


我想什么爱做的是pickle或存储整个Jupyter会话(所有大熊猫dataframes,np.arrays,变量,…),所以我可以放心地关闭服务器知道我可以在完全相同的状态返回到我的会话之前。

从技术上讲甚至可行吗?有没有我忽略的内置功能?


编辑:根据这个答案,有一种%store 魔法应该是“轻型泡菜”。但是,您必须像这样手动存储变量:

#inside a ipython/nb session
foo = "A dummy string"
%store foo
关闭种子,重新启动内核#r
%store -r foo进行刷新
print(foo) # "A dummy string"

这与我想要的功能相当接近,但是由于必须手动执行并且无法区分不同的会话,因此它的用处不大。

Let’s say I am doing a larger data analysis in Jupyter/Ipython notebook with lots of time consuming computations done. Then, for some reason, I have to shut down the jupyter local server I, but I would like to return to doing the analysis later, without having to go through all the time-consuming computations again.


What I would like love to do is pickle or store the whole Jupyter session (all pandas dataframes, np.arrays, variables, …) so I can safely shut down the server knowing I can return to my session in exactly the same state as before.

Is it even technically possible? Is there a built-in functionality I overlooked?


EDIT: based on this answer there is a %store magic which should be “lightweight pickle”. However you have to store the variables manually like so:

#inside a ipython/nb session
foo = "A dummy string"
%store foo
closing seesion, restarting kernel
%store -r foo # r for refresh
print(foo) # "A dummy string"

which is fairly close to what I would want, but having to do it manually and being unable to distinguish between different sessions makes it less useful.


回答 0

我认为迪尔很好地回答了您的问题。

pip install dill

保存笔记本会话:

import dill
dill.dump_session('notebook_env.db')

恢复笔记本会话:

import dill
dill.load_session('notebook_env.db')

资源

I think Dill answers your question well.

pip install dill

Save a Notebook session:

import dill
dill.dump_session('notebook_env.db')

Restore a Notebook session:

import dill
dill.load_session('notebook_env.db')

Source


回答 1

(我宁愿评论而不愿将其作为实际答案,但我需要更多的声誉才能发表评论。)

您可以系统地存储大多数类似数据的变量。我通常要做的是将所有数据帧,数组等存储在pandas.HDFStore中。在笔记本的开头,声明

backup = pd.HDFStore('backup.h5')

然后在产生它们时存储任何新变量

backup['var1'] = var1

最后,可能是一个好主意

backup.close()

在关闭服务器之前。下次您想继续使用笔记本时:

backup = pd.HDFStore('backup.h5')
var1 = backup['var1']

说实话,我也更喜欢ipython Notebook中的内置功能。您不能以这种方式保存所有内容(例如,对象,连接),并且很难用大量样板代码来组织笔记本。

(I’d rather comment than offer this as an actual answer, but I need more reputation to comment.)

You can store most data-like variables in a systematic way. What I usually do is store all dataframes, arrays, etc. in pandas.HDFStore. At the beginning of the notebook, declare

backup = pd.HDFStore('backup.h5')

and then store any new variables as you produce them

backup['var1'] = var1

At the end, probably a good idea to do

backup.close()

before turning off the server. The next time you want to continue with the notebook:

backup = pd.HDFStore('backup.h5')
var1 = backup['var1']

Truth be told, I’d prefer built-in functionality in ipython notebook, too. You can’t save everything this way (e.g. objects, connections), and it’s hard to keep the notebook organized with so much boilerplate codes.


回答 2

这个问题涉及到:如何在IPython Notebook中进行缓存?

为了保存单个单元的结果,缓存魔术派上了用场。

%%cache longcalc.pkl var1 var2 var3
var1 = longcalculation()
....

重新运行笔记本时,将从高速缓存中加载此单元格的内容。

这不能完全回答您的问题,但是对于所有冗长的计算结果都可以快速恢复的情况,这可能就足够了。这对我来说是一个可行的解决方案,同时点击了笔记本顶部的“ run-all”按钮。

缓存魔法救不了整个笔记本的状态还没有。据我所知,还没有其他系统可以恢复“笔记本”。这将需要保存python内核的所有历史记录。加载笔记本电脑并连接到内核后,应加载此信息。

This question is related to: How to cache in IPython Notebook?

To save the results of individual cells, the caching magic comes in handy.

%%cache longcalc.pkl var1 var2 var3
var1 = longcalculation()
....

When rerunning the notebook, the contents of this cell is loaded from the cache.

This is not exactly answering your question, but it might be enough to when the results of all the lengthy calculations are recovered fast. This in combination of hitting the run-all button on top of the notebook is for me a workable solution.

The cache magic cannot save the state of a whole notebook yet. To my knowledge there is no other system yet to resume a “notebook”. This would require to save all the history of the python kernel. After loading the notebook, and connecting to a kernel, this information should be loaded.


在jupyter笔记本中折叠单元格

问题:在jupyter笔记本中折叠单元格

我正在使用ipython Jupyter笔记本。假设我定义了一个在屏幕上占用很多空间的函数。有没有办法让牢房塌陷?

我希望函数保持执行和可调用状态,但是我想隐藏/折叠单元格以便更好地可视化笔记本。我怎样才能做到这一点?

I am using ipython Jupyter notebook. Let’s say I defined a function that occupies a lot of space on my screen. Is there a way to collapse the cell?

I want the function to remain executed and callable, yet I want to hide / collapse the cell in order to better visualize the notebook. How can I do this?


回答 0

jupyter contrib nbextensionsPython包包含一个代码折叠扩展,可以在笔记本内启用。请点击链接(Github)获得文档。

要使用命令行安装:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

为了使生活变得更轻松,我还建议您jupyter nbextensions configurator打包。这在笔记本电脑界面中提供了一个额外的选项卡,您可以在其中轻松地(停用)所有已安装的扩展程序。

安装:

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

The jupyter contrib nbextensions Python package contains a code-folding extension that can be enabled within the notebook. Follow the link (Github) for documentation.

To install using command line:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

To make life easier in managing them, I’d also recommend the jupyter nbextensions configurator package. This provides an extra tab in your Notebook interface from where you can easily (de)activate all installed extensions.

Installation:

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

回答 1

您可以创建一个单元格并将以下代码放入其中:

%%html
<style>
div.input {
    display:none;
}
</style>

运行此单元格将隐藏所有输入单元格。要显示它们,可以使用菜单清除所有输出。

否则,您可以尝试以下笔记本扩展:

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/Home_3x

You can create a cell and put the following code in it:

%%html
<style>
div.input {
    display:none;
}
</style>

Running this cell will hide all input cells. To show them back, you can use the menu to clear all outputs.

Otherwise you can try notebook extensions like below:

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/Home_3x


回答 2

JupyterLab支持细胞折叠。单击左侧的蓝色单元格栏将折叠该单元格。

JupyterLab supports cell collapsing. Clicking on the blue cell bar on the left will fold the cell.


回答 3

我遇到了类似的问题,@ Energya指出的“ nbextensions”工作得非常好,很轻松。对于笔记本扩展及其配置程序,安装说明很简单(我在Windows上使用anaconda进行了尝试)。

就是说,我想补充一点,以下扩展应该引起关注。

  • 隐藏输入| 此扩展允许在笔记本中隐藏单个码元。这可以通过单击工具栏按钮来实现:

  • 可折叠的标题| 允许笔记本具有可折叠的部分,并以标题分隔

  • 代码折叠| 已经提到过,但是为了完整性我添加了它

I had a similar issue and the “nbextensions” pointed out by @Energya worked very well and effortlessly. The install instructions are straight forward (I tried with anaconda on Windows) for the notebook extensions and for their configurator.

That said, I would like to add that the following extensions should be of interest.

  • Hide Input | This extension allows hiding of an individual codecell in a notebook. This can be achieved by clicking on the toolbar button:

  • Collapsible Headings | Allows notebook to have collapsible sections, separated by headings

  • Codefolding | This has been mentioned but I add it for completeness


回答 4

在〜/ .jupyter / custom /内创建具有以下内容的custom.js文件:

$("<style type='text/css'> .cell.code_cell.collapse { max-height:30px; overflow:hidden;} </style>").appendTo("head");
$('.prompt.input_prompt').on('click', function(event) {
    console.log("CLICKED", arguments)   
    var c = $(event.target.closest('.cell.code_cell'))
    if(c.hasClass('collapse')) {
        c.removeClass('collapse');
    } else {
        c.addClass('collapse');
    }
});

保存后,重新启动服务器并刷新笔记本。您可以通过单击输入标签(In [])折叠任何单元格。

Create custom.js file inside ~/.jupyter/custom/ with following contents:

$("<style type='text/css'> .cell.code_cell.collapse { max-height:30px; overflow:hidden;} </style>").appendTo("head");
$('.prompt.input_prompt').on('click', function(event) {
    console.log("CLICKED", arguments)   
    var c = $(event.target.closest('.cell.code_cell'))
    if(c.hasClass('collapse')) {
        c.removeClass('collapse');
    } else {
        c.addClass('collapse');
    }
});

After saving, restart the server and refresh the notebook. You can collapse any cell by clicking on the input label (In[]).


回答 5

hide_code扩展名允许您隐藏单个单元格和/或它们旁边的提示。安装为

pip3 install hide_code

访问https://github.com/kirbs-/hide_code/了解有关此扩展程序的更多信息。

The hide_code extension allows you to hide individual cells, and/or the prompts next to them. Install as

pip3 install hide_code

Visit https://github.com/kirbs-/hide_code/ for more info about this extension.


回答 6

首先,按照Energya的指示进行:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

第二个关键是:打开木星笔记本后,单击Nbextension选项卡。现在,从Nbextension提供的搜索工具(不是Web浏览器)中搜索“ colla”,然后您将找到一个名为“ Collapsible Headings”的内容

这就是你想要的!

Firstly, follow Energya’s instruction:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

Second is the key: After opening jupiter notebook, click the Nbextension tab. Now Search “colla” from the searching tool provided by Nbextension(not by the web browser), then you will find something called “Collapsible Headings”

This is what you want!


回答 7

正如其他人所提到的,您可以通过nbextensions进行此操作。我想简短地解释一下我所做的事情:

要启用可协作的标题:在终端中,首先输入以下内容来启用/安装Jupyter Notebook Extensions:

pip install jupyter_contrib_nbextensions

然后输入:

jupyter contrib nbextension install

重新打开Jupyter Notebook。转到“编辑”选项卡,然后选择“ nbextensions配置”。取消直接在标题“ Configurable nbextensions”下的复选框,然后选择“可折叠标题”。

As others have mentioned, you can do this via nbextensions. I wanted to give the brief explanation of what I did, which was quick and easy:

To enable collabsible headings: In your terminal, enable/install Jupyter Notebook Extensions by first entering:

pip install jupyter_contrib_nbextensions

Then, enter:

jupyter contrib nbextension install

Re-open Jupyter Notebook. Go to “Edit” tab, and select “nbextensions config”. Un-check box directly under title “Configurable nbextensions”, then select “collapsible headings”.


回答 8

这个问题有很多答案,在许多扩展中,我觉得所有这些都不令人满意(有些比其他更好),例如代码折叠,标题折叠等。没有一个人以简单有效的方式满足我的要求。令我惊讶的是,尚未实施解决方案(对于Jupyter Lab而言)。

实际上,我非常不满意,以至于我开发了一个非常简单的笔记本扩展,可以扩展/折叠笔记本单元中的代码,同时保持其可执行性。

GitHub存储库:https : //github.com/BenedictWilkinsAI/cellfolding

以下是该扩展程序的小演示:

只需双击代码单元的左侧,即可将其折叠为一行:

再次双击将展开该单元格。

该扩展可以通过pip轻松安装:

pip install nbextension-cellfolding
jupyter nbextension install --py cellfolding --user
jupyter nbextension enable --py cellfolding --user 

并且还与nbextension configurator兼容。我希望人们会发现这很有用!

There are many answers to this question, all of which I feel are not satisfactory (some more than others), of the many extensions – code folding, folding by headings etc etc. None do what I want in simple and effective way. I am literally amazed that a solution has not been implemented (as it has for Jupyter Lab).

In fact, I was so dissatisfied that I have developed a very simple notebook extension that can expand/collapse the code in a notebook cell, while keeping it executable.

The GitHub repository: https://github.com/BenedictWilkinsAI/cellfolding

Below is a small demo of what the extension does:

Simply double clicking left of the code cell will collapse it to a single line:

Double clicking again will expand the cell.

The extension can be installed easily with pip:

pip install nbextension-cellfolding
jupyter nbextension install --py cellfolding --user
jupyter nbextension enable --py cellfolding --user 

and is also compatible with nbextension configurator. I hope that people will find this useful!


回答 9

潘岩建议的改进版本。它添加了显示代码单元的按钮:

%%html
<style id=hide>div.input{display:none;}</style>
<button type="button" 
onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">
Show inputs</button>

或python:

# Run me to hide code cells

from IPython.core.display import display, HTML
display(HTML(r"""<style id=hide>div.input{display:none;}</style><button type="button"onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">Show inputs</button>"""))

There’s also an improved version of Pan Yan suggestion. It adds the button that shows code cells back:

%%html
<style id=hide>div.input{display:none;}</style>
<button type="button" 
onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">
Show inputs</button>

Or python:

# Run me to hide code cells

from IPython.core.display import display, HTML
display(HTML(r"""<style id=hide>div.input{display:none;}</style><button type="button"onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">Show inputs</button>"""))

回答 10

除了启用扩展,您不需要做太多事情:

http://localhost:8888/nbextensions?nbextension=collapsible_headings
http://localhost:8888/nbextensions?nbextension=codefolding/main

最有可能在以下位置找到所有扩展:

http://localhost:8888/nbextensions

You don’t need to do much except to enable the extensions:

http://localhost:8888/nbextensions?nbextension=collapsible_headings
http://localhost:8888/nbextensions?nbextension=codefolding/main

Most probable you will find all your extensions in here:

http://localhost:8888/nbextensions


回答 11

我用来获得预期结果的是:

  1. 将以下代码块保存toggle_cell.py在与笔记本相同目录中的文件中
from IPython.core.display import display, HTML
toggle_code_str = '''
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Sloution"></form>
'''

toggle_code_prepare_str = '''
    <script>
    function code_toggle() {
        if ($('div.cell.code_cell.rendered.selected div.input').css('display')!='none'){
            $('div.cell.code_cell.rendered.selected div.input').hide();
        } else {
            $('div.cell.code_cell.rendered.selected div.input').show();
        }
    }
    </script>

'''

display(HTML(toggle_code_prepare_str + toggle_code_str))

def hide_sloution():
    display(HTML(toggle_code_str))
  1. 在笔记本的第一个单元格中添加以下内容
from toggle_cell import toggle_code as hide_sloution
  1. 您需要添加切换按钮即可调用的任何单元格 hide_sloution()

What I use to get the desired outcome is:

  1. Save the below code block in a file named toggle_cell.py in the same directory as of your notebook
from IPython.core.display import display, HTML
toggle_code_str = '''
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Sloution"></form>
'''

toggle_code_prepare_str = '''
    <script>
    function code_toggle() {
        if ($('div.cell.code_cell.rendered.selected div.input').css('display')!='none'){
            $('div.cell.code_cell.rendered.selected div.input').hide();
        } else {
            $('div.cell.code_cell.rendered.selected div.input').show();
        }
    }
    </script>

'''

display(HTML(toggle_code_prepare_str + toggle_code_str))

def hide_sloution():
    display(HTML(toggle_code_str))
  1. Add the following in the first cell of your notebook
from toggle_cell import toggle_code as hide_sloution
  1. Any cell you need to add the toggle button to simply call hide_sloution()

在ipython Notebook中测量单元执行时间的简单方法

问题:在ipython Notebook中测量单元执行时间的简单方法

除了单元的原始输出,我想花时间在单元执行上。

为此,我尝试了%%timeit -r1 -n1但它没有公开定义在单元格内的变量。

%%time 适用于仅包含1条语句的单元格。

In[1]: %%time
       1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1

In[2]: %%time
       # Notice there is no out result in this case.
       x = 1
       x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs

最好的方法是什么?

更新资料

我已经在Nbextension中使用Execute Time了一段时间了。这太棒了。

I would like to get the time spent on the cell execution in addition to the original output from cell.

To this end, I tried %%timeit -r1 -n1 but it doesn’t expose the variable defined within cell.

%%time works for cell which only contains 1 statement.

In[1]: %%time
       1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1

In[2]: %%time
       # Notice there is no out result in this case.
       x = 1
       x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs

What’s the best way to do it?

Update

I have been using Execute Time in Nbextension for quite some time now. It is great.


回答 0

使用单元魔术和Phillip Cloud在github上的此项目:

通过将其放在笔记本顶部或如果您始终希望默认情况下将其放在配置文件中来进行加载:

%install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py
%load_ext autotime

如果加载,则后续单元执行的每个输出将包括执行时间(以分钟和秒为单位)。

Use cell magic and this project on github by Phillip Cloud:

Load it by putting this at the top of your notebook or put it in your config file if you always want to load it by default:

%install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py
%load_ext autotime

If loaded, every output of subsequent cell execution will include the time in min and sec it took to execute it.


回答 1

我发现克服此问题的唯一方法是执行带有print的最后一条语句。

不要忘了单元魔术始于,%%行魔术始于%

%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)

请注意,在下一个单元格中将不考虑在单元格内执行的任何更改,这在存在管道时是很直观的:

The only way I found to overcome this problem is by executing the last statement with print.

Do not forget that cell magic starts with %% and line magic starts with %.

%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)

Notice that any changes performed inside the cell are not taken into consideration in the next cells, something that is counter intuitive when there is a pipeline:


回答 2

%time%timeit现在来IPython中的一部分内置的魔法命令

%time and %timeit now come part of ipython’s built-in magic commands


回答 3

一种更简单的方法是在jupyter_contrib_nbextensions软件包中使用ExecuteTime插件。

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable execute_time/ExecuteTime

An easier way is to use ExecuteTime plugin in jupyter_contrib_nbextensions package.

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable execute_time/ExecuteTime

回答 4

我只是%%time在单元格的开头添加了时间。您可以在Jupyter Spark群集/虚拟环境上使用相同的名称。只需%%time在单元格的顶部添加,您将获得输出。在使用Jupyter的Spark集群上,我将其添加到单元格的顶部,并得到如下输出:-

[1]  %%time
     import pandas as pd
     from pyspark.ml import Pipeline
     from pyspark.ml.classification import LogisticRegression
     import numpy as np
     .... code ....

Output :-

CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
Wall time: 1min 18s

I simply added %%time at the beginning of the cell and got the time. You may use the same on Jupyter Spark cluster/ Virtual environment using the same. Just add %%time at the top of the cell and you will get the output. On spark cluster using Jupyter, I added to the top of the cell and I got output like below:-

[1]  %%time
     import pandas as pd
     from pyspark.ml import Pipeline
     from pyspark.ml.classification import LogisticRegression
     import numpy as np
     .... code ....

Output :-

CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
Wall time: 1min 18s

回答 5

import time
start = time.time()
"the code you want to test stays here"
end = time.time()
print(end - start)
import time
start = time.time()
"the code you want to test stays here"
end = time.time()
print(end - start)

回答 6

您可以使用timeit魔术功能。

%timeit CODE_LINE

或在细胞上

%%timeit 

SOME_CELL_CODE

https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb检查更多IPython魔术函数

You can use timeit magic function for that.

%timeit CODE_LINE

Or on the cell

%%timeit 

SOME_CELL_CODE

Check more IPython magic functions at https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb


回答 7

这不是很漂亮,但没有额外的软件

class timeit():
    from datetime import datetime
    def __enter__(self):
        self.tic = self.datetime.now()
    def __exit__(self, *args, **kwargs):
        print('runtime: {}'.format(self.datetime.now() - self.tic))

然后,您可以像这样运行它:

with timeit():
    # your code, e.g., 
    print(sum(range(int(1e7))))

% 49999995000000
% runtime: 0:00:00.338492

This is not exactly beautiful but without extra software

class timeit():
    from datetime import datetime
    def __enter__(self):
        self.tic = self.datetime.now()
    def __exit__(self, *args, **kwargs):
        print('runtime: {}'.format(self.datetime.now() - self.tic))

Then you can run it like:

with timeit():
    # your code, e.g., 
    print(sum(range(int(1e7))))

% 49999995000000
% runtime: 0:00:00.338492

回答 8

有时,使用时单元格中的格式会有所不同print(res),但是jupyter / ipython带有display。请参阅下面有关使用熊猫的格式差异的示例。

%%time
import pandas as pd 
from IPython.display import display

df = pd.DataFrame({"col0":{"a":0,"b":0}
              ,"col1":{"a":1,"b":1}
              ,"col2":{"a":2,"b":2}
             })

#compare the following
print(df)
display(df)

display语句可以保留格式。

Sometimes the formatting is different in a cell when using print(res), but jupyter/ipython comes with a display. See an example of the formatting difference using pandas below.

%%time
import pandas as pd 
from IPython.display import display

df = pd.DataFrame({"col0":{"a":0,"b":0}
              ,"col1":{"a":1,"b":1}
              ,"col2":{"a":2,"b":2}
             })

#compare the following
print(df)
display(df)

The display statement can preserve the formatting.


回答 9

您可能还需要查看python的分析魔术命令%prun,该命令给出类似以下内容的信息-

def sum_of_lists(N):
    total = 0
    for i in range(5):
        L = [j ^ (j >> i) for j in range(N)]
        total += sum(L)
    return total

然后

%prun sum_of_lists(1000000)

将返回

14 function calls in 0.714 seconds  

Ordered by: internal time      

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    5    0.599    0.120    0.599    0.120 <ipython-input-19>:4(<listcomp>)
    5    0.064    0.013    0.064    0.013 {built-in method sum}
    1    0.036    0.036    0.699    0.699 <ipython-input-19>:1(sum_of_lists)
    1    0.014    0.014    0.714    0.714 <string>:1(<module>)
    1    0.000    0.000    0.714    0.714 {built-in method exec}

当处理大量代码时,我发现它很有用。

you may also want to look in to python’s profiling magic command %prunwhich gives something like –

def sum_of_lists(N):
    total = 0
    for i in range(5):
        L = [j ^ (j >> i) for j in range(N)]
        total += sum(L)
    return total

then

%prun sum_of_lists(1000000)

will return

14 function calls in 0.714 seconds  

Ordered by: internal time      

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    5    0.599    0.120    0.599    0.120 <ipython-input-19>:4(<listcomp>)
    5    0.064    0.013    0.064    0.013 {built-in method sum}
    1    0.036    0.036    0.699    0.699 <ipython-input-19>:1(sum_of_lists)
    1    0.014    0.014    0.714    0.714 <string>:1(<module>)
    1    0.000    0.000    0.714    0.714 {built-in method exec}

I find it useful when working with large chunks of code.


回答 10

遇到麻烦时,意味着什么:

?%timeit 要么 ??timeit

要获取详细信息:

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.

When in trouble what means what:

?%timeit or ??timeit

To get the details:

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.

回答 11

如果要打印壁单元执行时间,这是一个技巧,请使用

%%time
<--code goes here-->

但是请确保%% time是一个魔术函数,因此请将其放在代码的第一行

如果您将其放在代码的某些行之后,则会出现用法错误,并且无法正常工作。

If you want to print wall cell execution time here is a trick, use

%%time
<--code goes here-->

but here make sure that, the %%time is a magic function, so put it at first line in your code.

if you put it after some line of your code it’s going to give you usage error and not gonna work.


在IPython Notebook中同时使用Python 2.x和Python 3.x

问题:在IPython Notebook中同时使用Python 2.x和Python 3.x

我使用IPython笔记本,并且希望能够选择在IPython中创建2.x或3.x python笔记本。

我最初有Anaconda。使用Anaconda时,必须更改全局环境变量以选择所需的python版本,然后才能启动IPython。这不是我想要的,所以我卸载了Anaconda,现在使用MacPorts和PiP设置了自己的安装。看来我还是要用

port select --set python <python version> 

在python 2.x和3.x之间切换。这并不比anaconda解决方案好。

启动IPython笔记本后,是否有一种方法可以选择要使用的python版本,最好使用当前的MacPorts构建?

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.

I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now have set up my own installation using MacPorts and PiP. It seems that I still have to use

port select --set python <python version> 

to toggle between python 2.x and 3.x. which is no better than the anaconda solution.

Is there a way to select what version of python you want to use after you start an IPython notebook, preferably with my current MacPorts build?


回答 0

这里的想法是安装多个ipython内核。这是有关Python的说明。如果你不使用Python,我最近添加的说明采用纯virtualenvs。

水蟒> = 4.1.0

从版本4.1.0开始,anaconda包含一个特殊的程序包nb_conda_kernels,该程序包可检测笔记本内核的conda环境并自动注册它们。这使得使用新的python版本就像创建新的conda环境一样容易:

conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel

重新启动jupyter notebook之后,新内核可通过图形界面使用。请注意,必须将新软件包明确安装到新环境中。conda文档中的“ 管理环境”部分提供了更多信息。

手动注册内核

不想使用nb_conda_kernels或仍使用旧版本的anaconda的用户可以使用以下步骤来手动注册ipython内核。

配置python2.7环境:

conda create -n py27 python=2.7
conda activate py27
conda install notebook ipykernel
ipython kernel install --user

配置python3.6环境:

conda create -n py36 python=3.6
conda activate py36
conda install notebook ipykernel
ipython kernel install --user

在此之后,你应该能够之间进行选择python2
python3创造的接口一个新的笔记本时。

另外,如果要更改内核名称,可以将--name--display-name选项传递给ipython kernel install。请参阅ipython kernel install --help以获取更多信息。

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

Anaconda >= 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new environments. The Managing environments section in conda’s docs provides further information.

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

configure the python2.7 environment:

conda create -n py27 python=2.7
conda activate py27
conda install notebook ipykernel
ipython kernel install --user

configure the python3.6 environment:

conda create -n py36 python=3.6
conda activate py36
conda install notebook ipykernel
ipython kernel install --user

After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.


回答 1

如果您在Python 3上运行Jupyter,则可以这样设置Python 2内核:

python2 -m pip install ipykernel

python2 -m ipykernel install --user

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:

python2 -m pip install ipykernel

python2 -m ipykernel install --user

http://ipython.readthedocs.io/en/stable/install/kernel_install.html


回答 2

这些说明说明了如何为非anaconda用户在单独的虚拟环境中安装python2和python3内核。如果您使用anaconda,请找到我的其他答案,以直接针对anaconda量身定制解决方案。

我假设您已经jupyter notebook安装了。


首先,请确保您有python2和提供的python3口译员pip

在ubuntu上,您可以通过以下方式安装它们:

sudo apt-get install python-dev python3-dev python-pip python3-pip

接下来准备并注册内核环境

python -m pip install virtualenv --user

# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate

# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate

为了简化操作,您可能需要将激活命令的外壳别名添加到外壳配置文件中。根据不同的系统和外壳使用,这可以是例如~/.bashrc~/.bash_profile~/.zshrc

alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'

重新启动外壳程序后,现在可以在激活要使用的环境后安装新软件包。

kernel2
python -m pip install <pkg-name>
deactivate

要么

kernel3
python -m pip install <pkg-name>
deactivate

These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.

I assume that you already have jupyter notebook installed.


First make sure that you have a python2 and a python3 interpreter with pip available.

On ubuntu you would install these by:

sudo apt-get install python-dev python3-dev python-pip python3-pip

Next prepare and register the kernel environments

python -m pip install virtualenv --user

# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate

# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate

To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc

alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'

After restarting your shell, you can now install new packages after activating the environment you want to use.

kernel2
python -m pip install <pkg-name>
deactivate

or

kernel3
python -m pip install <pkg-name>
deactivate

回答 3

使用当前版本的Notebook / Jupyter,您可以创建Python3内核。使用Python 2从命令行启动新的笔记本应用程序后,您应该在下拉菜单“新建”中看到条目“ Python 3”。这为您提供了一个使用Python 3的笔记本。因此,您可以并排放置两个笔记本,并使用不同的Python版本。

细节

  1. 创建此目录: mkdir -p ~/.ipython/kernels/python3
  2. ~/.ipython/kernels/python3/kernel.json使用以下内容创建此文件:

    {
        "display_name": "IPython (Python 3)", 
        "language": "python", 
        "argv": [
            "python3", 
            "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
            "-f", "{connection_file}"
        ], 
        "codemirror_mode": {
            "version": 2, 
            "name": "ipython"
        }
    }
  3. 重新启动笔记本服务器。

  4. 从下拉菜单“新建”中选择“ Python 3”
  5. 使用Python 3笔记本
  6. 从下拉菜单“新建”中选择“ Python 2”
  7. 使用Python 2笔记本

With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.

The Details

  1. Create this directory: mkdir -p ~/.ipython/kernels/python3
  2. Create this file ~/.ipython/kernels/python3/kernel.json with this content:

    {
        "display_name": "IPython (Python 3)", 
        "language": "python", 
        "argv": [
            "python3", 
            "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
            "-f", "{connection_file}"
        ], 
        "codemirror_mode": {
            "version": 2, 
            "name": "ipython"
        }
    }
    
  3. Restart the notebook server.

  4. Select „Python 3“ from the dropdown menu „New“
  5. Work with a Python 3 Notebook
  6. Select „Python 2“ from the dropdown menu „New“
  7. Work with a Python 2 Notebook

回答 4

提供了一个解决方案,该解决方案允许我通过配置Ipython kernelspec来保留MacPorts的安装。

要求:

  • MacPorts安装在通常的/ opt目录中
  • python 2.7是通过macports安装的
  • python 3.4通过macports安装
  • 为python 2.7安装了ipython
  • 为python 3.4安装了ipython

对于python 2.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self

对于python 3.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self

现在,您可以打开一个Ipython笔记本,然后选择python 2.x或python 3.x笔记本。

A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.

Requirements:

  • MacPorts is installed in the usual /opt directory
  • python 2.7 is installed through macports
  • python 3.4 is installed through macports
  • Ipython is installed for python 2.7
  • Ipython is installed for python 3.4

For python 2.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self

For python 3.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self

Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.


回答 5

通过我的Linux安装,我做到了:

sudo ipython2 kernelspec install-self

现在,我的python 2又回到了列表中。

参考:

http://ipython.readthedocs.org/en/latest/install/kernel_install.html


更新:

上面的方法现已弃用,将来会被删除。新方法应为:

sudo ipython2 kernel install

From my Linux installation I did:

sudo ipython2 kernelspec install-self

And now my python 2 is back on the list.

Reference:

http://ipython.readthedocs.org/en/latest/install/kernel_install.html


UPDATE:

The method above is now deprecated and will be dropped in the future. The new method should be:

sudo ipython2 kernel install


回答 6

以下是将python2内核添加到jupyter笔记本的步骤:

打开一个终端并创建一个新的python 2环境: conda create -n py27 python=2.7

激活环境:Linux source activate py27或Windowsactivate py27

在环境中安装内核: conda install notebook ipykernel

在env外部安装内核: ipython kernel install --user

关闭环境: source deactivate

尽管答案很晚,希望有人发现它有用:p

Following are the steps to add the python2 kernel to jupyter notebook::

open a terminal and create a new python 2 environment: conda create -n py27 python=2.7

activate the environment: Linux source activate py27 or windows activate py27

install the kernel in the env: conda install notebook ipykernel

install the kernel for outside the env: ipython kernel install --user

close the env: source deactivate

Although a late answer hope someone finds it useful :p


回答 7

使用sudo pip3 install jupyter安装了python3 jupyter和sudo pip install jupyter安装jupyter笔记本python2。然后,您可以调用ipython kernel install命令来启用两种类型的笔记本以在jupyter笔记本中进行选择。

Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.


回答 8

我查看了这个出色的信息,然后想知道

  1. 我已经安装了python2,python3和IPython,
  2. 我安装了PyCharm,
  3. PyCharm将IPython用于其Python控制台,

如果 PyCharm将使用

  1. IPython的-PY2时菜单>文件>设置>项目>项目解释== PY2
  2. 当菜单>文件>设置>项目>项目解释器== py3时,IPython-py3

答案:是的!

PS我也安装了适用于Windows的Python启动器。

I looked at this excellent info and then wondered, since

  1. i have python2, python3 and IPython all installed,
  2. i have PyCharm installed,
  3. PyCharm uses IPython for its Python Console,

if PyCharm would use

  1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3

ANSWER: Yes!

P.S. i have Python Launcher for Windows installed as well.


回答 9

在Windows 7下,我安装了anaconda和anaconda3。我走进去\Users\me\anaconda\Scripts执行

sudo .\ipython kernelspec install-self

然后我走进去\Users\me\anaconda3\Scripts执行

sudo .\ipython kernel install

(我知道了 jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.

启动后jupyter notebook(在anaconda3中),我在右上角的“新建”下获得了一个整洁的下拉菜单,让我在Python 2 odr和Python 3内核之间进行选择。

Under Windows 7 I had anaconda and anaconda3 installed. I went into \Users\me\anaconda\Scripts and executed

sudo .\ipython kernelspec install-self

then I went into \Users\me\anaconda3\Scripts and executed

sudo .\ipython kernel install

(I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)

After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under “New” letting me choose between Python 2 odr Python 3 kernels.


回答 10

  • 如果您在虚拟环境中运行anaconda。
  • 当您创建一个新笔记本时,我没有显示选择虚拟环境内核。
  • 然后,您必须使用以下命令将其设置到ipykernel中
$ pip install --user ipykernel
$ python -m ipykernel install --user --name=test2
  • If you are running anaconda in virtual environment.
  • And when you create a new notebook but i’s not showing to select the virtual environment kernel.
  • Then you have to set it into the ipykernel using the following command
$ pip install --user ipykernel
$ python -m ipykernel install --user --name=test2