August 8, 2010

Carbon Emacsのpython-modeの設定

Carbon Emacsにはデフォルトでpython-mode.elが入ってる。
ただし、設定しないとpython.elのまま。python.elだとバッファ上で実行するから同じフォルダの他のファイルをインポートできない。ちょっと困る。時もある。

以下設定方法。

~/.emacsファイルに次のように記述。
;;; python-mode
(autoload 'python-mode "python-mode" "Python editing mode." t)

以下使い方。python-mode.elより抜粋。後で見やすくする?
;; electric keys
(define-key py-mode-map ":" 'py-electric-colon)
;; indentation level modifiers
(define-key py-mode-map "\C-c\C-l" 'py-shift-region-left)
(define-key py-mode-map "\C-c\C-r" 'py-shift-region-right)
(define-key py-mode-map "\C-c<" 'py-shift-region-left) (define-key py-mode-map "\C-c>" 'py-shift-region-right)
;; subprocess commands
(define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
(define-key py-mode-map "\C-c\C-m" 'py-execute-import-or-reload)
(define-key py-mode-map "\C-c\C-s" 'py-execute-string)
(define-key py-mode-map "\C-c|" 'py-execute-region)
(define-key py-mode-map "\e\C-x" 'py-execute-def-or-class)
(define-key py-mode-map "\C-c!" 'py-shell)
(define-key py-mode-map "\C-c\C-t" 'py-toggle-shells)
;; Caution! Enter here at your own risk. We are trying to support
;; several behaviors and it gets disgusting. :-( This logic ripped
;; largely from CC Mode.
;;
;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
;; backwards deletion behavior to DEL, which both Delete and
;; Backspace get translated to. There's no way to separate this
;; behavior in a clean way, so deal with it! Besides, it's been
;; this way since the dawn of time.
(if (not (boundp 'delete-key-deletes-forward))
(define-key py-mode-map "\177" 'py-electric-backspace)
;; However, XEmacs 20 actually achieved enlightenment. It is
;; possible to sanely define both backward and forward deletion
;; behavior under X separately (TTYs are forever beyond hope, but
;; who cares? XEmacs 20 does the right thing with these too).
(define-key py-mode-map [delete] 'py-electric-delete)
(define-key py-mode-map [backspace] 'py-electric-backspace))
;; Separate M-BS from C-M-h. The former should remain
;; backward-kill-word.
(define-key py-mode-map [(control meta h)] 'py-mark-def-or-class)
(define-key py-mode-map "\C-c\C-k" 'py-mark-block)
;; Miscellaneous
(define-key py-mode-map "\C-c:" 'py-guess-indent-offset)
(define-key py-mode-map "\C-c\t" 'py-indent-region)
(define-key py-mode-map "\C-c\C-d" 'py-pdbtrack-toggle-stack-tracking)
(define-key py-mode-map "\C-c\C-f" 'py-sort-imports)
(define-key py-mode-map "\C-c\C-n" 'py-next-statement)
(define-key py-mode-map "\C-c\C-p" 'py-previous-statement)
(define-key py-mode-map "\C-c\C-u" 'py-goto-block-up)
(define-key py-mode-map "\C-c#" 'py-comment-region)
(define-key py-mode-map "\C-c?" 'py-describe-mode)
(define-key py-mode-map "\C-c\C-h" 'py-help-at-point)
(define-key py-mode-map "\e\C-a" 'py-beginning-of-def-or-class)
(define-key py-mode-map "\e\C-e" 'py-end-of-def-or-class)
(define-key py-mode-map "\C-c-" 'py-up-exception)
(define-key py-mode-map "\C-c=" 'py-down-exception)
;; stuff that is `standard' but doesn't interface well with
;; python-mode, which forces us to rebind to special commands
(define-key py-mode-map "\C-xnd" 'py-narrow-to-defun)
;; information
(define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
(define-key py-mode-map "\C-c\C-v" 'py-version)
(define-key py-mode-map "\C-c\C-w" 'py-pychecker-run)
;; shadow global bindings for newline-and-indent w/ the py- version.
;; BAW - this is extremely bad form, but I'm not going to change it
;; for now.
(mapc #'(lambda (key)
(define-key py-mode-map key 'py-newline-and-indent))
(where-is-internal 'newline-and-indent))
;; Force RET to be py-newline-and-indent even if it didn't get
;; mapped by the above code. motivation: Emacs' default binding for
;; RET is `newline' and C-j is `newline-and-indent'. Most Pythoneers
;; expect RET to do a `py-newline-and-indent' and any Emacsers who
;; dislike this are probably knowledgeable enough to do a rebind.
;; However, we do *not* change C-j since many Emacsers have already
;; swapped RET and C-j and they don't want C-j bound to `newline' to
;; change.
(define-key py-mode-map "\C-m" 'py-newline-and-indent)

August 6, 2010

Snow Leopardに64bitのSciPyをインストールする

SciPyはPythonの数値解析ソフトウェア。

追記。

現在は Scipy Superpack
Recent builds of fundamental Python scientific computing packages for OS X
から。



SciPy.orgでも紹介されているが、Mac OS X 10.6 Snow LeopardにプリインストールされているPython 2.6.1で利用できる64bitのSciPyやらNumPyやらのインストールスクリプトがあって便利。(プリインストールのPythonのみ。FinkやらDarwin PortsからインストールしたPythonだと使えない、らしい。)

SciPy Superpack for Python 2.6 (64-bit)の下の方にある「Download Scipy Superpack Installer for OSX 10.6」とあるリンクからスクリプトをダウンロード。

ターミナルから
sh superpack_10.6_2010.07.27.sh
とすると、gFortranをインストールするか聞かれたり、管理者パスワードを聞かれたりしながら作業が進んでいく。

インストールされるのは
gFortran
ez_setup
numpy
matplotlib
scipy
pymc
ipython
nose
DateUtils

とても便利。

August 5, 2010

AutopagerizeでFilckrを除く

Tumblrで回避するために Exclude Patterns に /[^.]+\.tumblr\.com\// を追加した

July 29, 2010

Pythonで隠れマルコフモデルのSmoothingの例

Pythonで隠れマルコフモデルのFilteringの例のSmoothingバージョン.

スムージング
  • 現在まで(0~T)のすべての観測があるときに,過去の状態(t;0<t<T)を推定する.
  • i.e. P(Xt|Y0:T)を求める.
Filtering同様,P(Xt|Y0:T)を変形する.

cは正規化項.P(Xt|Y0:t)はフィルタリング(Forward Algorithm)で求められる.P(Yt+1|Xt+1)は出力確率.P(Xt+1|Xt)は遷移確率.で,再帰的にP(Yt+1:T|Xt)を求められる.以下実装.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 状態変数
states = ['Rainy', 'Sunny']

# 観測列
observations = ['walk', 'shop', 'clean', 'shop', 'walk']

# 初期確率
start_probability = {'Rainy': 0.6, 'Sunny': 0.4}
 
# 遷移確率
transition_probability = {
   'Rainy' : {'Rainy': 0.7, 'Sunny': 0.3},
   'Sunny' : {'Rainy': 0.4, 'Sunny': 0.6},
   }

# 出力確率 
emission_probability = {
   'Rainy' : {'walk': 0.1, 'shop': 0.4, 'clean': 0.5},
   'Sunny' : {'walk': 0.6, 'shop': 0.3, 'clean': 0.1},
   }

# Forward
def forward(y1, prior, states, trans_p, emit_p):
    post = {}
    for x1 in states:
        post[x1] = emit_p[x1][y1] * sum([ trans_p[x][x1] * prior[x] for x in states ])
    s = sum(post.values())
    for k,v in post.items():
        post[k] = v/s
    return post

# Filtering
def filtering(observations, states, start_p, trans_p, emit_p):
    prior = start_p
    T = len(observations)
    for t in range(T):
        post = forward( observations[t], prior, states, trans_p, emit_p )
        prior = post
    return post

# Backward
def backward(y1, prior, states, trans_p, emit_p):
    post = {}
    for x in states:
        post[x] = sum([ emit_p[x1][y1] * prior[x1] * trans_p[x][x1] for x1 in states ])
    s = sum(post.values())
    for k,v in post.items():
        post[k] = v/s
    return post

# Smoothing
def smoothing(observations, t, states, start_p, trans_p, emit_p):
    T = len(observations)
    # forward
    f = filtering(observations[:t], states, start_p, trans_p, emit_p)
    # backward
    b = {}
    for x in states:
        b[x] = 1.0 / len(states)
    for i in range(T-1, t-1,-1):
        b = backward(observations[i], b, states, trans_p, emit_p) 
    # product
    results = {}
    for x in states:
        results[x] = f[x] * b[x]
    # normalization
    s = sum(results.values())
    for k,v in results.items():
        results[k] = v/s
    return results

print "Smoothing", smoothing(observations, 3, states, start_probability, transition_probability, emission_probability)
print "Filtering", filtering(observations[:3], states, start_probability, transition_probability, emission_probability)
実行結果は
Smoothing {'Rainy': 0.85679684480228568, 'Sunny': 0.14320315519771437}
Filtering {'Rainy': 0.86342066067036916, 'Sunny': 0.1365793393296309}
['walk', 'shop', 'clean', 'shop', 'walk']という観測列があったときの3日目(cleanをした日)のスムージングの結果とフィルタリングの結果を表している.フィルタリングでは4日目にshopして5日目にwalkしたという情報も使っているので若干Rainyの確率が下がっている.

ちなみに,4日目も5日目もcleanだった場合(観測列が['walk', 'shop', 'clean', 'clean', 'clean'])の3日目の推定結果は
Smoothing {'Rainy': 0.90669272213214924, 'Sunny': 0.093307277867850785}
Filtering {'Rainy': 0.86342066067036916, 'Sunny': 0.1365793393296309}
となり雨の確率が上がる.

4日目も5日目もwalkだった場合は(観測列が['walk', 'shop', 'clean', 'walk', 'walk'])の3日目の推定結果は
Smoothing {'Rainy': 0.78605072114831254, 'Sunny': 0.21394927885168744}
Filtering {'Rainy': 0.86342066067036916, 'Sunny': 0.1365793393296309}
となり雨の確率は下がる.

July 27, 2010

Pythonで隠れマルコフモデルのFilteringの例

http://en.wikipedia.org/wiki/Viterbi_algorithm
の天気の例で使えるFiltering。

隠れマルコフモデルの例
隠れマルコフモデルの例 その2

フィルタリング
  • 現在までのすべての観測があるときに,現在の状態を推定する.
  • i.e. P(Xt|Y0:t)を求める.
P(Xt|Y0:t)をガリガリ式変形する(マルコフ性,ベイズ,周辺化でごにょごにょする)と,
が得られる.P(Xt|Xt-1)は遷移確率.P(Yt|Xt)は出力確率.cは正規化項.で,再帰的にP(Xt|Y0:t)を求める.以下実装.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 状態変数
states = ('Rainy', 'Sunny')

# 観測列
observations = ['walk', 'shop', 'clean']

# 初期確率
start_probability = {'Rainy': 0.6, 'Sunny': 0.4}
 
# 遷移確率
transition_probability = {
   'Rainy' : {'Rainy': 0.7, 'Sunny': 0.3},
   'Sunny' : {'Rainy': 0.4, 'Sunny': 0.6},
   }

# 出力確率 
emission_probability = {
   'Rainy' : {'walk': 0.1, 'shop': 0.4, 'clean': 0.5},
   'Sunny' : {'walk': 0.6, 'shop': 0.3, 'clean': 0.1},
   }

# Forward Algorithm
def forward(y1, prior, states, trans_p, emit_p):
    post = {}
    for x1 in states:
        post[x1] = emit_p[x1][y1] * sum([ trans_p[x][x1] * prior[x] for x in states ])
    s = sum(post.values())
    for k,v in post.items():
        post[k] = v/s
    return post

# Filtering
def filtering(observations, states, start_p, trans_p, emit_p):
    prior = start_p
    T = len(observations)
    for t in range(T):
        post = forward( observations[t], prior, states, trans_p, emit_p )
        prior = post
    return post

print filtering(observations, states, start_probability, transition_probability, emission_probability)

上記の実行結果は

{'Rainy': 0.86342066067036916, 'Sunny': 0.1365793393296309}

つまり, walk→shop→clean という観測列があったら3日目の天気は雨が86%,晴れが14%だよっ,ということ.