gpt4 book ai didi

python - 在python中编写或编写二进制文件以将数据保存到文件中?

转载 作者:行者123 更新时间:2023-11-30 09:49:01 25 4
gpt4 key购买 nike

我正在尝试将目录中的功能保存到文件中。数据和错误已显示如下。 open 语句中保存数据而不扭曲数据的正确“字母”应该是什么?

错误

write() argument must be str, not numpy.ndarray

代码

generator1 = ImageDataGenerator(rescale=1. / 255).flow_from_directory(train_data_dir,target_size=(img_width, img_height),batch_size=batch_size,shuffle=False)
print("generator1 images loaded")

for i in range(len(generator1)):
kl = generator1[i]
bottleneck_features_train = model.predict_on_batch(kl[0])
print("first prediction")
print (bottleneck_features_train)
file =open('/home/rehan/predictions/bottleneck_features_train_%s.py'%i, 'w')
file.write(bottleneck_features_train)
file.close()

数据

 [[[ 0.50452518  0.          0.         ...,  0.          0.84091663  0.        ]
[ 0.538715 0. 0.07498804 ..., 0. 0.50906491 0. ]
[ 0.5355916 0. 1.27406454 ..., 0.14854321 0.55418521 0. ]
[ 1.24407315 0. 1.74664402 ..., 0.11201498 0.55507243 0. ]]

[[ 0.05677766 0. 0. ..., 0. 0.82949859 0. ]
[ 0. 0. 0.19032657 ..., 0.12606588 0.02242988 0. ]
[ 0.10961182 0. 1.54800272 ..., 0.37970039 0. 0. ]
[ 0.42456442 0. 1.87542152 ..., 0.36944041 0.29935738 0. ]]

[[ 0.04067653 0. 0. ..., 0. 0.55476826 0. ]
[ 0.31820443 0. 0. ..., 0. 0. 0. ]
[ 0.58587539 0. 0.25692856 ..., 0.03251171 0. 0. ]
[ 0.66836131 0. 0.19993514 ..., 0. 0.19460687 0. ]]

[[ 0.46838504 0. 0. ..., 0. 0.91270626 0. ]
[ 1.46697009 0. 0. ..., 0. 0.53989708 0. ]
[ 2.26325178 0. 0. ..., 0. 0. 0. ]
[ 1.71381867 0. 0. ..., 0. 0.34278265 0. ]]]]

最佳答案

如果您想将 numpy 数组直接写入文件并能够再次加载它,您应该使用 pickle

写它:

import pickle

with open("pickle_file.pickle", "wb") as handle:
pickle.dump(your_array, handle)

阅读它:

with open("pickle_file.pickle", "rb") as handle:
your_array = pickle.load(handle)

关于python - 在python中编写或编写二进制文件以将数据保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48344228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com