スポンサーリンク

[numpy] ndarray型とmatrix型の違い

ndarray型とmatrix型の違い

2つの型の違いの代表的な例としては、*による演算方法が異なります。
ndarrayだと要素毎に掛け算、matrixだと内積といった演算になります。
ndarray型で内積を行う場合はdotメソッドがあります。

ndarray型を使うべき

ndarray型を基本的に使うべきです。
理由は、numpyの公式ドキュメントに非推奨であることが記載されているためです。

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

https://numpy.org/doc/stable/reference/generated/numpy.matrix.html

上記の公式ドキュメントを翻訳すると、非推奨であるとともに、将来的に削除されることが記載されています。