繁体   English   中英

将数据追加到数组的“右”列和行(在Python中)

[英]Appending data to the 'right' column and row of an array (in Python)

这个想法是创建一个数组,其中第一个原始数据中的值对应于管理单元的ID。 第一列对应于该管理单元内图像的标签。 每个图像都有几个标签。 因此,其想法是检查是否已将任何标签附加到数组中,如果它们是首次出现,则我将其附加。 如果该标签比之前出现的要早,则该标签和管理单元ID的交集上的元素应增加1(对于首次出现的情况也是如此)。 我已经在这部分上了。 因此,假设管理单元的ID为1、36、15、20、16、3。我知道,现在我在ID为=的管理单元中分析标签为“狮子,牛,猫,熊猫”的图像36.在我之前带有“门”标签的某个地方,该标签在不同的管理部门中多次出现。 所以我想有一个数组,它看起来像:

[0, 1, 36, 15, 20, 16, 3],
['door', 5, 0, 0, 4, 0, 1],
['lion', 0, 1, 0, 0, 0, 0],
['cow', 0, 1, 0, 0, 0, 0],
['cat', 0, 1, 0, 0, 0, 0],
['panda', 0, 1, 0, 0, 0, 0]

到目前为止,我有空间部分,并且:

import numpy as np
tags_array = []
np.asarray(tags_array)
tags_array[0:] = [1, 36, 15, 20, 16, 3]
tags = 'lion,cow,cat,panda'
tags_sep = tags.split(',')
my_id = 36
for tag in tags_sep:
    #if tag is not yet in the array
    tags_array.append(tag) #to the first column
    tags_array.append(1) #to the column with the first row equal to 36
    #else add +1 to the element in the column 36 and row of the tag

任何提示都非常感谢!

如果您使用字典,则更加容易,然后可以轻松将其更改为所需的数组:

tags = 'lion,cow,cat,panda'.split(",")
id = 36

for i in range(len(tags)):
    if not id in ids:
        ids = np.append(ids, id)
        for key in dico.keys():
            dico[key] = np.append(dico[key], 0)

    if not tags[i] in dico.keys():
        dico[tags[i]] = np.zeros(len(ids)).astype(int)
        dico[tags[i]][int(np.where(ids==id)[0])] = 1
    else:
        dico[tags[i]][int(np.where(ids==id)[0])] += 1

当然,在您定义带有标签的dico作为键之前:

dico={}
dico['door'] = [5, 0, 0, 4, 0, 1]
dico['lion'] = [0, 0, 0, 0, 0, 0]
dico['cow'] = [0, 0, 0, 0, 0, 0]
dico['cat'] = [0, 0, 0, 0, 0, 0]
dico['panda'] = [0, 0, 0, 0, 0, 0]

ids

ids = np.array([1, 36, 15, 20, 16, 3])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM