Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

AIを学ぶ4 〜Jupyter Notebook を使ってみる〜

こんにちは、 鯨井貴博@opensourcetechです。

 

今回は、AI(深層学習や機械学習)に利用できる Python の統合開発環境である、

Jupyter Notebook 環境を用意します。

なお、操作は Mac OS上で行なっています。

 

 

pip3 を使用してインストール

pip3 からjupyter パッケージをインストールします。

なお、pip3が入っていない場合、以下を参照ください。

opensourcetech.hatenablog.jp

 

bash-3.2$ sudo pip3 install jupyter
Password:
The directory '/Users/********/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/********/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting jupyter
Downloading https://files.pythonhosted.org/packages/83/df/0f5dd132200728a86190397e1ea87cd76244e42d39ec5e88efd25b2abd7e/jupyter-1.0.0-py2.py3-none-any.whl
Collecting ipykernel (from jupyter)
Downloading https://files.pythonhosted.org/packages/ab/3f/cd624c835aa3336a9110d0a99e15070f343b881b7d651ab1375ef226a3ac/ipykernel-4.8.2-py3-none-any.whl (108kB)
100% |████████████████████████████████| 112kB 1.7MB/s
Collecting ipywidgets (from jupyter)
Downloading https://files.pythonhosted.org/packages/f0/5d/868df21e3b004a5a61294cab70e1f6f44986933eb3aa9c396dfd5112acb2/ipywidgets-7.3.0-py2.py3-none-any.whl (109kB)
100% |████████████████████████████████| 112kB 9.0MB/s
Collecting notebook (from jupyter)
Downloading https://files.pythonhosted.org/packages/5e/7c/7fd8e9584779d65dfcad9fa2e09c76131a41f999f853a9c7026ed8585586/notebook-5.6.0-py2.py3-none-any.whl (8.9MB)
100% |████████████████████████████████| 8.9MB 2.2MB/s
Collecting qtconsole (from jupyter)
Downloading https://files.pythonhosted.org/packages/90/ff/047e0dca2627b162866920e7aa93f04523c0ae81e5c67060eec85701992d/qtconsole-4.3.1-py2.py3-none-any.whl (108kB)
100% |████████████████████████████████| 112kB 10.0MB/s
Collecting nbconvert (from jupyter)
Downloading https://files.pythonhosted.org/packages/39/ea/280d6c0d92f8e3ca15fd798bbcc2ea141489f9539de7133d8fe10ea4b049/nbconvert-5.3.1-py2.py3-none-any.whl (387kB)
100% |████████████████████████████████| 389kB 6.9MB/s
Collecting jupyter-console (from jupyter)
Downloading https://files.pythonhosted.org/packages/77/82/6469cd7fccf7958cbe5dce2e623f1e3c5e27f1bb1ad36d90519bc2d5d370/jupyter_console-5.2.0-py2.py3-none-any.whl
Collecting jupyter-client (from ipykernel->jupyter)
Downloading https://files.pythonhosted.org/packages/94/dd/fe6c4d683b09eb05342bd2816b7779663f71762b4fa9c2d5203d35d17354/jupyter_client-5.2.3-py2.py3-none-any.whl (89kB)
100% |████████████████████████████████| 92kB 6.9MB/s
Requirement already satisfied: tornado>=4.0 in /usr/local/lib/python3.6/site-packages (from ipykernel->jupyter) (5.1)
Collecting traitlets>=4.1.0 (from ipykernel->jupyter)
Downloading https://files.pythonhosted.org/packages/93/d6/abcb22de61d78e2fc3959c964628a5771e47e7cc60d53e9342e21ed6cc9a/traitlets-4.3.2-py2.py3-none-any.whl (74kB)
100% |████████████████████████████████| 81kB 12.5MB/s
Collecting ipython>=4.0.0 (from ipykernel->jupyter)
Downloading https://files.pythonhosted.org/packages/b1/7f/91d50f28af3e3a24342561983a7857e399ce24093876e6970b986a0b6677/ipython-6.4.0-py3-none-any.whl (750kB)
100% |████████████████████████████████| 757kB 6.2MB/s
Collecting widgetsnbextension~=3.3.0 (from ipywidgets->jupyter)
Downloading https://files.pythonhosted.org/packages/b9/43/f6ff09448f7b961e102fd75b7e46a5d44b68b9746bb1ab5c4be64c3e236d/widgetsnbextension-3.3.0-py2.py3-none-any.whl (2.2MB)
100% |████████████████████████████████| 2.2MB 5.9MB/s
Collecting nbformat>=4.2.0 (from ipywidgets->jupyter)
Downloading https://files.pythonhosted.org/packages/da/27/9a654d2b6cc1eaa517d1c5a4405166c7f6d72f04f6e7eea41855fe808a46/nbformat-4.4.0-py2.py3-none-any.whl (155kB)
100% |████████████████████████████████| 163kB 8.8MB/s
Collecting terminado>=0.8.1 (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/2e/20/a26211a24425923d46e1213b376a6ee60dc30bcdf1b0c345e2c3769deb1c/terminado-0.8.1-py2.py3-none-any.whl
Collecting prometheus-client (from notebook->jupyter)
Collecting jinja2 (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
100% |████████████████████████████████| 133kB 11.4MB/s
Collecting Send2Trash (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/49/46/c3dc27481d1cc57b9385aff41c474ceb7714f7935b1247194adae45db714/Send2Trash-1.5.0-py3-none-any.whl
Collecting jupyter-core>=4.4.0 (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/1d/44/065d2d7bae7bebc06f1dd70d23c36da8c50c0f08b4236716743d706762a8/jupyter_core-4.4.0-py2.py3-none-any.whl (126kB)
100% |████████████████████████████████| 133kB 10.9MB/s
Collecting ipython-genutils (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/fa/bc/9bd3b5c2b4774d5f33b2d544f1460be9df7df2fe42f352135381c347c69a/ipython_genutils-0.2.0-py2.py3-none-any.whl
Collecting pyzmq>=17 (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/96/f9/b8641e4db55f5df80e0b0c15aaf2c177de2136c8c33b4ba518710ef0fe39/pyzmq-17.1.0-cp36-cp36m-macosx_10_6_intel.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 7.3MB/s
Requirement already satisfied: pygments in /usr/local/lib/python3.6/site-packages (from qtconsole->jupyter) (2.2.0)
Collecting pandocfilters>=1.4.1 (from nbconvert->jupyter)
Collecting entrypoints>=0.2.2 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/cc/8b/4eefa9b47f1910b3d2081da67726b066e379b04ca897acfe9f92bac56147/entrypoints-0.2.3-py2.py3-none-any.whl
Collecting mistune>=0.7.4 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/c8/8c/87f4d359438ba0321a2ae91936030110bfcc62fef752656321a72b8c1af9/mistune-0.8.3-py2.py3-none-any.whl
Collecting testpath (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/15/19/d7bc380c479a184e4a5a9ce516e4e2a773165f89b445f7cdced83d94de25/testpath-0.3.1-py2.py3-none-any.whl (161kB)
100% |████████████████████████████████| 163kB 8.3MB/s
Collecting bleach (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/30/b6/a8cffbb9ab4b62b557c22703163735210e9cd857d533740c64e1467d228e/bleach-2.1.3-py2.py3-none-any.whl
Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.0 in /usr/local/lib/python3.6/site-packages (from jupyter-console->jupyter) (1.0.15)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/site-packages (from jupyter-client->ipykernel->jupyter) (2.7.3)
Requirement already satisfied: six in /usr/local/lib/python3.6/site-packages (from traitlets>=4.1.0->ipykernel->jupyter) (1.11.0)
Collecting decorator (from traitlets>=4.1.0->ipykernel->jupyter)
Downloading https://files.pythonhosted.org/packages/bc/bb/a24838832ba35baf52f32ab1a49b906b5f82fb7c76b2f6a7e35e140bac30/decorator-4.3.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->jupyter) (39.0.1)
Requirement already satisfied: backcall in /usr/local/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->jupyter) (0.1.0)
Collecting simplegeneric>0.8 (from ipython>=4.0.0->ipykernel->jupyter)
Collecting pexpect; sys_platform != "win32" (from ipython>=4.0.0->ipykernel->jupyter)
Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 9.9MB/s
Requirement already satisfied: appnope; sys_platform == "darwin" in /usr/local/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->jupyter) (0.1.0)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->jupyter) (0.7.4)
Requirement already satisfied: jedi>=0.10 in /usr/local/lib/python3.6/site-packages (from ipython>=4.0.0->ipykernel->jupyter) (0.12.1)
Collecting jsonschema!=2.5.0,>=2.4 (from nbformat>=4.2.0->ipywidgets->jupyter)
Downloading https://files.pythonhosted.org/packages/77/de/47e35a97b2b05c2fadbec67d44cfcdcd09b8086951b331d82de90d2912da/jsonschema-2.6.0-py2.py3-none-any.whl
Collecting ptyprocess; os_name != "nt" (from terminado>=0.8.1->notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from jinja2->notebook->jupyter)
Collecting html5lib!=1.0b1,!=1.0b2,!=1.0b3,!=1.0b4,!=1.0b5,!=1.0b6,!=1.0b7,!=1.0b8,>=0.99999999pre (from bleach->nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/a5/62/bbd2be0e7943ec8504b517e62bab011b4946e1258842bc159e5dfde15b96/html5lib-1.0.1-py2.py3-none-any.whl (117kB)
100% |████████████████████████████████| 122kB 15.7MB/s
Requirement already satisfied: wcwidth in /usr/local/lib/python3.6/site-packages (from prompt-toolkit<2.0.0,>=1.0.0->jupyter-console->jupyter) (0.1.7)
Requirement already satisfied: parso>=0.3.0 in /usr/local/lib/python3.6/site-packages (from jedi>=0.10->ipython>=4.0.0->ipykernel->jupyter) (0.3.1)
Collecting webencodings (from html5lib!=1.0b1,!=1.0b2,!=1.0b3,!=1.0b4,!=1.0b5,!=1.0b6,!=1.0b7,!=1.0b8,>=0.99999999pre->bleach->nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Installing collected packages: ipython-genutils, decorator, traitlets, jupyter-core, pyzmq, jupyter-client, simplegeneric, ptyprocess, pexpect, ipython, ipykernel, terminado, prometheus-client, jsonschema, nbformat, MarkupSafe, jinja2, pandocfilters, entrypoints, mistune, testpath, webencodings, html5lib, bleach, nbconvert, Send2Trash, notebook, widgetsnbextension, ipywidgets, qtconsole, jupyter-console, jupyter
Successfully installed MarkupSafe-1.0 Send2Trash-1.5.0 bleach-2.1.3 decorator-4.3.0 entrypoints-0.2.3 html5lib-1.0.1 ipykernel-4.8.2 ipython-6.4.0 ipython-genutils-0.2.0 ipywidgets-7.3.0 jinja2-2.10 jsonschema-2.6.0 jupyter-1.0.0 jupyter-client-5.2.3 jupyter-console-5.2.0 jupyter-core-4.4.0 mistune-0.8.3 nbconvert-5.3.1 nbformat-4.4.0 notebook-5.6.0 pandocfilters-1.4.2 pexpect-4.6.0 prometheus-client-0.3.0 ptyprocess-0.6.0 pyzmq-17.1.0 qtconsole-4.3.1 simplegeneric-0.8.1 terminado-0.8.1 testpath-0.3.1 traitlets-4.3.2 webencodings-0.5.1 widgetsnbextension-3.3.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
bash-3.2$ pip3 list
Package Version
------------------ -------
appnope 0.1.0
backcall 0.1.0
bleach 2.1.3
cycler 0.10.0
decorator 4.3.0
entrypoints 0.2.3
html5lib 1.0.1
ipykernel 4.8.2
ipython 6.4.0
ipython-genutils 0.2.0
ipywidgets 7.3.0
jedi 0.12.1
Jinja2 2.10
jsonschema 2.6.0
jupyter 1.0.0  <-------インストールされてますね!
jupyter-client 5.2.3
jupyter-console 5.2.0
jupyter-core 4.4.0
kiwisolver 1.0.1
MarkupSafe 1.0
matplotlib 2.2.2
mistune 0.8.3
nbconvert 5.3.1
nbformat 4.4.0
neo4j-driver 1.5.3
notebook 5.6.0
numpy 1.14.5
pandocfilters 1.4.2
parso 0.3.1
pexpect 4.6.0
pickleshare 0.7.4
pip 10.0.1
prometheus-client 0.3.0
prompt-toolkit 1.0.15
ptyprocess 0.6.0
Pygments 2.2.0
pyparsing 2.2.0
python-dateutil 2.7.3
pytz 2018.5
pyzmq 17.1.0
qtconsole 4.3.1
Send2Trash 1.5.0
setuptools 39.0.1
simplegeneric 0.8.1
six 1.11.0
terminado 0.8.1
testpath 0.3.1
tornado 5.1
traitlets 4.3.2
wcwidth 0.1.7
webencodings 0.5.1
wheel 0.31.0
widgetsnbextension 3.3.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

 

 Jupyter Notebook を起動する

ターミナルで、ファイル操作(pythonファイルの保存など)を行うディレクトリへ移動し、jupyter notebookを実行します。

bash-3.2$ cd /tmp
bash-3.2$ pwd
/tmp
bash-3.2$ jupyter notebook
[I 18:16:28.047 NotebookApp] Writing notebook server cookie secret to /Users/xxx/Library/Jupyter/runtime/notebook_cookie_secret
[I 18:16:28.887 NotebookApp] Serving notebooks from local directory: /private/tmp
[I 18:16:28.887 NotebookApp] The Jupyter Notebook is running at:
[I 18:16:28.887 NotebookApp] http://localhost:8888/?token=06c711274d041bd7b25c0065e1040515e7c83d2b01a403c1
[I 18:16:28.887 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 18:16:28.888 NotebookApp]

Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=06c711274d041bd7b25c0065e1040515e7c83d2b01a403c1
[I 18:16:29.258 NotebookApp] Accepting one-time-token-authenticated connection from ::1

 

すると、ブラウザが起動しJupyter Notebook 環境を使うことが出来ます。

 ※起動時にいたカレントディレクトリの内容が表示

f:id:opensourcetech:20180722183407p:plain

 Jupyter Notebook から Pythonを操作する

右側にある「New」からPython3を選択。

f:id:opensourcetech:20180722183447p:plain

新しくタブが開くなどし、緑で囲まれた箇所にPythonのプログラムを入力します。

f:id:opensourcetech:20180722183503p:plain

 

Numpyを使って足し算をしてみる例

※Outのところに、答えが帰されます。

f:id:opensourcetech:20180722183515p:plain

 

Pythonファイルの保存は、File > Save as... などから行います。

f:id:opensourcetech:20180722183546p:plain

f:id:opensourcetech:20180722183610p:plain

 

ディレクトリに、test.py が追加されました。

f:id:opensourcetech:20180722183621p:plain

 

ターミナルのログを見ても、上記の操作がされていることがわかります。

[I 18:19:16.591 NotebookApp] Creating new notebook in
[I 18:19:17.875 NotebookApp] Kernel started: 28b34ca8-c2f0-4eb0-acf0-e1c1ff76a01f
[I 18:19:19.564 NotebookApp] Adapting to protocol v5.1 for kernel 28b34ca8-c2f0-4eb0-acf0-e1c1ff76a01f
[W 18:21:53.809 NotebookApp] 404 GET /api/contents/test.py?type=notebook&content=0&_=1532251157154 (::1) 4.08ms referer=http://localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3
[I 18:21:53.818 NotebookApp] Uploading file to /test.py

 

 

 Jupyter Notebook を終了する

 ターミナルへ ctrl+c を入力します。

すると、以下のように終了するか(yes/no)を聞かれるので、

yを選択すると終了します。

[I 18:37:40.658 NotebookApp] interrupted
Serving notebooks from local directory: /private/tmp
2 active kernels
The Jupyter Notebook is running at:
http://localhost:8888/?token=06c711274d041bd7b25c0065e1040515e7c83d2b01a403c1
Shutdown this notebook server (y/[n])? y
[C 18:37:45.591 NotebookApp] Shutdown confirmed
[I 18:37:45.594 NotebookApp] Shutting down 2 kernels
[I 18:37:45.906 NotebookApp] Kernel shutdown: f20c1d6f-42c8-4c72-93bc-13e710f28fbd
[I 18:37:45.909 NotebookApp] Kernel shutdown: 28b34ca8-c2f0-4eb0-acf0-e1c1ff76a01f
bash-3.2$

 

 

 

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

Opensourcetech by Takahiro Kujirai