#author("2020-01-31T12:10:29+00:00","","")
#mynavi()
#setlinebreak(on)

* 目次 [#aca986db]
#contents
- 関連
-- [[Python]]
-- [[Python覚え書き]]
-- [[IPythonの使い方メモ]]

#TODO

* インストール [#n8186efa]
#html(<div style="padding-left:10px">)
https://www.anaconda.com/download/
#html(</div>)

* 起動 [#ncfccf39]
#html(<div style="padding-left:10px">)
#myterm2(){{
jupyter notebook
}}
#html(</div>)

* マジックコマンド [#nca4ac2a]
#html(<div style="padding-left:10px">)
基本的に IPython のマジックコマンドが分かってれば大丈夫。
※[[IPythonの使い方メモ]] 参照
#html(</div>)

* Tips [#s7748688]
#html(<div style="padding-left:10px">)

** 全ての変数等を整形して表示する [#h137d758]
#html(<div class="pl10">)

変数名を書くだけで表示
#mycode2(){{
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

df1 = .......
df2 = .......

df1
df2
}}

display による表示
#mycode2(){{
from IPython.display import display

  :
display(df1)
display(df2)
}}

#html(</div>)

** 自作モジュールを自動リロードする [#c969f77c]
#html(<div class="pl10">)

以下を行っておくと import 済みのモジュールが実行の度にリロードされる為、モジュールを修正した場合の読み込み直しの手間が減る。

#mycode2(){{
%load_ext autoreload
%autoreload 2
}}

#html(</div>)


** データフレームの表示行数 または 列数を設定する [#fc8fa079]
#html(<div class="pl10">)

pandas のデータフレームを表示する際に 行数 または 列数が多い場合は途中が省略して表示されるが、以下の通り最大表示数を設定する事ができる。

#mycode2(){{
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
}}

#html(</div>)


** 共通のスタートアップスクリプト(その1) [#hb3a8458]
#html(<div class="pl10">)

各ノートブック共通で行う初期設定などは ~/.ipython/profile_default/startup 配下にファイルを設置する事によって自動で行う事ができる。

※jupyter ではなく ipython の仕組みの模様。
※ファイル名順に読み込まれる為、ファイル名の接頭文字に連番を付与して読み込み順を制御する事が可能。

例) ~/.ipython/profile_default/startup/00-init.ipy
#mycode2(){{
# オブジェクトの自動表示
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

# オブジェクトの表示用
from IPython.display import display

# 実行する度に自作モジュールを自動リロードする
%load_ext autoreload
%autoreload 2

# グラフを常にインライン表示
%matplotlib inline

# 最大列数、行数の設定
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
}}
#html(</div>)

** 共通のスタートアップスクリプト(その2) [#o6370453]
#html(<div class="pl10">)

別のやり方として  ~/.ipython/profile_default/ipython_config.py に書く方法もある。

まずプロファイルを生成。
#myterm2(){{
ipython profile create
[ProfileCreate] Generating default config file: '/ユーザのホームディレクイトリ/.ipython/profile_default/ipython_config.py'
}}

生成されたプロファイルを以下の様に修正する。

例) &#126;/.ipython/profile_default/ipython_config.py
#mycode2(){{
  :
# 実行するコマンドを1行ずつ指定
c.InteractiveShellApp.exec_lines = [
    'import numpy as np',
    'import pandas as pd'
]

# 読み込むファイルのPATHを相対 または フルPATHで指定
c.InteractiveShellApp.exec_files = [
    'settings.ipy',
    '/xxx/xxxxx/settings.ipy'
]
  :
}}

#html(</div>)

** 他のノートブックを読み込む [#q1f087ee]
#html(<div class="pl10">)

やり方はある様だが・・・
https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html

たぶん普通にマジックコマンドで  %run するのが、いちばんシンプル。
#mycode(){{
%run other_notebook.ipynb
}}

#html(</div>)



#html(</div>)

トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS