pythonによるデータ分析入門を写経していく(ipythonの使い方)
Ipythonについて
- Ipythonはタブ補完や、フォーマット整形などをしてくれる高性能pythonのコンソール
- python2系の場合は
ipython
,3系の場合はipython3
で起動する
- python2系の場合は
- pythonの開発にリッチなIDEはいらない.Ipythonがあればいい!という意見があるようです。
- たしかに、LL系の言語だと、IDEいらないってのはあるな
- irbとかipythonとか、Rのコンソールで動かして、動いたらコードに張りつけという形で開発を進めることが多いような
- けど、IDEがあったほうが落ち着くのです。。。私は。
Ipythonの使い方
- 以下のデモは、pythonの3系でやってます
イントロスペクション
- 変数の後ろに?をつけると、そのオブジェクトの情報を返してくれる
stringの場合
In [216]: this_is_str = 'hogehoge' In [217]: this_is_str? Type: str String Form:hogehoge Length: 8 Docstring: str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
数字の場合
In [219]: this_is_num = 8
In [220]: this_is_num?
Type: int
String Form:8
Docstring:
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
関数の場合
In [223]: add(2,3)
Out[223]: 5
In [224]: add?
Type: function
String Form:<function add at 0x109c9b170>
File: /Users/michi/Desktop/numpy/ch02/<ipython-input-222-2cc5e7bf157f>
Definition: add(x, y)
Docstring: <no docstring>
関数の場合??でソースの表示も可能
In [225]: add?? Type: function String Form:<function add at 0x109c9b170> File: /Users/michi/Desktop/numpy/ch02/<ipython-input-222-2cc5e7bf157f> Definition: add(x, y) Source: def add(x,y): return x+y
ワイルドカードを用いた検索
*
と?
で可能In [227]: np.*load*? np.__loader__ np.load np.loads np.loadtxt np.pkgload
クリップボードから直接実行
%paste
でクリップボードのソースを直接いきなり実行In [288]: %paste x = 1 y = 2 if (x > 0 ): c= x+y y = 100 c ## -- End pasted text -- Out[288]: 3
%cpaste
でもう一度、貼付けて、確認してから実行%cpasteコマンドをたたしてから
ctr+v
で確認してから、Enterで実行(中止はctr+d
)In [292]: %cpaste Pasting code; enter '--' alone on the line to stop or use Ctrl-D. :x = 1 :y = 2 :if (x > 0 ): : c= x+y : : y = 100 : :c :-- Out[292]: 3
実行時間の測定
− %run
や関数の実行時間を測定できるコマンド%timeit
と%time
がある
- %timeit
の方は、複数実行した結果を返してくれるので、短いメソッドに効果的
In [295]: %timeit add(3,4)
10000000 loops, best of 3: 118 ns per loop
過去の入出力を取得する
一つ前の出力を取得する
- 1つ前の出力の取得は
_
- 2つ前の出力は
__
(アンダースコア2つ) 3つ前の出力は
___
(アンダースコア3つ)In [314]: x + y Out[314]: 5 In [315]: x + y + 1 Out[315]: 6 In [316]: x + y + 2 Out[316]: 7 In [317]: _ Out[317]: 7 In [318]: ___ Out[318]: 6
特定の行の入出力を取得する
- 入力の取得は
_i{行番号}
出力の取得は
_{行番号}
In [304]: hoge = 'fuga' In [305]: hoge Out[305]: 'fuga' In [306]: _i305 Out[306]: 'hoge' In [307]: _305 Out[307]: 'fuga'
OSのコマンドを使う
!
をプレフィックスにつけるとOSコマンドをpythonから触れるIn [322]: ls figure_1.png figure_2.png figure_3.png In [326]: foo = !ls | grep figure_1 In [327]: foo Out[327]: ['figure_1.png']