プログラミングとかLinuxとかの備忘録

プログラミング、Linuxでハマった箇所や環境構築のメモ

Jupyter Notebook (IPython Notebook)をインストールして試す

スポンサーリンク

使用環境

$ cat /etc/redhat-release 
Fedora release 23 (Twenty Three)

インストール

仮想環境作成

$ mkvirtualenv --no-site-packages --python /opt/python3.5.1/bin/python3.5 notebook
(notebook)$ pip install -U pip

IPythonNotebookのインストール

(notebook)$ pip install ipython jupyter

IPython4.0以上では,jupyterを別にインストールしないとipython notebookすると以下のエラーが発生する(ImportError: No module named notebook.notebookapp)

(notebook)$ ipython notebook
...
ImportError: No module named notebook.notebookapp

その他ライブラリのインストール

(notebook)$ pip install matplotlib
(notebook)$ pip install Cython pandas

この時点で自動でインストールされたものも含めると以下のパッケージ構成になる

(notebook)$ pip freeze
cycler==0.10.0
Cython==0.23.4
decorator==4.0.9
ipykernel==4.2.2
ipython==4.1.1
ipython-genutils==0.1.0
ipywidgets==4.1.1
Jinja2==2.8
jsonschema==2.5.1
jupyter==1.0.0
jupyter-client==4.1.1
jupyter-console==4.1.1
jupyter-core==4.0.6
MarkupSafe==0.23
matplotlib==1.5.1
mistune==0.7.1
nbconvert==4.1.0
nbformat==4.0.1
notebook==4.1.0
numpy==1.10.4
pandas==0.17.1
path.py==8.1.2
pexpect==4.0.1
pickleshare==0.6
ptyprocess==0.5.1
Pygments==2.1.1
pyparsing==2.1.0
python-dateutil==2.4.2
pytz==2015.7
pyzmq==15.2.0
qtconsole==4.2.0
simplegeneric==0.8.1
six==1.10.0
terminado==0.6
tornado==4.3
traitlets==4.1.0
wheel==0.24.0

IPython(Jupyter) Notebookを試す

ipython notebookで以下のような警告が出た.
jupyter notebookに変わったらしい.

(notebook)$ ipython notebook
[TerminalIPythonApp] WARNING | Subcommand `ipython notebook` is deprecated and will be removed in future versions.
[TerminalIPythonApp] WARNING | You likely want to use `jupyter notebook`... continue in 5 sec. Press Ctrl-C to quit now.

改めて

(notebook)$ jupyter notebook

とすると、以下のページが開く

f:id:vild:20200606170607p:plain
Jupyter Notebook 1

右上のNewからPython3を選択すると以下のようにNotebookが開く

f:id:vild:20200606170324p:plainf:id:vild:20200606170037p:plain
Jupyter Notebook 2,3

pandas,matplotlibの利用

numpyを使用してデータを作成

import numpy as np

X = [i*np.pi/180. for i in range(0, 180)]
Y = [np.cos(x) for x in X]

作成したデータをpandasのデータフレームに変換し,csvに書き出す

import pandas as pd

data1 = {"x": X, "y": Y}
data1_pd = pd.DataFrame.from_records(data1)

data1_pd.to_csv("data.csv")

csvからデータを読み込む

import pandas as pd

data = pd.read_csv("data.csv")

matplotlibを使用して読み込んだデータをプロットする

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot(data["x"], data["y"])

以上をJupyter Notebookで実行した結果が以下のスクリーンショット

f:id:vild:20200606170035p:plain
Jupyter Notebook 4
f:id:vild:20200606165819p:plain
Jupyter Notebook 5