NumpyとPDLで行列の積の動きが違うようだけど・・・
Numpy
In [17]: train_x = np.array([[2,1],[4,1],[9,1]]) In [18]: train_y = [5,9,19] In [19]: np.linalg.inv(train_x.T.dot(train_x)).dot(train_x.T).dot(train_y) Out[19]: array([ 2., 1.])
PDL
pdl> $train_x = pdl[[2,1],[4,1],[9,1]] pdl> $train_y = [5,9,19] pdl> inv( $train_x->transpose x $train_x) x ($train_x->transpose) x $train_y Dim mismatch in matmult of [3x2] x [3x1]: 3 != 1 at /usr/lib/perl5/site_perl/5.14/i686-cygwin-threads-64int/PDL/Primitive.pm line 265, <DATA> line 207. PDL::matmult('PDL=SCALAR(0x808d00a0)', 'ARRAY(0x808cfe00)', 'PDL=SCALAR(0x808cfab8)') called at Basic/Core/Core.pm.PL (i.e. PDL::Core.pm) line 863 PDL::__ANON__('PDL=SCALAR(0x808d00a0)', 'ARRAY(0x808cfe00)', '') called at (eval 76) line 4 main::__ANON__() called at /usr/local/bin/perldl line 724 eval {...} called at /usr/local/bin/perldl line 724 main::eval_and_report('inv( $train_x->transpose x $train_x) x ($train_x->transpose) ...') called at /usr/local/bin/perldl line 660 main::process_input() called at /usr/local/bin/perldl line 680 eval {...} called at /usr/local/bin/perldl line 680 pdl>
array([[-0.11538462, -0.03846154, 0.15384615],
[ 0.91025641, 0.52564103, -0.43589744]])
と
[5,9,19]
の積がPDLでは計算できないけれど、Numpyだとできる
ということなんだろうか。
行列同士の積って、行列の数の制約みたいなものがあるはずで、
PDLの実装が厳密ということなのかな??
うーむ。Numpyでは行列どおしの積と認識していないと捉えるのが
自然のような気がするし、
うーん。わからないなぁ。(数学力弱い・・)
でもって、PDLのほうを以下のように変えるとどちらも
同じ結果
pdl> $train_x = pdl[[2,1],[4,1],[9,1]] pdl> $train_y = [[5],[9],[19]] pdl> print inv( $train_x->transpose x $train_x) x ($train_x->transpose) x $train_y [ [2] [1] ]
In [27]: train_x = np.array([[2,1],[4,1],[9,1]]) In [28]: train_y = np.array([[5],[9],[19]]) In [29]: np.linalg.inv(train_x.T.dot(train_x)).dot(train_x.T).dot(train_y) Out[29]: array([[ 2.], [ 1.]])
Numpyが気を利かせてくれているのだろうというよくわからない推論。