numpy.matrix.A1 和 ravel 的区别 [英] Difference between numpy.matrix.A1 and ravel
问题描述
据我所知,我还没有看到任何帖子对此进行评分,如果有的话,请注意我.
To my knowledge, I haven't seen any posts regrading this, if any, please note me.
根据 SciPy 网站,numpy.matrix.A1
等价于 np.asarray(x).ravel()
.
一个例子就足以说明问题:
One example will be enough to illustrate the problem:
x = np.matrix(np.arange(12).reshape((1, -1)))
print("Shape of x: ", x.shape)
print("Shape of x with asarray: ", np.asarray(x).shape)
print("Equality: ", np.array_equal(x, np.asarray(x)))
print("Shape of x ravel flatten: ", x.ravel().shape)
print("Shape of x ravel flatten with asarray: ", np.asarray(x).ravel().shape)
打印:
Shape of x: (1, 12)
Shape of x with asarray: (1, 12)
Equality: True
Shape of x ravel flatten: (1, 12)
Shape of x ravel flatten with asarray: (12,)
问题:
正如观察到的,扁平化数组的维度与asarray
不同,只是想知道为什么它会呈现这种维度不一致?
As observed, the dimension of flattened array is different with asarray
, just wondered why it's being presenting such inconsistencies in dimensions?
从asarray
函数的np
实现来看,我没有看到任何可能导致维度问题的东西,而且它通过了相等测试(x ==np.asarray(x)
).但除此之外,还有什么可能对数组进行隐式更改.
From np
implementation of asarray
function, I didn't see any thing might cause a dimension problem, plus it passes the equality test (x == np.asarray(x)
). But other than this, what could be possibly making implicit changes to the array.
def asarray(a, dtype=None, order=None):
return array(a, dtype, copy=False, order=order)
已
这可能会令人困惑
加上它通过了相等性测试 (x == np.asarray(x)
)
plus it passes the equality test (
x == np.asarray(x)
)
更准确地说,我的意思是,它通过了相等性测试 (np.array_equal(x, np.asarray(x))
)
to be more precise, I mean, it passes the equality test (np.array_equal(x, np.asarray(x))
)
推荐答案
NumPy 矩阵 (np.matrix
) 总是 2D.(数学上,严格定义矩阵,而不是矩阵或向量.)
NumPy matrices (np.matrix
) are always 2D. (Mathematically, the strict definition of a matrix, rather than a matrix or vector.)
来自np.matrix.ravel
:
返回展平后的矩阵 (1, N)
其中 N
是数字原始矩阵中的元素.
Return the matrix flattened to shape
(1, N)
whereN
is the number of elements in the original matrix.
NumPy 矩阵的一些动机是为了 Matlab 用户.请参阅此处了解一些NumPy matrix
与 array
上的更好点.
Some motivation for NumPy matrices is for Matlab users. See here for some of the finer points on NumPy matrix
versus array
.
简而言之,一个 NumPy 数组(这里是 asarray(x)
的结果)可以是一维结构.矩阵最小可以是 2d.type(np.asarray(x))
毫不奇怪,是一个数组.(不要与 np.asanyarray()
,你的结果将是一个矩阵,因为它是一个数组子类.
In brief, a NumPy array (the result of asarray(x)
here) can be a 1-dimensional structure. Matrices can be a minimum of 2d. type(np.asarray(x))
is, not shockingly, an array. (Not to be confused with np.asanyarray()
, for which your result would be a matrix because it's an array subclass.
最后,您注意到:
它通过了相等性测试 (x == np.asarray(x)
)
it passes the equality test (
x == np.asarray(x)
)
我明白这可能有点令人困惑.从技术上讲,您希望使用 np.array_equal(x, np.asarray(x))
,尽管它的计算结果仍为 True
.然而,NumPy 逻辑测试通常意味着数据结构不可知,一般来说:
I see how this could be a bit confusing. Technically, you want to use np.array_equal(x, np.asarray(x))
, although that still evaluates to True
. However, NumPy logic testing is generally meant to be data-structure agnostic, in general:
np.array_equal([1, 2, 3], np.array([1, 2, 3]))
# True
(这是 来源code>array_equal()——两者都被转换为数组.)
(Here is the source for array_equal()
--both are cast to arrays.)
底线是它们的最小维度"不同,一个是另一个的子类.
The bottom line is that their "minimum dimensionalities" are different, and one is a subclass of the other.
issubclass(np.matrix, np.ndarray)
# True
这篇关于numpy.matrix.A1 和 ravel 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!