Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何将扩充的MNIST数据集保存到文件?_Python 3.x_Dictionary_Scikit Learn_Save_Mnist - Fatal编程技术网

Python 3.x 如何将扩充的MNIST数据集保存到文件?

Python 3.x 如何将扩充的MNIST数据集保存到文件?,python-3.x,dictionary,scikit-learn,save,mnist,Python 3.x,Dictionary,Scikit Learn,Save,Mnist,我在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

我在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序列化


这个问题不是MNIST所特有的。如果您想将ndarray数据存储为JSON,那么您必须进行更多的预处理。看这里-

否则,您应该能够直接在字典中使用numpy.save()或pickle。