numpy - Python - Datatype Retention in saving to Pickle file -
i'm saving dictionary of numpy arrays pickle file. , unpickling them new variables. code this:
pickling:
# here variables 'train_dataset', 'train_labels' etc np arrays. save = { 'train_dataset': train_dataset, 'train_labels': train_labels, 'valid_dataset': valid_dataset, 'valid_labels': valid_labels, 'test_dataset': test_dataset, 'test_labels': test_labels, } pickle.dump(save, f, pickle.highest_protocol)
unpickling:
save = pickle.load(f) train_dataset_new = save['train_dataset'] train_labels_new = save['train_labels'] valid_dataset_new = save['valid_dataset'] valid_labels_new = save['valid_labels'] test_dataset_new = save['test_dataset'] test_labels_new = save['test_labels']
will variables loaded pickle file np arrays? please elaborate bit if can.
thanks
quoting directly docs:
read string open file object file , interpret pickle data stream, reconstructing , returning original object hierarchy.
little test code check datatype of loaded variable <type 'numpy.ndarray'>
:
import numpy np import pickle #f = open( "pickled.p", "wb" ) train_dataset = np.ones(5) train_labels = np.ones(5) valid_dataset = np.ones(5) valid_labels = np.ones(5) test_dataset = np.ones(5) test_labels = np.ones(5) print type(train_dataset) # <type 'numpy.ndarray'> print train_dataset.shape # <5l,> # here variables 'train_dataset', 'train_labels' etc np arrays. save = { 'train_dataset': train_dataset, 'train_labels': train_labels, 'valid_dataset': valid_dataset, 'valid_labels': valid_labels, 'test_dataset': test_dataset, 'test_labels': test_labels, } pickle.dump(save, open( "save.p", "wb" ), pickle.highest_protocol) save = pickle.load(open( "save.p", "rb" )) train_dataset_new = save['train_dataset'] train_labels_new = save['train_labels'] valid_dataset_new = save['valid_dataset'] valid_labels_new = save['valid_labels'] test_dataset_new = save['test_dataset'] test_labels_new = save['test_labels'] print type(train_dataset_new) # <type 'numpy.ndarray'> print train_dataset_new.shape # <5l,>
Comments
Post a Comment