- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我读了Docs , 但仍然不太了解 item
的区别和用例。
但最近我发现只有 item
起作用的地方:
a = np.array(100) # a has shape ()!
a.item() # or a.item(0)
这是从 a
中获取值的唯一方法,a[0]
不起作用。
最佳答案
ndarray.item
允许您使用平面索引来解释数组,而不是使用 []
表示法。这允许你做这样的事情:
import numpy as np
a = np.arange(16).reshape((4,4))
print(a)
#[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]
print(a[2,2]) # "Normal" indexing, taking the 3rd element from the 3rd row
# 10
print(a.item(12)) # Take the 12th element from the array, equal to [3,0]
# 12
它还允许您轻松传递索引元组,如下所示:
print(a.item((1,1))) # Equivalent to a[1,1]
# 5
最后,正如您在问题中提到的,这是一种将 size = 1
的数组元素作为 Python 标量获取的方法。请注意,这与 numpy
标量不同,例如如果 a = np.array([1.0], dtype=np.float32)
那么 type(a [0]) != 类型(a.item(0))
。
b = np.array(3.14159)
print(b, type(b))
# 3.14159 <class 'numpy.ndarray'>
print(b.item(), type(b.item()))
# 3.14159 <class 'float'>
关于python - ndarray.item(arg) 和 ndarry[arg] 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192810/
我对 python numpy 很陌生。 如果我确实有一个 numpy 向量列表。确保计算速度快的最佳方法是什么。 我目前正在这样做,但我发现它太慢了。 vec = sum(list of numpy
我有一个 466 x 700 numpy.ndarry。对于 466 x 700 ndarray 中的每个 ndarray,我想按索引删除一个元素。到目前为止是这样的: normalized_img
我正在尝试使用 cv2 将 20 张图像转换为 ndarry,然后使用 ndarry 训练 CNN 模型 (mxnet)。首先,我将具有各种像素的彩色图像转换为灰色和 128 x 128 像素图像。所
我读了Docs , 但仍然不太了解 item 的区别和用例。 但最近我发现只有 item 起作用的地方: a = np.array(100) # a has shape ()! a.item() #
我是一名优秀的程序员,十分优秀!