SlideShare a Scribd company logo
1 of 25
Download to read offline
Y U M I K O N A G ATA K E
D E C . 1 9 . 2 0 1 4 	
 
PYTHON FOR DATA
ANALYSIS (3)
IPythonについて	
•  通常の標準インタプリタと同様、OSシェルのコマンドラインから
起動させる。
•  Pyhtonのprint関数と⽐比べ、pythonオブジェクトが綺麗麗に整形され
てよみやすい。
3.1.1 タブによる補完(1)	
•  タブの補完
IPythonのシェルに表現(⽂文字)の⼀一部を⼊入⼒力力し、タブ
<Tab>を押すと、その場所においてある空間からマッ
チする⼊入⼒力力候補を提⽰示する機能。
補完対象:オブジェクト名、オブジェクト型名、関数名等
3.1.1 タブによる補完(2) 	
•  オブジェクト変数のメソッドを呼び出すことも。
•  モジュールも同様。
3.1.2 イントロスペクション(1)	
•  変数の前後どちらかに疑問符(?)をつけたらそのオブ
ジェクトの⼀一般情報を⾒見見ることができる。
オブジェクトイントロスペクション
3.1.2 イントロスペクション(2)	
•  add_̲numbers関数の情報をみると・・・
3.1.2 イントロスペクション(3)	
•  さらに??をつけると、その関数のソースコードが表⽰示さ
れる。
3.1.2 イントロスペクション(4)	
•  ?のもう⼀一つの機能:名前空間探索索
→UNIX系のワイルドカード機能を呼び出す際に使われ
る。
↑Numpyのトップレベルの名前空間でloadという⽂文字
を含む全ての関数を表⽰示させる。
3.1.3 %run コマンド(1)	
•  %runコマンドを使うと、Pythonプログラムを起動でき
る。
    (ファイルに保存されているスクリプトを実⾏行行する。)
    chap3.py
実⾏行行中コードを終了了する場合は、<Ctrl-‐‑‒C>を押しましょ
う。
3.1.4 クリップボードからの実行コード(1)	
•  クリップボードから直接貼り付けできる!
→ちょっとしたコード⽚片も試せる。
→わざわざ〜~.pyファイルを作る必要がなくなる!
                                                        事前に貼り付けたいコード
                                をコピーしておき、%paste
                                    を使う。
%pasteはクリップボードの中⾝身を1つのブロックとして扱う。
%cpasteも同様。
3.1.5 キーボードショートカット	
コマンド	
  説明	
 
Ctrl-p or ↑	
  コマンド履履歴から⼊入⼒力力したものを遡って表⽰示
Ctrl-N or ↓ コマンド履履歴から⼊入⼒力力したものを時系列列順に表⽰示
Ctrl-R コマンド履履歴を遡ってインクリメンタルサーチ(?)
Ctrl-Shift-V クリップボードから貼り付け
Ctrl-C 実⾏行行中コードの中断
Ctrl-A カーソルを⾏行行頭へ
Ctrl-E カーソルを⾏行行末へ
Ctrl-K カーソル位置から⾏行行末までのテキストを削除
Ctrl-U 現在⼊入⼒力力している⾏行行の全てのテキストを削除
Ctrl-F カーソルを1⽂文字分進める
Ctrl-B カーソルを1⽂文字分戻す
Ctrl-L 画⾯面の消去
3.1.7 マジックコマンド(1)	
•  マジックコマンドの⽬目的:
①通常タスクを容易易に⾏行行う
②Ipythonシステムを容易易にコントロールする
マジックコマンド例例:%timeit  
                    (Python命令令の実⾏行行時間を計測)
コマンドラインオプションを確認する時は?を使うこと
3.1.7 マジックコマンド(2)	
コマンド	
  説明	
 
%quickref Ipythonのクイックリファレンスを表⽰示
%magic 全てのマジックコマンドの詳細な説明を表⽰示
%hist コマンド⼊入⼒力力の履履歴を表⽰示
%pdb	
  例例外発⽣生後のデバッガー⾃自動切切り替え
%paste クリップボード内の整形済みコードの実⾏行行
%cpaste Pythonコードの実⾏行行のための特別なプロンプトを表⽰示
%reset 名前空間から名前を削除する
%page OBJECT 引数のObjectを綺麗麗に表⽰示する(less  と同じ?)
3.1.7 マジックコマンド(3)	
コマンド	
  説明	
 
%run script.py 引数(script.py)を、IPython内で実⾏行行
%prun statement プロファイラ出⼒力力を表⽰示し、cProfile付きの命令令を実⾏行行
%time statement 命令令の実⾏行行時間を計測
%timeit
statement
命令令の実⾏行行時間を複数回計測
%who, %who_ls,
%whos
利利⽤用中の名前空間の中にある、変数のリストを返す
%xdel variable オブジェクトを削除
3.1.9 Matplotlib 連携・Pylabモード	
•  Ipythonでmatplotlibを起動すると…
  *IPythonシェルが、描画中も機能する
  *IPythonをmatplotlib機能付きで起動するには…
        $  ipython  -‐‑‒-‐‑‒pylab
•    matplotlibについては、後述で記載します
 
3.2 入出力のログについて	
•  ⼊入⼒力力コマンドだけでなく、出⼒力力もログすることができる
  %logstart
3.3 オペレーティングシステムとの連携	
コマンド	
  説明	
 
!cmd	
  OSのシェル上でcmdを実⾏行行
output = !cmd args cmdを実⾏行行し、stdoutに出⼒力力される実⾏行行結果を保管
%alias alias_name
cmd
シェルコマンドに別名をつける
%bookmark ディレクトリブックマーク機能を有効にする
%cd directory	
  引数先のディレクトリへ移動
%pwd 現在の作業ディレクトリを表⽰示
%pushd directory 現在の作業ディレクトリを保存し、引数先へ移動
%popd スタックに保存されたディレクトリを削除し、そこへ
ディレクトリを移動
%dirs 現在のディレクトリのスタック状況を表⽰示
%dhist ディレクトリの訪問履履歴を表⽰示
%env 	
  システム環境変数を、ディクショナリ変数に変換
3.3.1 ディレクトリブックマーク機能	
•  IPythonは、さまざまなディレクトリを⾏行行き来できるよ
うにブックマーク機能があります
•  愛⽤用しているディレクトリをブックマークに登録
  
  %bookmark  bm  /Users/nagatake/Desktop/
python_̲for_̲data_̲analysis
        (⾚赤⽂文字はブックマーク名)
以降降、マジック関数%cdの引数にこのブックマーク名を
与えらえるようになる
3.4 ソフトウェア開発ツール	
•  インタラクティブデバッガ
pdbデバッガの特徴:タブ補完・構⽂文のハイライト
                    例例外トレースバッグ時の周辺⾏行行表⽰示
•  run  ch03/ipython_̲bug.py  をしてみましょう。(デフォルト)
3.4 ソフトウェア開発ツール(2)	
•  %debugを使うと…
•  デフォルトでは開始位置:スタックの最下層(9⾏行行⽬目から)
•  スタック内の移動はu(up)またはd(down)を使う
3.5 処理時間計測:%time と %timeit	
•  データ分析アプリケーションが⼤大規模になったり、
⻑⾧長時間実⾏行行する場合、どの関数がいちばん時間がかかっ
ているか、等実⾏行行時間の計測は⾮非常に重要!
(しかしレベルはさまざま…)
•  でも、処理理時間の計測のためにコードを書くのが⾯面倒く
さい!
→%timeや%timeitを利利⽤用しよう
%time  指定した処理理を1度度だけ実⾏行行
(ex.  ⽂文字列列リストのうち、特定の⽂文字列列から始まるものを抽出する
場合、各methodの⽐比較ができる)
3.5 処理時間計測:%time と %timeit(2)	
•  作業:600,000の⽂文字列列からなるリストから、ʼ’fooʼ’から
始まる⽂文字列列を抽出する
strings  =  ['foo',  'foobar',  'baz',  'qux',  
    'python',  'Guido  Van  Rossum']  *  100000  
method1  =  [x  for  x  in  strings  if  x.startswith('foo')]  
method2  =  [x  for  x  in  strings  if  x[:3]  ==  'foo']  
このmethod2つを%timeで簡単に⽐比較できる!
3.5 処理時間計測:%time と %timeit(3)	
•  結果は…
この結果からmethod2の⽅方が良良いのではないかという判
断が下せる
3.5 処理時間計測:%time と %timeit(4)	
•  ちなみに・・・Wall  time  :  実⾏行行処理理時間
この値は厳密な計測とはいえない(何度度かやると、毎回
値が違う可能性が⾼高い)
•  そこで、%timeitの登場!
  有⽤用な場⾯面:実⾏行行時間がものすごく短いもの
3.6 IPython HTMLノートブック	
•  (今更更かもしれませんが・・・)
IPythonノートブック
      :Web技術を基盤とした⽂文書フォーマット  
コードの出⼒力力や図の共有に⾮非常に便便利利!
  起動⽅方法は
  $ipython  notebook  00pylab=inline

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Chapter3