python如何将 数组文件 存储为json文件以及对于json文件的读取
简介
最近项目中要用到PCA计算,PCA从文件中读取数据,然后再写入数据
code
#encoding = utf-8 import numpy as np import json from sklearn.decomposition import PCA from sklearn.datasets import load_iris from json import JSONEncoder fileName = "projector.json" with open(fileName,‘r‘,encoding=‘utf-8‘) as file: data=json.load(file) #<class ‘dict‘>,JSON文件读入到内存以后,就是一个Python中的字典。 # 字典是支持嵌套的, # print(data) X = data["data"] pca = PCA(n_components=3) pca.fit(X) newX=pca.transform(X) new_item = {‘image_path‘: newX} data = [] data.append(new_item) class NumpyArrayEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() return JSONEncoder.default(self, obj) encodedNumpyData = json.dumps(data, cls=NumpyArrayEncoder) with open(‘pathpoints.json‘, ‘w+‘) as jsonFile: jsonFile.write(encodedNumpyData)
相关推荐
夜斗不是神 2020-11-17
zhjn0 2020-11-24
学习web前端 2020-11-09
waiwaiLILI 2020-11-03
raidtest 2020-10-09
myccc 2020-09-24
jzlixiao 2020-09-15
guicaizhou 2020-09-15
digwtx 2020-09-14
大秦铁骑 2020-08-19
thatway 2020-08-19
lovecodeblog 2020-08-19
codetyper 2020-08-16
comwayLi 2020-08-16
MongoDB数据库 2020-08-16
cjsyrwt 2020-08-14
Tristahong 2020-08-05
csuzxm000 2020-08-02
前端开发Kingcean 2020-07-30