【问题标题】:Finding all possible iterations of a nested JSON structure查找嵌套 JSON 结构的所有可能迭代
【发布时间】:2022-11-04 00:34:33
【问题描述】:

我有一个我一直在处理的配置文件,例如:

"Preprocessing": {
"BOW":{"ngram_range":[1,2], "max_features":[100, 200]},
"RemoveStopWords": {"Parameter1": ["..."]}
}

这个想法是获取这些数据并在两个预处理步骤之间运行每次迭代,并将其传递给 Preprocessing 对象。我正在寻找的输出是:

[{"BOW":{"ngram_range":1, "max_features":100}, "RemoveStopWords":{"Parameter1": "..."},
{"BOW":{"ngram_range":2, "max_features":100}, "RemoveStopWords":{"Parameter1": "..."},
{"BOW":{"ngram_range":1, "max_features":200}, "RemoveStopWords":{"Parameter1": "..."},
{"BOW":{"ngram_range":2, "max_features":200}, "RemoveStopWords":{"Parameter1": "..."}]

当前代码:

def unpack_preprocessing_steps(preprocessing: dict):
        """
        This script will take the Preprocessing section of the config file
        and produce a list of preprocessing combinations.
        """
        preprocessing_steps = [] # save for all steps bow, w2v, etc.
        preprocessing_params = [] # individual parameters for each preprocessing step
        for key, values in preprocessing.items():
            preprocessing_steps.append(key) 
            for _, values2 in values.items():
                preprocessing_params.append(values2)
        iterables = product(*preprocessing_params) # Creates a matrix of every combination
        iterable_of_params = [i for i in iterables] 

        exploded_preprocessing_list = []
        for params in iterable_of_params:
            individual_objects = {} # store each object as an unpackable datatype
            for step, param in zip(preprocessing_steps, params):

                individual_objects[step] = param # This stores ever iteration as it's own set of preprocesses
            exploded_preprocessing_list.append(individual_objects)
        
        return exploded_preprocessing_list

当前输出(和错误)输出为:

[{"BOW":1, "RemoveStopWords":100},
 {"BOW":2, "RemoveStopWords":200}]

【问题讨论】:

    标签: python machine-learning data-structures statistics


    【解决方案1】:

    这应该对你有用 - 假设你总是想要相同的 RemoveStopWords 部分:

    config = {
        "Preprocessing": {
          "BOW": {"ngram_range":[1,2]},
          "RemoveStopWords": {"Parameter1": ["..."]},
        }
    }
    
    newconf = []
    for rng in config["Preprocessing"]["BOW"]["ngram_range"]:
        newconf.append({
            "BOW": {"ngram_range": rng},
            "RemoveStopWords": config["Preprocessing"]["RemoveStopWords"],
        })
    
    print(newconf)
    

    结果:

    [{'BOW': {'ngram_range': 1}, 'RemoveStopWords': {'Parameter1': ['...']}}, {'BOW': {'ngram_range': 2}, 'RemoveStopWords': {'Parameter1': ['...']}}]
    

    【讨论】:

    • 不幸的是,我正在尝试使用可变数量的功能和参数编写此代码。所以代码应该足够健壮以捕获它以前从未见过的关键值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2016-12-07
    • 1970-01-01
    • 2015-09-03
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多