Your SlideShare is downloading. ×
0
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 : ...
しかし関数のarrayは……In : fs = np.array([lambda x: x+1,                    lambda x: x+2,                    lambda x: x+3])In : ...
現時点では……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)--...
numpy.ndarrayをcallableに         すればいいらしい!In : fs = np.array([lambda x: x+1,                    lambda x: x+2,             ...
してみた!class _callableArray(np.ndarray):    def __call__(self, *arg):        return np.array([f(*arg) for f in self])def car...
これで勝つる!In : fs = carray([lambda x: x+1,                  lambda x: x+2,                  lambda x: x+3])In : fs(1)Out: arr...
予想外の効果も!In : xs = np.array([10,20,30])In : fs(xs)Out:array([[11, 21, 31],       [12, 22, 32],       [13, 23, 33]])outer pr...
予想外の効果も!fs = [f+1, f+2, f+3] # callable arrayxs = [10, 20, 30] # numpy.ndarrayfs(xs) = [f+1([10,20,30]), # [11,21,31]     ...
(例1)計画行列     phi0(x1) phi1(x1) ... phiM-1(x1)Φ=   phi0(x2) phi1(x2) ... phiM-1(x2)             ...     phi0(xN) phi1(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,...
まとめ●   callableなndarrayを実装しました●   表記が単純に●   生成コスト: 生成時に余分なndarrayオブジェクトをひとつ    生成する●   呼び出しコスト: __call__メソッドの呼び出し●   安全性チェ...
Upcoming SlideShare
Loading in...5
×

Tokyo scipy2

1,221

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,221
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Tokyo scipy2"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×