我试图理解Numpy中dtypes的逻辑.
numpy.min_scalar_type(10)
-> uint8
和:
a = numpy.array([10])
print(a.dtype)
-> int32 (on my machine)
我期待uint8而不是int32,因为(1.9)doc说:
numpy.array(object, dtype=None, …)
dtype : data-type, optional. The desired data-type for the array. If
not given, then the type will be determined as the minimum type
required to hold the objects in the sequence
和:
numpy.min_scalar_type(a)
For scalar a, returns the data type with the smallest size and
smallest scalar kind which can hold its value.
…
Returns: out : dtype. The minimal data type.
我误解了什么吗?
最佳答案 python列表可以包含不同类型的对象,例如X = [‘apples’,’oranges’,10].如果你输入([10]),你会发现容器的Python类型在技术上被称为列表,而不是数组.
相反,在numpy数组中,所有对象都是相同类型的dtype.
文档告诉您,在创建numpy数组时,dtype设置为将包含所有现有对象的类型.</