我正在尝试使用混合数据类型(字符串,整数,整数)创建NumPy数组/矩阵(Nx3).但是当我通过添加一些数据来附加这个矩阵时,我收到一个错误:TypeError:无效的类型提升.拜托,有人可以帮我解决这个问题吗?
当我使用样本数据创建数组时,NumPy将矩阵中的所有列转换为’S’数据类型.而且我不能为数组指定数据类型,因为当我这样做时res = np.array([“TEXT”,1,1],dtype =’S,i4,i4′) – 我收到一个错误:TypeError :期望一个可读的缓冲区对象
templates.py
import numpy as np
from pprint import pprint
test_array = np.zeros((0, 3), dtype='S, i4, i4')
pprint(test_array)
test_array = np.append(test_array, [["TEXT", 1, 1]], axis=0)
pprint(test_array)
print("Array example:")
res = np.array(["TEXT", 1, 1])
pprint(res)
输出:
array([], shape=(0L, 3L),
dtype=[('f0', 'S'), ('f1', '
Array example:
array(['TEXT', '1', '1'], dtype='|S4')
错误:
Traceback (most recent call last):
File "templates.py", line 5, in
test_array = np.append(test_array, [["TEXT", 1, 1]], axis=0)
File "lib\site-packages\numpy\lib\function_base.py", line 3543, in append
return concatenate((arr, values), axis=axis)
TypeError: invalid type promotion