Tokyo scipy2
Upcoming SlideShare
Loading in...5
×
 

Tokyo scipy2

on

  • 1,350 views

 

Statistics

Views

Total Views
1,350
Views on SlideShare
1,293
Embed Views
57

Actions

Likes
0
Downloads
14
Comments
0

3 Embeds 57

http://a0.twimg.com 39
http://paper.li 17
http://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Tokyo scipy2 Tokyo scipy2 Presentation Transcript

  • Callable ArrayTokyo.SciPy #2Lightning Talk 11/10/15 id:(t)yatsuta
  • universal functionは便利!In : def f(x): return x+1 ....:In : xs = np.array([1,2,3])In : f(xs)Out: array([2, 3, 4])
  • しかし関数のarrayは……In : fs = np.array([lambda x: x+1, lambda x: x+2, lambda x: x+3])In : fs(1)-------------------------------TypeErrorTraceback (most recent call last)...TypeError: numpy.ndarray object is notcallable
  • しかし関数のarrayは……In : fs = np.array([lambda x: x+1, lambda x: x+2, lambda x: x+3])In : fs(1)Out: array([2, 3, 4])とできれば、かなり嬉しい!
  • 現時点では……In : np.array([f(1) for f in fs])Out: array([2, 3, 4])np.array関数 + リストの内包表記……
  • よく見ると……In : fs = np.array([lambda x: x+1, lambda x: x+2, lambda x: x+3])In : fs(1)-------------------------------TypeErrorTraceback (most recent call last)...TypeError: numpy.ndarray object is notcallable
  • numpy.ndarrayをcallableに すればいいらしい!In : fs = np.array([lambda x: x+1, lambda x: x+2, lambda x: x+3])In : fs(1)-------------------------------TypeErrorTraceback (most recent call last)...TypeError: numpy.ndarray object is notcallable
  • してみた!class _callableArray(np.ndarray): def __call__(self, *arg): return np.array([f(*arg) for f in self])def carray(funcs): buf = np.array(funcs) return _callableArray(shape=buf.shape, dtype=buf.dtype, buffer=buf)
  • これで勝つる!In : fs = carray([lambda x: x+1, lambda x: x+2, lambda x: x+3])In : fs(1)Out: array([2, 3, 4])
  • 予想外の効果も!In : xs = np.array([10,20,30])In : fs(xs)Out:array([[11, 21, 31], [12, 22, 32], [13, 23, 33]])outer productの関数適用版
  • 予想外の効果も!fs = [f+1, f+2, f+3] # callable arrayxs = [10, 20, 30] # numpy.ndarrayfs(xs) = [f+1([10,20,30]), # [11,21,31] f+2([10,20,30]), # [12,22,32] f+3([10,20,30])] # [13,23,33] # numpy.ndarray(2d)
  • (例1)計画行列 phi0(x1) phi1(x1) ... phiM-1(x1)Φ= phi0(x2) phi1(x2) ... phiM-1(x2) ... phi0(xN) phi1(xN) ... phiM-1(xN)
  • (例1)計画行列def design_matrix(phis, xs): return np.transpose(phis(xs))これだけ!
  • (例2)matplotlib用データ w_phis: M個の重み付きガウス基底関数 reg_func: 回帰関数(w_phisの総和) plot_xs: 0から1まで101点のlinspace
  • (例2)matplotlib用データIn : w_phis_ys = w_phis(plot_xs)In : plot(plot_xs, w_phis_ys.T, ...)In : reg_func_ys = np.sum(w_phis_ys, axis=0)など……現在鋭意コーディング中……
  • まとめ● callableなndarrayを実装しました● 表記が単純に● 生成コスト: 生成時に余分なndarrayオブジェクトをひとつ 生成する● 呼び出しコスト: __call__メソッドの呼び出し● 安全性チェック皆無:中身が関数であるかどうかはユーザ の責任● 所詮はリスト内包表記+array関数のラッパ● 多分もっといい実装方法はあるはず……● メリット・デメリットを見極めてお使いください