【发布时间】:2014-01-21 07:11:30
【问题描述】:
当使用 astype 将 np.array 转换为 uint8 时,数组元素的类型不会改变。
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x.astype(uint8)
array([[1, 2],
[1, 2]], dtype=uint8)
>>> type(x[0,0])
<type 'numpy.float64'>
为什么元素仍然是 float64 而不是 uint8?
【问题讨论】:
-
作为一般规则:如果一个方法返回一些东西,那么它不会修改原始对象。如果它没有返回任何东西,那么它确实会修改原始对象。这几乎总是适用于内置对象、标准库和大多数其他库。在您的示例中,很明显
astype正在返回一些东西。
标签: python python-2.7 numpy