我在sklearn库的MNIST数据集上进行了数据增强。现在我想将增强的数据集保存到文件中,因为它的计算相当长。
我想以类似于原始sklearn.utils.Bunch或字典的MNIST格式保存它,以便保存用于检索数据的通用表单X, y = mnist['data'], mnist['target'];,
我怎么能这么做?
import matplotlib;
import matplotlib.pyplot as plt;
from sklearn.datasets import fetch_openml;
mnist = fetch_openml("mnist_784");
X, y = mnist['data'], mnist['target'];
y = y.astype(int);
....
X_augmented, y_augmented = expand_dataset(X,y);
data_augmented = {"data": X_train_augmented, "target": y_train_augmented};
How to save to file?我试过
import json
f = open("MNIST_augmented","w");
json.dump(data_augmented, f);
f.close();但我明白错误
TypeError: ndarray类型的对象不是JSON可序列化的
发布于 2019-12-06 08:30:12
这个问题并不是专门针对MNIST的。如果要将ndarray数据存储为JSON,则必须进行更多的预处理。看这里- NumPy array is not JSON serializable
否则,您应该能够在字典中直接使用numpy.save()或泡菜。https://docs.scipy.org/doc/numpy/reference/generated/numpy.save.html https://wiki.python.org/moin/UsingPickle
https://stackoverflow.com/questions/59203381
复制相似问题