pickle模块
- 保存.p文件
import numpy as np
import pickle
img_db = np.array(img_db)
zeros = np.zeros(12500, dtype=np.uint8)
ones = np.ones(12500, dtype=np.uint8)
labels = np.concatenate((zeros, ones))
obj = {'img': img_db, 'labels': labels}
with open('data.p', 'wb') as file:
pickle.dump(obj, file)
- 导入.p文件
# Load pickled data
import pickle
# TODO: fill this in
training_file = 'data/train.p'
testing_file = 'data/test.p'
with open(training_file, mode='rb') as f:
train = pickle.load(f)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
X_train, y_train = train['features'], train['labels']
X_test, y_test = test['features'], test['labels']
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(X_train.shape, X_test.shape)
导入.pkl文件
保存.pkl文件