我有一个包含图像向量和相关标签(即images
)的类列表。
images.append([np.array(image_array_to_vector),np.array([label])])
如果我想读取X
中的图像和y
中的标签,我执行了以下操作:
X = np.array([i[0] for i in images])
y = [i[1] for i in images]
然后,我想将X
保存在一个文本文件中,如下所示:
X_to_text_file = np.savetxt('x.txt', X.reshape(np.shape(X)), fmt='%5f')
上面的方法运行良好。我想做的更改是将标签附加到X
中的向量,并将其保存到一个文本文件中。
例如,我尝试这样做:
X = np.array([[i[0],i[1]] for i in images])
但是,得到了以下错误:
TypeError: Mismatch between array dtype ('object') and format specifier ('%5f %5f')
并且,当尝试使用字典时,如下所示:
X = np.array([{i[1]:i[0]} for i in images])
我得到了这个错误:
TypeError: unhashable type: 'numpy.ndarray'
那么,是的,我如何将标签附加到向量并将其保存在文本文件中?
谢谢。
发布于 2018-10-18 20:25:25
在不知道原始数据的外观或您希望将来如何从.txt中检索信息的情况下,回答此问题有点困难,但将"%5f“格式更改为字符串"%s”格式应该可以解决您的问题:
X_to_text_file = np.savetxt('x.txt', X.reshape(np.shape(X)), fmt='%s')
https://stackoverflow.com/questions/52881753
复制相似问题