简体   繁体   中英

write or write binary for saving data to file in python?

i am trying to save features from the directory to the file. The data and the error has been displayed below. What should be the correct "letter" in the open statement for saving the data without distorting it?

Error

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()

Data

 [[[ 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.        ]]]]

If you want to write your numpy array straight to file and be able to load it again, you should use pickle

To write it:

import pickle

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

To read it:

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

Convert it to a string object.

Ex:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM