类型错误:不可散列的类型:mlb 的“列表”

时间:2021-03-31 10:36:34

标签: python json dictionary sampling multilabel-classification

我正在尝试创建在 MultiLabelBinarizer 中使用的标签索引,以便我可以在大型 JSON 数据集上运行分层采样算法。 Json 文件格式为:

sample_json = {
  "test_field": "some data",
  "option": "some options",
  "distros": {
    "Labels": {
      "contact": [
        "first_names",
        "last_names",
        "nickname"
      ]
    }
  }
}

我需要创建它,以便我可以对标签进行编码并按照详细的 here 运行迭代分层。准备好索引后,我将运行 MultiLabelBinarizer 对“标签”列表进行编码并创建这些值的矩阵。然后,我将在该矩阵上运行分层抽样算法,以确定从零开始的训练和测试索引。我下面的代码导致错误。

for filename in files:
    if filename.endswith('.txt'):
        with open(filename) as f:
            lines = f.readlines()
            for l in lines:
                doc = json.loads(l)
                distros_per_test_field = defaultdict(set)
                test_field = doc['test_field'].strip().lower()
                distros_per_test_field[test_field].update(test_field)
                distributions = []
                for item in doc['distros']['Labels']:
                    for x in doc['distros']['Labels'][item]:
                        distributions.append(x)
                if distributions:
                    distros_per_test_field[test_field].add(distributions)
     

0 个答案:

没有答案