我有两个不同的数组,一个是字符串,另一个是整数.我想将它们连接到一个数组中,其中每列都具有原始数据类型.我目前的解决方案(见下文)将整个数组转换为dtype = string,这看起来非常低效.
combined_array = np.concatenate((A, B), axis = 1)
是否有可能多发dtypes的combined_array时候A.dtype = string和B.dtype = int?
我试图将一个矩阵保存为文本,该矩阵在每行末尾有 288 个浮点数和 1 个字符串,我使用了这样的 savetxt:
np.savetxt('name', matrix, delimiter=' ', header='string', comments='', fmt= '%3f'*288 + '%s')
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行代码时,它会引发如下异常:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\numpy\lib\npyio.py", line 1371, in savetxt
v = format % tuple(row) + newline
TypeError: must be real number, not numpy.str_
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\numpy\lib\npyio.py", line 1375, in savetxt
% (str(X.dtype), format))
TypeError: …Run Code Online (Sandbox Code Playgroud)