我正在尝试将目录中的功能保存到文件中。数据和错误已在下面显示。 打开语句中用于保存数据而不扭曲数据的正确“字母”应该是什么?
错误
write() argument must be str, not numpy.ndarray
CODE
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. ]]]]
答案 0 :(得分:1)
如果您想将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)
答案 1 :(得分:0)
将其转换为字符串对象。
<强>实施例强>
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(str(bottleneck_features_train))
file.close()