[英]convert a 2D numpy matrix to 2D numpy array
Certain functions in Numpy return a 2d matrix as output. Numpy中的某些函数返回2d矩阵作为输出。 but I want them to be in 2d array form. 但我希望它们采用二维数组形式。 What is the most efficient (memory and cpu) way to convert a 2d matrix to a 2d array? 将2d矩阵转换为2d数组的最有效方法(内存和cpu)是什么?
Note that a numpy.matrix
is already an ndarray
subclass, and nothing more than a specialized 2D array. 注意, numpy.matrix
已经是ndarray
子类,仅是专门的2D数组。 Hence you're most likely quite alright without converting your matrix to an explicit numpy.array
unless you have a particular reason to do so, perhaps the additional generality of a Numpy array. 因此,除非有特殊原因,否则您很可能无需将矩阵转换为显式的numpy.array
,这numpy.array
,也许还有Numpy数组的其他一般性。
Should this be the case, you can convert your matrix to an array with numpy.asarray()
. 在这种情况下,您可以使用numpy.asarray()
将矩阵转换为数组。 It's important you use this method and not numpy.asanyarray()
in your case as with numpy.asanyarray()
allows subclasses of ndarray
to pass through, as your matrix would. 使用此方法,而不是它的重要numpy.asanyarray()
在你的情况与numpy.asanyarray()
允许的子类ndarray
通过,作为您的矩阵会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.