【发布时间】:2015-02-26 07:10:22
【问题描述】:
执行此代码:
import numpy as np
py_list = [2013, 8, 0.6552562894775783]
custom_type = np.dtype([
('YEAR',np.uint16),
('DOY', np.uint16),
('REF',np.float16)
])
NumPy_array = np.array(py_list)
NumPy_array_converted = NumPy_array.astype(custom_type)
print 'custom_type is:'
print custom_type
print '---------------------------------------------'
print 'py_list is:'
print py_list
print '---------------------------------------------'
print 'NumPy_array is:'
print NumPy_array
print '---------------------------------------------'
print 'NumPy_array converted to custom_type is:'
print NumPy_array_converted
print '---------------------------------------------'
打印:
custom_type is:
[('YEAR', '<u2'), ('DOY', '<u2'), ('REF', '<f2')]
---------------------------------------------
py_list is:
[2013, 8, 0.6552562894775783]
---------------------------------------------
NumPy_array is:
[ 2.01300000e+03 8.00000000e+00 6.55256289e-01]
---------------------------------------------
NumPy_array converted to custom_type is:
[(2013, 2013, 2013.0) (8, 8, 8.0) (0, 0, 0.6552734375)]
---------------------------------------------
1) 问题是为什么在转换为自定义数据类型后,与未转换的 numpy 数组 NumPy_array 相比,我的数据增加了三倍 NumPy_array_converted?
2) 如何更改custom_type 以获得不三倍数组?
【问题讨论】:
标签: python arrays numpy type-conversion custom-data-type