Pythonによるデータ分析入門を読んで1

今日のヌートリアはすごく近くで食事に夢中になっていました。
概要
仕事も落ち着いてきたので、Python を久しぶりにいじっています。
うろ覚えになっているところがほとんどですので、「Puthonによるデータ分析入門 第2版」を読んでいます。
読み進める中で P95 の先頭に次のような記述があります。
%time for _ in range(10): my_arr2 = my_arr * 2
実行結果
CPU times: total: 15.6 ms
Wall time: 15.6 ms
この中の %time が分からず、いろいろ調べたので記録します。
IPython
最初 Python の言語の規則の一部だろうと思い、それをキーにして資料を検索しましたが思うようなものは検索できませんでした。
やっと %time は IPython のマジックコマンドの一つであることが分かりました。
https://ipython.readthedocs.io/en/stable/index.html
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-time
ちなみにコマンドプロンプトから IPython を起動して本のコードを実行すると次のようになります。

ですので、私は IPython は Pyhton を動かすシェルのような働きをするものだと理解しました。
なので、%time は IPython の中しか使えないのではと思っています。
マジックコマンドは、Line magics と Cell magics の2種類があります。
ファイルの保存
Line magics と Cell magics の一覧は次のWebに載っていました。
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-time
その一覧表から、保存の機能を持ったコマンドを探します。
「%notebook」「%save」がその機能を果たすようです。
拡張子「.ipynb」の保存は %notebook
IPython の コマンドプロンプトを使って実行してきたものを拡張子「.ipynb」で保存するには、WEBでは次のよう記載しています。
%notebook filename
その使い方は次のように記載があります。
Export and convert IPython notebooks.
This function can export the current IPython history to a notebook file. For example, to export the history to “foo.ipynb” do “%notebook foo.ipynb”.
positional arguments:
filename Notebook name or filename
実際に動かしてみます。
カレントディレクトリを Jupyter Notebook の作業ディレクトリに変更して、IPython を実行します。
私の場合は「C:\Users\ユーザー名\learning\python」ですので次のようになりました。

本のコードを実行します。
そして「%notebook mytest.jpynb」として実行します。拡張子は Jupyter Notebook で見たいので「jpynb」としました。

「mytest.jpynb」が作成された様子です。

拡張子「.py」の保存は %save
WEBの説明では、拡張子「.py」で保存するには次のように記載しています。
%save [options] filename [history]
その使い方は次のように記載があります
Save a set of lines or a macro to a given filename.
Usage:
%save [options] filename [history]
Options:
-r: use ‘raw’ input. By default, the ‘processed’ history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed at the command line is used instead.
-f: force overwrite. If file exists, %save will prompt for overwrite unless -f is given.
-a: append to the file instead of overwriting it.
The history argument uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.
If no ranges are specified, saves history of the current session up to this point.
It adds a ‘.py’ extension to the file if you don’t do so yourself, and it asks for confirmation before overwriting existing files.
If -r option is used, the default extension is .ipy.
ファイル名「mytest.py」で保存するには、次のようにします。
%save -r mytest.py
実践してみます。

mytest.py に次のコマンドが書き込まれましたというメッセージが出ました。

作業ディレクトリを開いてみると「mytest.py」が作成されています。

Jupyter NoteBook
しかし、%time は IPython の中しか使えないのではと書きましたが、Jupyter Notebook の中でも使えますので、IPython と同等の機能を備えているようです。

「%notebook mytest.jpynb」で作成した「 mytest.jpynb」を「Jupyter Notebook」で見てみます。

ブラウザが開き次のように表示されます。

「mytest.ipynb」を開いてみます。
次のように表示され、引き続き作業ができます。

実行した結果の経過までは保存しないようです。
IPython と Jupyter NoteBook の関係
これから推測するに、IPython の Notebook の機能と Jupyter Notebook の Notebook の機能は同じものと思われます。
ここまでとします。