从numpy数组中删除换行符

时间:2019-10-10 21:25:28

标签: python numpy

我有一个函数可以计算由多个单词组成的每个名称的平均向量,该函数返回形状为numpy.ndarray的{​​{1}}。产生的向量如下:

(100,)

就像收到numpy数组一样,我正在按以下方式删除换行符:

[ 0.00127441  0.0002633   0.00039622  0.00055501  0.00070984 -0.00089766
 -0.00073814 -0.00224919  0.00233035 -0.00037628  0.00125402 -0.00052623
  0.00114087 -0.00070441 -0.00419099  0.00031204 -0.0002703  -0.00290918
  ...(13 lines)
0.00260704 -0.00000406 -0.00160876  0.00134342]

但是出现以下错误:

temp = ["%.8f" % number for number in name_avg_vector]
temp=re.sub('\s+', ' ', temp)
name_avg_vector= np.array(list(temp))

我也尝试过更改printoptions,但是在存储numpy数组值的文件中继续有换行符:

---> 79     temp=re.sub('\s+', ' ', name_avg_vector)
TypeError: cannot use a string pattern on a bytes-like object

之后,我尝试使用array_repr删除换行符:

import sys
np.set_printoptions(threshold=sys.maxsize)
np.set_printoptions(threshold=np.inf)

,但另存为:

name_avg_vector = np.array_repr(name_avg_vector).replace('\n', '')

如Anoyz在here中所述,转换为列表摆脱了诸如['array([-0.00849786, 0.00113221, -0.00643946, 0.00437448, -0.00740928, 0.00381133, 0.00178376, -0.00065115, -0.00050142, -0.0001178 , 0.00029183, 0.00015484, -0.00001569, 0.0006973 , 0.00051486, 0.00006652, -0.00099618, -0.00049231, 0.0003479 , 0.00135821, 0.00078396, 0.00038927, 0.00040825, -0.00093267, 0.00025755, -0.00012063, -0.00074733, 0.00120466, 0.00041425, -0.00062592, 0.00098112, 0.00101578, -0.00048335, 0.00079251, -0.00112981, ... -0.00050014, 0.00133685, -0.00020537, -0.00082505])'] 之类的换行符。

谢谢

1 个答案:

答案 0 :(得分:0)

您的numpy数组似乎具有dtype bash-3.2$ /usr/local/bin/npm run-script foo > es-console-spa@0.0.0 foo /source/es-console-spa > node --nolazy --inspect-brk=9229 foo.js Debugger listening on ws://127.0.0.1:9229/03df431d-f859-405d-a63f-3cf86ceacb56 For help, see: https://nodejs.org/en/docs/inspector Debugger attached. foo Waiting for the debugger to disconnect... Killed: 9 bash-3.2$ ,因此它实际上不包含任何新行。我假设您在执行float之类的操作时会看到换行符。解决问题的一种方法是编写自己的循环以所需的格式打印值。