简体   繁体   English

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

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

The idea is to create an array, where the values in the first raw correspond to the IDs of the administrative units. 这个想法是创建一个数组,其中第一个原始数据中的值对应于管理单元的ID。 The first column corresponds to the tags to the images which are within this administrative unit. 第一列对应于该管理单元内图像的标签。 Every image has several tags. 每个图像都有几个标签。 So the idea is to check if any of the tags are already appended to the array, if they appear for the first time then I append them. 因此,其想法是检查是否已将任何标签附加到数组中,如果它们是首次出现,则我将其附加。 If the tag has appeared before than the element on the intersection of this tag and the ID of the administrative unit should increase by 1 (also in the case if they appear for the first time). 如果该标签比之前出现的要早,则该标签和管理单元ID的交集上的元素应增加1(对于首次出现的情况也是如此)。 I have already stacked on this part. 我已经在这部分上了。 So let's say the IDs of the administrative units are 1, 36, 15, 20, 16, 3. And I know that now I analyse the image with tags 'lion,cow,cat,panda' in the administrative unit with the ID = 36. And somewhere before I has the tag 'door', which appeared several times in different administrative units. 因此,假设管理单元的ID为1、36、15、20、16、3。我知道,现在我在ID为=的管理单元中分析标签为“狮子,牛,猫,熊猫”的图像36.在我之前带有“门”标签的某个地方,该标签在不同的管理部门中多次出现。 So I would like to have an array, which will look like: 所以我想有一个数组,它看起来像:

[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]

So far I have the spatial part and that: 到目前为止,我有空间部分,并且:

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

Any hints are really appreciated! 任何提示都非常感谢!

If you work with a dictionary it's easier and then you can easily change it into the array you want : 如果您使用字典,则更加容易,然后可以轻松将其更改为所需的数组:

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

of course before you should define dico with the tags as the keys : 当然,在您定义带有标签的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]

and ids : ids

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

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

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