zoukankan      html  css  js  c++  java
  • 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)
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    Jmeter压力测试-结果分析(三十)
    C#正则密码验证
    Docker
    c# framework 缓存依赖
    NETMQ订阅,超时断线重连
    RabbitMQ发布/订阅模式
    RAS非对称加密解密,公钥私钥
    .NETCore批量插入数据BulkLoader
    .NETcore使用CSRedisCore操作Redis
    Rabbitmq简单队列
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/12665886.html
Copyright © 2011-2022 走看看