LoginSignup
6
5

More than 5 years have passed since last update.

numpyのmatrixとmatは違うものなんですね

Posted at

この間numpyを使っていてハマったのですが、numpyのmatrixmatは違うものみたいです。本家のドキュメントを見てみるとmatmatrix(data, copy=False)と等価というふうに書いていて、コピーされないようになっているみたいです。これはどういうことかというと、

>>> import numpy as np
>>> m = np.matrix([[1.0, 2.0],[3.0, 4.0]])
>>> m
matrix([[ 1.,  2.],
        [ 3.,  4.]])
>>> a = np.matrix(m)
>>> a[0, 0] = 5.0
>>> a
matrix([[ 5.,  2.],
        [ 3.,  4.]])
>>> m
matrix([[ 1.,  2.],
        [ 3.,  4.]])
>>> #aを変更してもmは変わらない
>>> 
>>> b = np.mat(m)
>>> b[0, 0] = 6.0
>>> b
matrix([[ 6.,  2.],
        [ 3.,  4.]])
>>> m
matrix([[ 6.,  2.],
        [ 3.,  4.]])
>>> #bを変更するとmも変わる

ちょっとハマったので、書いてみました。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5