相关疑难解决方法(0)

将不同的数据类型存储在一个NumPy数组中?

我有两个不同的数组,一个是字符串,另一个是整数.我想将它们连接到一个数组中,其中每列都具有原始数据类型.我目前的解决方案(见下文)将整个数组转换为dtype = string,这看起来非常低效.

combined_array = np.concatenate((A, B), axis = 1)

是否有可能多发dtypes的combined_array时候A.dtype = stringB.dtype = int

python arrays types numpy

43
推荐指数
2
解决办法
4万
查看次数

python numpy.savetext 混合格式的矩阵

我试图将一个矩阵保存为文本,该矩阵在每行末尾有 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)

python numpy

3
推荐指数
1
解决办法
2573
查看次数

标签 统计

numpy ×2

python ×2

arrays ×1

types ×1