如何将增强的MNIST数据集保存到文件?

问题描述 投票:0回答:1

我对sklearn库的MNIST数据集进行了数据扩充。现在,我想将扩充数据集保存到文件中,因为其计算时间很长。

我想以类似于原始MNIST的格式保存它,其类型为sklearn.utils.Bunch或字典,以便保留用于检索数据的通用格式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序列化

python-3.x dictionary scikit-learn save mnist
1个回答
0
投票

此问题并非特定于MNIST。如果要将ndarray数据存储为JSON,则必须做更多的预处理。看到这里-NumPy array is not JSON serializable

否则,您应该可以直接在字典中使用numpy.save()或pickle。https://docs.scipy.org/doc/numpy/reference/generated/numpy.save.htmlhttps://wiki.python.org/moin/UsingPickle

© www.soinside.com 2019 - 2024. All rights reserved.