12

我必须将一个 numpy 浮点数组转换为一个字符串(存储在 SQL DB 中),然后还将相同的字符串转换回一个 numpy 浮点数组。

这就是我要去字符串的方式(基于这篇文章

VIstring = ''.join(['%.5f,' % num for num in VI])
VIstring= VIstring[:-1] #Get rid of the last comma

所以首先这确实有效,这是一个好方法吗?他们是摆脱最后一个逗号的更好方法吗?或者我可以获得join为我插入逗号的方法吗?

其次,更重要的是,有没有一种巧妙的方法可以从字符串返回到浮点数组?

这是数组和字符串的示例:

VI
array([ 17.95024446,  17.51670904,  17.08894626,  16.66695611,
        16.25073861,  15.84029374,  15.4356215 ,  15.0367219 ,
        14.64359494,  14.25624062,  13.87465893,  13.49884988,
        13.12881346,  12.76454968,  12.40605854,  12.00293814,
        11.96379322,  11.96272486,  11.96142533,  11.96010489,
        11.95881595,  12.26924591,  12.67548634,  13.08158864,
        13.4877041 ,  13.87701221,  14.40238245,  14.94943786,
        15.49364166,  16.03681428,  16.5498035 ,  16.78362298,
        16.90331119,  17.02299387,  17.12193689,  17.09448654,
        17.00066063,  16.9300633 ,  16.97229868,  17.2169709 ,  17.75368411])

VIstring
'17.95024,17.51671,17.08895,16.66696,16.25074,15.84029,15.43562,15.03672,14.64359,14.25624,13.87466,13.49885,13.12881,12.76455,12.40606,12.00294,11.96379,11.96272,11.96143,11.96010,11.95882,12.26925,12.67549,13.08159,13.48770,13.87701,14.40238,14.94944,15.49364,16.03681,16.54980,16.78362,16.90331,17.02299,17.12194,17.09449,17.00066,16.93006,16.97230,17.21697,17.75368'

哦,是的,精度损失%.5f完全没问题,这些值是由原始点插值的,只有小数点后 4 位精度,所以我不需要打败它。所以当恢复 numpy 数组时,我很高兴只得到 5 位小数精度(显然我想)

4

3 回答 3

23

首先,您应该使用join这种方式来避免最后一个逗号问题:

VIstring = ','.join(['%.5f' % num for num in VI])

然后读回来,使用numpy.fromstring

np.fromstring(VIstring, sep=',')
于 2013-05-10T13:13:41.143 回答
8
>>> import numpy  as np
>>> from cStringIO import StringIO
>>> VI = np.array([ 17.95024446,  17.51670904,  17.08894626,  16.66695611,
        16.25073861,  15.84029374,  15.4356215 ,  15.0367219 ,
        14.64359494,  14.25624062,  13.87465893,  13.49884988,
        13.12881346,  12.76454968,  12.40605854,  12.00293814,
        11.96379322,  11.96272486,  11.96142533,  11.96010489,
        11.95881595,  12.26924591,  12.67548634,  13.08158864,
        13.4877041 ,  13.87701221,  14.40238245,  14.94943786,
        15.49364166,  16.03681428,  16.5498035 ,  16.78362298,
        16.90331119,  17.02299387,  17.12193689,  17.09448654,
        17.00066063,  16.9300633 ,  16.97229868,  17.2169709 ,  17.75368411])
>>> s = StringIO()
>>> np.savetxt(s, VI, fmt='%.5f', newline=",")
>>> s.getvalue()
'17.95024,17.51671,17.08895,16.66696,16.25074,15.84029,15.43562,15.03672,14.64359,14.25624,13.87466,13.49885,13.12881,12.76455,12.40606,12.00294,11.96379,11.96272,11.96143,11.96010,11.95882,12.26925,12.67549,13.08159,13.48770,13.87701,14.40238,14.94944,15.49364,16.03681,16.54980,16.78362,16.90331,17.02299,17.12194,17.09449,17.00066,16.93006,16.97230,17.21697,17.75368,'
>>> np.fromstring(s.getvalue(), sep=',')
array([ 17.95024,  17.51671,  17.08895,  16.66696,  16.25074,  15.84029,
        15.43562,  15.03672,  14.64359,  14.25624,  13.87466,  13.49885,
        13.12881,  12.76455,  12.40606,  12.00294,  11.96379,  11.96272,
        11.96143,  11.9601 ,  11.95882,  12.26925,  12.67549,  13.08159,
        13.4877 ,  13.87701,  14.40238,  14.94944,  15.49364,  16.03681,
        16.5498 ,  16.78362,  16.90331,  17.02299,  17.12194,  17.09449,
        17.00066,  16.93006,  16.9723 ,  17.21697,  17.75368])
于 2013-05-10T13:20:29.153 回答
5

如果你想要一些字符串表示(不一定是 CSV),你可以试试这个,我一直在使用:

import numpy, json

## arr is some numpy.ndarray
s = json.dumps(arr.tolist())
arrback = numpy.array(json.loads(s))

它适用于最常见的数据类型。

于 2013-05-10T13:31:12.410 回答