我有一个Numpy Array,其元素用单引号引起来,我想将dtype转换为浮点型。
array =
[['20150101' '0.12']
['20150102' '0.42']
['20150103' '0.12']
['20150104' '0.46']
['20150105' '0.14']
['20150106' '0.1']
['20150107' '0.27']
['20150108' '0.03']
['20150109' '0.16']
['20150110' '0.29']
['20150111' '0.32']
['20150112' '0.16']]
我尝试过:
values = array.item().split(' ')
new_array = np.asarray(values, dtype='float')
但是我得到了ValueError:只能将大小为1的数组转换为Python标量。我希望输出看起来像这样:(没有单引号)
new_array =
[[20150101 0.12]
[20150102 0.42]
[20150103 0.12]
[20150104 0.46]
[20150105 0.14]
[20150106 0.10]
[20150107 0.27]
[20150108 0.03]
[20150109 0.16]
[20150110 0.29]
[20150111 0.32]
[20150112 0.16]]
有一个numpy函数可以让我删除单引号吗?
答案 0 :(得分:2)
您显示的是带有字符串dtype
的2d数组,我可以使用它来重新创建:
In [420]: arr = np.array([['20150101', '0.12'],
...: ['20150102', '0.42'],
...: ['20150103', '0.12'],
...: ['20150104', '0.46']])
In [421]:
In [421]: arr
Out[421]:
array([['20150101', '0.12'], # the repr display
['20150102', '0.42'],
['20150103', '0.12'],
['20150104', '0.46']], dtype='<U8')
In [422]: print(arr) # the str display
[['20150101' '0.12']
['20150102' '0.42']
['20150103' '0.12']
['20150104' '0.46']]
引号反映了数组的基本性质;它们不仅仅是数组的偶然部分。
转换为float dtype数组:
In [423]: arr.astype(float)
Out[423]:
array([[2.0150101e+07, 1.2000000e-01],
[2.0150102e+07, 4.2000000e-01],
[2.0150103e+07, 1.2000000e-01],
[2.0150104e+07, 4.6000000e-01]])
广泛的价值观要求科学的记号。第一列本身显示为:
In [424]: _[:,0]
Out[424]: array([20150101., 20150102., 20150103., 20150104.])
我可以混合使用整数和浮点数:
In [426]: arr1 = np.zeros((4,), dtype='i,f')
In [427]: arr1
Out[427]:
array([(0, 0.), (0, 0.), (0, 0.), (0, 0.)],
dtype=[('f0', '<i4'), ('f1', '<f4')])
In [428]: arr1['f0'] = arr[:,0]
In [429]: arr1['f1'] = arr[:,1]
In [430]: arr1
Out[430]:
array([(20150101, 0.12), (20150102, 0.42), (20150103, 0.12),
(20150104, 0.46)], dtype=[('f0', '<i4'), ('f1', '<f4')])
这是一维结构化数组。请注意符号上的差异,包括()的使用。