$30 off During Our Annual Pro Sale. View Details »

JupyterNotebook入門

Masa
February 18, 2018

 JupyterNotebook入門

はんなりPython#3 2018/02/16
JupyterNotebook について入門者向け解説
https://github.com/masayuki14/hannari-python-3

https://hannari-python.connpass.com/event/77366/

Masa

February 18, 2018
Tweet

More Decks by Masa

Other Decks in Programming

Transcript

  1. Jupyter
    Notebook 入門
    はんなりPython #3
    2018/02/16
    Navigate : Space / Arrow Keys | - Menu | - Fullscreen | - Overview | - Blackout | - Speaker | - Help
    M F O B S ?

    1 / 52

    View Slide

  2. 自己紹介
    森崎雅之
    @masayuki14
    Follow me !!
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    2 / 52

    View Slide

  3. 自己紹介
    コミュニティ
    はんなりPython
    OSS Gate
    主夫
    兼 パートタイムプログラマ
    兼 スプーキーズアンバサダー
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    3 / 52

    View Slide

  4. スプーキーズ@京都
    ソーシャルゲーム ✕ ボードゲーム
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    4 / 52

    View Slide

  5. スプーキーズ@京都
    社内勉強会やってます
    WebRTCを活用する
    Docker勉強会
    ゲームAIを作って競い合う構想
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    5 / 52

    View Slide

  6. スプーキーズ@京都
    エンジニア勉強会 #1 3/23(金)
    connpass 79323
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    6 / 52

    View Slide

  7. スプーキーズ@京都
    Webエンジニア募集中
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    7 / 52

    View Slide

  8. 今日する話
    Jupyter
    Notebook 入門
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    8 / 52

    View Slide

  9. 今日する話
    1. Jupyter Notebook とは
    2. Use Jupyter Notebook
    3. Use Pandas
    4. 身近なデータを可視化
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    9 / 52

    View Slide

  10. Jupyter Notebook とは
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    10 / 52

    View Slide

  11. Jupyter Notebook とは
    Project Jupyter
    複数のプログラミング言語にまたがるインタラクティ
    ブコンピューティングのためのサービスを開発する
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    11 / 52

    View Slide

  12. Jupyter Notebook とは
    Project Jupyter のサービスの1つ
    ライブコード、方程式、可視化、テキストを含むドキ
    ュメントを作成して共有できるオープンソースの
    Webアプリケーション
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    12 / 52

    View Slide

  13. Use Jupyter Notebook
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    13 / 52

    View Slide

  14. Use Jupyter Notebook
    by Docker
    FROM python:latest
    # Install miniconda to /miniconda
    RUN curl -LO 'https://repo.continuum.io/miniconda/Miniconda3-late
    RUN bash Miniconda3-latest-Linux-x86_64.sh -b -p /miniconda
    ENV PATH=/miniconda/bin:${PATH}
    RUN conda update -y conda
    # install for jupyter notebook
    RUN conda install -y pandas matplotlib nb_conda
    RUN conda install -y pyyaml
    RUN mkdir -p /root/notebook
    WORKDIR /root/notebook
    CMD jupyter notebook --ip=0.0.0.0 --allow-root
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    14 / 52

    View Slide

  15. Use Jupyter Notebook
    Build image
    Run docker
    $ docker build -t jupyter .
    $ docker run -it --rm -v $(pwd)/notebook:/root/notebook -p 80:888
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    15 / 52

    View Slide

  16. Use Jupyter Notebook
    起動ログに初回アクセスのURLが表示される
    Copy/paste this URL into your browser when you connect for the fi
    to login with a token:
    http://0.0.0.0:8888/?token=ba4fc6de0d99161f5e144ad4c1167ebf074
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    16 / 52

    View Slide

  17. Use Jupyter Notebook
    にアクセス!!
    http://localhost/?
    token=ba4fc6de0d99161f5e144ad4c1167ebf074ddc29b916
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    17 / 52

    View Slide

  18. Use Jupyter Notebook
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    18 / 52

    View Slide

  19. Use Jupyter Notebook
    Hello world
    def hello():
    return 'Hello Jupyter.'
    hello()
    'Hello Jupyter.'
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    19 / 52

    View Slide

  20. Use Jupyter Notebook
    グラフを表示
    # グラフ表示を有効化
    %matplotlib inline
    import pandas as pd
    df = pd.DataFrame([1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987
    df.plot()
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    20 / 52

    View Slide

  21. Use Jupyter Notebook
    データの特徴
    fibo_pd.describe()
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    21 / 52

    View Slide

  22. Use Jupyter Notebook
    Markdown
    Markdownでメモなどを残せる
    LaTeXで数式を書ける
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    22 / 52

    View Slide

  23. Use Jupyter Notebook
    Notebook URL
    保存した .ipynb ファイルをGitHubにPushすると表
    示できる
    notebook
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    23 / 52

    View Slide

  24. Use Pandas
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    24 / 52

    View Slide

  25. Use Pandas
    1次元データ構造のシリーズ(Series)
    2次元データ構造のデータフレーム(DataFrame)
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    25 / 52

    View Slide

  26. Use Pandas
    Pandasをつかおう
    import pandas as pd
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    26 / 52

    View Slide

  27. Use Pandas
    データフレームの基本的な使い方
    In [1]: import pandas as pd
    ...:
    ...: # columnsオプションで列名を指定
    ...: df = pd.DataFrame([1,2,3],
    ...: columns=['value'])
    ...: df
    Out[1]:
    value
    0 1
    1 2
    2 3
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    27 / 52

    View Slide

  28. Use Pandas
    タプルで配列を渡す
    In [2]: df = pd.DataFrame([
    ...: ('apple', 100), ('oragne', 230), ('grape', 290), ('ba
    ...: columns=['name', 'price']
    ...: )
    ...: df
    Out[2]:
    name price
    0 apple 100
    1 oragne 230
    2 grape 290
    3 banana 100
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    28 / 52

    View Slide

  29. Use Pandas
    ディクショナリで配列を渡す
    In [3]: df = pd.DataFrame({
    ...: 'name': ['apple', 'orange', 'pear', 'peach'],
    ...: 'price': [120, 150, 230, 360],
    ...: 'order': [3, 8, 4, 5]
    ...: })
    ...: df
    Out[3]:
    name order price
    0 apple 3 120
    1 orange 8 150
    2 pear 4 230
    3 peach 5 360
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    29 / 52

    View Slide

  30. Use Pandas
    列の追加
    In [4]: df['color'] = ['red', 'orange', 'green', 'pink']
    ...: df['total'] = df['order'] * df['price']
    ...: df
    Out[4]:
    name
    order
    price
    color
    total
    0
    apple
    3
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    30 / 52

    View Slide

  31. Use Pandas
    インデックスの追加
    In [5]: df.index = ['Apple', 'Orange', 'Pear', 'Peach']
    ...: df
    Out[5]:
    name order price color total
    Apple apple 3 120 red 360
    Orange orange 8 150 orange 1200
    Pear pear 4 230 green 920
    Peach peach 5 360 pink 1800
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    31 / 52

    View Slide

  32. Use Pandas
    列の選択
    In [6]: df['price']
    Out[6]:
    Apple 120
    Orange 150
    Pear 230
    Peach 360
    Name: price, dtype: int64
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    32 / 52

    View Slide

  33. Use Pandas
    列の選択(複数)
    In [7]: df[['price', 'color']]
    Out[7]:
    price color
    Apple 120 red
    Orange 150 orange
    Pear 230 green
    Peach 360 pink
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    33 / 52

    View Slide

  34. Use Pandas
    head, tail
    In [8]: df.head(2)
    Out[8]:
    name order price color total
    Apple apple 3 120 red 360
    Orange orange 8 150 orange 1200
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    34 / 52

    View Slide

  35. Use Pandas
    インデックス指定
    In [9]: df.loc[['Apple', 'Pear']]
    In [9]: df[1:3]
    Out[9]:
    name order price color total
    Orange orange 8 150 orange 1200
    Pear pear 4 230 green 920
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    35 / 52

    View Slide

  36. Use Pandas
    条件による指定
    In [10]: df[df.price > 200]
    Out[10]:
    name order price color total
    Pear pear 4 230 green 920
    Peach peach 5 360 pink 1800
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    36 / 52

    View Slide

  37. Use Pandas
    対話的、探索的にデータを操作できる
    https://git.io/vA8Uj
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    37 / 52

    View Slide

  38. 身近なデータを可視化
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    38 / 52

    View Slide

  39. 身近なデータを可視化
    Pandas でデータ操作だけじゃつ
    まらない
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    39 / 52

    View Slide

  40. 身近なデータを可視化
    そうだ可視化しよう
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    40 / 52

    View Slide

  41. 身近なデータを可視化
    グラフを書いてみよう
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    41 / 52

    View Slide

  42. 身近なデータを可視化
    今年は雪が多い
    福井県出身なんです
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    42 / 52

    View Slide

  43. [ GitPitch @ github/masayuki14/hannari-python-3 ]

    43 / 52

    View Slide

  44. 身近なデータを可視化
    昭和38年、昭和56年と比較する
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    44 / 52

    View Slide

  45. 身近なデータを可視化
    気象庁の観測データをDL
    http://www.data.jma.go.jp/gmd/risk/obsdl/index.php
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    45 / 52

    View Slide

  46. 身近なデータを可視化
    最深積雪をグラフにする
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    46 / 52

    View Slide

  47. 身近なデータを可視化
    年別の最深積雪
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    47 / 52

    View Slide

  48. 身近なデータを可視化
    昭和38年 (1963年)
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    48 / 52

    View Slide

  49. 身近なデータを可視化
    昭和56年 (1981年)
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    49 / 52

    View Slide

  50. 身近なデータを可視化
    平成30年 (2018年)
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    50 / 52

    View Slide

  51. まとめ
    Jupyter Notebook を使うことは
    データサイエンスのはじめの一歩
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    51 / 52

    View Slide

  52. ありがとうございました
    発表資料 https://git.io/vAl8
    [ GitPitch @ github/masayuki14/hannari-python-3 ]

    52 / 52

    View Slide