numpy.minimum()的返回类型?

发布于 2024-10-30 20:34:44 字数 683 浏览 5 评论 0原文

当我在布尔数组上使用 numpy 函数 minimum()maximum() 时,结果的类型打印为 numpy。 int32。但是,与 numpy.int32 类型的比较失败(即使在强制转换之后)。这是一个错误吗?

g = np.ones((5, 5), dtype = np.bool)
h = np.maximum(g, 4)
i = np.int32(h)

print 'type of g ', g.dtype.type   # prints <type 'numpy.bool_'>
print 'type of h ', h.dtype.type   # prints <type 'numpy.int32'>
print 'type of i ', i.dtype.type   # prints <type 'numpy.int32'>

print h.dtype.type == i.dtype.type # prints True
print h.dtype.type == np.int32     # prints False
print i.dtype.type == np.int32     # prints False
print i.dtype.type == np.bool_     # prints False

When I use the numpy functions minimum() and maximum() on boolean arrays, the type of the result prints as numpy.int32. However, a comparison with the numpy.int32 type fails (even after a cast). Is this a bug?

g = np.ones((5, 5), dtype = np.bool)
h = np.maximum(g, 4)
i = np.int32(h)

print 'type of g ', g.dtype.type   # prints <type 'numpy.bool_'>
print 'type of h ', h.dtype.type   # prints <type 'numpy.int32'>
print 'type of i ', i.dtype.type   # prints <type 'numpy.int32'>

print h.dtype.type == i.dtype.type # prints True
print h.dtype.type == np.int32     # prints False
print i.dtype.type == np.int32     # prints False
print i.dtype.type == np.bool_     # prints False

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最偏执的依靠 2024-11-06 20:34:44

对我来说似乎很正常。使用 np.maximum 返回 g4 的最大值,即 4 (True == 1)。你会得到一个由四组成的矩阵,它是整数类型。

如果您使用此语法进行类型比较,它们将起作用:h.dtype.type == np.dtype(np.int32)或更简单地h.dtype == np.int32< /代码>。

Seems normal enough to me. Using np.maximum returns the maximum of g and 4, which is 4 (True == 1). You get a matrix full of fours, which is integer type.

If you use this syntax for your type comparisons, they work: h.dtype.type == np.dtype(np.int32) or more simply h.dtype == np.int32.

七色彩虹 2024-11-06 20:34:44

返回类型是一个数组:

numpy.minimum(x1, x2[, out])

数组元素的按元素最小值。

比较两个数组并返回一个包含元素最小值的新数组。如果要比较的元素之一是 nan,则返回该元素。如果两个元素都是 nan,则返回第一个元素。后一种区别对于复杂的 nan 很重要,复杂的 nan 被定义为实部或虚部中至少有一个是 nan。最终效果是 nan 被传播。

参数:
x1, x2 : 类似数组
保存要比较的元素的数组。它们必须具有相同的形状,或者可以广播为单个形状的形状。
返回:
y : {ndarray,标量}
x1 和 x2 的最小值(按元素)。如果 x1 和 x2 都是标量,则返回标量。

来自: http://docs .scipy.org/doc/numpy/reference/ generated/numpy.minimum.html?highlight=minimum#numpy.minimum

The return type is an array:

numpy.minimum(x1, x2[, out])

Element-wise minimum of array elements.

Compare two arrays and returns a new array containing the element-wise minima. If one of the elements being compared is a nan, then that element is returned. If both elements are nans then the first is returned. The latter distinction is important for complex nans, which are defined as at least one of the real or imaginary parts being a nan. The net effect is that nans are propagated.

Parameters :
x1, x2 : array_like
The arrays holding the elements to be compared. They must have the same shape, or shapes that can be broadcast to a single shape.
Returns :
y : {ndarray, scalar}
The minimum of x1 and x2, element-wise. Returns scalar if both x1 and x2 are scalars.

From: http://docs.scipy.org/doc/numpy/reference/generated/numpy.minimum.html?highlight=minimum#numpy.minimum

╰◇生如夏花灿烂 2024-11-06 20:34:44

如果您阅读 np.maximum 的文档,您就会知道它比较两个数组,或者在您的情况下比较一个数组和一个标量。因此,对于 g 的每个元素,它将返回 4。

If you read the documentaation for np.maximum, you know it compares two arrays, or in your case an array and a scalar. So it will return 4 for every element of g.

没企图 2024-11-06 20:34:44

问题是 C 中有两种整数类型:intlong,并且它们在 32 位平台上都可以是 32 位。 Numpy 中的当前实现为两者创建了一个单独的标量类型。当两者大小相同时,在 dtype 比较中,两者的处理方式相同(dtypes 是数组内容的描述符——与标量类型不同)。然而,如果直接比较标量类型,就会发现实际上存在两个 32 位整数标量类型。

是的,这有点令人困惑,并且可能会在某个时候得到修复。

但是,在比较 int == np.int32 时,您会遇到同样的问题。正确的方法是比较 dtypes 而不是标量类型。

The issue is that there are two integer types in C: int and long, and they can both be 32-bit on 32-bit platforms. The current implementation in Numpy creates a separate scalar type for both. The two are treated the same in dtype comparisons when they are the same size (dtypes are descriptors for the contents of the arrays -- separate from scalar types). Nevertheless, the fact that there are actually two 32-bit integer scalar types shows up if you go comparing the scalar types directly.

Yes, it's a bit confusing, and will probably be fixed at some point.

However, you will run into the same issue when comparing int == np.int32. The correct way is to compare dtypes rather than scalar types.

眼眸印温柔 2024-11-06 20:34:44

这很奇怪......我认为发生了一些奇怪的事情。
我确实注意到

h.dtype == np.int32 # True

This is strange.. I think there is something odd going on.
I do notice though that

h.dtype == np.int32 # True

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文