1

我正在尝试将由字符串组成的数组转换为具有浮点值和字符串的数组。我的代码目前如下所示:

datatype1=np.dtype([
('LOCATION_THETA',np.float64),
('LOCATION_PHI',np.float64),
('ETHETA_MAGN',np.float64),
('ETHETA_PHASE',np.float64),
('EPHI_MAGN',np.float64),
('EPHI_PHASE',np.float64),
('DIRECTIVITY_VERT',np.float64),
('DIRECTIVITY_HORIZ',np.float64),
('DIRECTIVITY_TOTAL',np.float64),
('POLARISATION_AXIALR',np.float64),
('POLARISATION_ANGLE',np.float64),
('POLARISATION_DIRECTION','|S5')])

table2=np.array(table,dtype=datatype1)

table一个numpy的字符串数组)看起来像这样:

[['0.00' '0.00' '5.751E-01' '-2.08' '9.532E-05' '-86.19' '1.7442' '-73.8670' '1.7442' '0.0002' '0.00' 'RIGHT']
 ['2.00' '0.00' '5.747E-01' '-2.11' '1.291E-04' '-82.47' '1.7390' '-71.2312' '1.7390' '0.0002' '0.00' 'RIGHT']
 ['4.00' '0.00' '5.738E-01' '-2.21' '1.632E-04' '-80.31' '1.7243' '-69.1973' '1.7243' '0.0003' '0.00' 'RIGHT']
 ['6.00' '0.00' '5.722E-01' '-2.38' '1.973E-04' '-78.94' '1.7001' '-67.5479' '1.7001' '0.0003' '0.00' 'RIGHT']
 ['8.00' '0.00' '5.699E-01' '-2.61' '2.314E-04' '-78.02' '1.6663' '-66.1644' '1.6663' '0.0004' '0.01' 'RIGHT']
...

但是,当我执行脚本时,出现以下错误:

ValueError: could not convert string to float: RIGHT

它不应该这样做,因为我希望字符串是|S5,而不是float......

在此先感谢您的帮助!

4

1 回答 1

1

这里发生的是,当你这样做时:

ts = np.array(t, dtype=dt)

dtype 被应用于table. 它适用于前 11 个元素,然后它得到'RIGHT'它不能变成一个整数。这是没有它的情况'RIGHT'(这会很混乱!):

>>> t[:2,:-1]
array([['0.00', '0.00', '5.751E-01', '-2.08', '9.532E-05', '-86.19', '1.7442', '-73.8670', '1.7442', '0.0002', '0.00'],
       ['2.00', '0.00', '5.747E-01', '-2.11', '1.291E-04', '-82.47', '1.7390', '-71.2312', '1.7390', '0.0002', '0.00']], 
      dtype='|S9')

>>> np.array(t[:2,:-1], dt)
array([[(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00'),
        (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00'),
        (0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, '5.751'),
        (-2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, '-2.08'),
        (9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, '9.532'),
        (-86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, '-86.1'),
        (1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, '1.744'),
        (-73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, '-73.8'),
        (1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, '1.744'),
        (0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, '0.000'),
        (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00')],
       [(2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, '2.00'),
        (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00'),
        (0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, '5.747'),
        (-2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, '-2.11'),
        (0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, '1.291'),
        (-82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, '-82.4'),
        (1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, '1.739'),
        (-71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, '-71.2'),
        (1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, '1.739'),
        (0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, '0.000'),
        (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00')]], 
      dtype=[('LOCATION_THETA', '<f8'), ('LOCATION_PHI', '<f8'), ('ETHETA_MAGN', '<f8'), ('ETHETA_PHASE', '<f8'), ('EPHI_MAGN', '<f8'), ('EPHI_PHASE', '<f8'), ('DIRECTIVITY_VERT', '<f8'), ('DIRECTIVITY_HORIZ', '<f8'), ('DIRECTIVITY_TOTAL', '<f8'), ('POLARISATION_AXIALR', '<f8'), ('POLARISATION_ANGLE', '<f8'), ('POLARISATION_DIRECTION', 'S5')])

因此,您可以看到,对于每个元素,您都会得到一个带有 dtype 的漂亮的小元组('record')datatype1(它甚至使最后一个为您的字符串)。

有几种方法可以解决这个问题,最好的方法是从一开始就使用正确的 dtype 创建/导入数组,这样您就不必复制它。对于某些转换,可以在view其中简单地解释数据,就好像它具有新的复杂 dtype 一样,但这不会将字符串转换为数字,因为这比假装数据是数字更复杂。

在您的情况下,您可能应该使用比常规结构化数组recarray稍微复杂的 a ,然后您可以使用该函数。它需要一个列列表,每个列都有统一的类型,而不是行,因此转置:fromarrays

>>> np.rec.fromarrays(t.T, dt)
rec.array([ (0.0, 0.0, 0.5751, -2.08, 9.532e-05, -86.19, 1.7442, -73.867, 1.7442, 0.0002, 0.0, 'RIGHT'),
       (2.0, 0.0, 0.5747, -2.11, 0.0001291, -82.47, 1.739, -71.2312, 1.739, 0.0002, 0.0, 'RIGHT'),
       (4.0, 0.0, 0.5738, -2.21, 0.0001632, -80.31, 1.7243, -69.1973, 1.7243, 0.0003, 0.0, 'RIGHT'),
       (6.0, 0.0, 0.5722, -2.38, 0.0001973, -78.94, 1.7001, -67.5479, 1.7001, 0.0003, 0.0, 'RIGHT'),
       (8.0, 0.0, 0.5699, -2.61, 0.0002314, -78.02, 1.6663, -66.1644, 1.6663, 0.0004, 0.01, 'RIGHT')], 
      dtype=[('LOCATION_THETA', '<f8'), ('LOCATION_PHI', '<f8'), ('ETHETA_MAGN', '<f8'), ('ETHETA_PHASE', '<f8'), ('EPHI_MAGN', '<f8'), ('EPHI_PHASE', '<f8'), ('DIRECTIVITY_VERT', '<f8'), ('DIRECTIVITY_HORIZ', '<f8'), ('DIRECTIVITY_TOTAL', '<f8'), ('POLARISATION_AXIALR', '<f8'), ('POLARISATION_ANGLE', '<f8'), ('POLARISATION_DIRECTION', 'S5')])

迷人的!但是等等,现在是这个rec.array......如果你想保持这种状态,那很好。如果您希望它是一个常规的结构化数组,请执行以下操作:

>>> np.asarray(np.rec.fromarrays(t.T, dt))
于 2013-09-25T15:06:46.997 回答