[英]Compare two numpy arrays and return matching nth element of row
我有 2 個 numpy arrays:
第一個數組包含 3 個元素的矩陣:Class 名稱、ID、坐標
['Mobile Phone', '000ad20b5e452b24','0.0196.800512617.6939.200512']
第二個數組包含 3 個元素的矩陣: Class 名稱、ID、圖像數組:
['Mobile Phone', '000ad20b5e452b24',
array([[[183, 205, 210],
[181, 203, 208],
[186, 206, 211],
...,
[202, 216, 222],
[201, 213, 219],
[202, 214, 220]],
[[178, 200, 205],
[177, 199, 204],
[179, 199, 204],
...,
[186, 200, 206],
[189, 201, 207],
[194, 206, 212]],
[[174, 196, 201],
[173, 195, 200],
[174, 193, 200],
...,
[170, 184, 190],
[172, 184, 190],
[177, 189, 195]],
...,
[[217, 226, 235],
[216, 225, 234],
[213, 222, 231],
...,
[ 88, 97, 110],
[ 96, 105, 118],
[100, 109, 122]],
[[202, 209, 218],
[193, 200, 209],
[181, 190, 199],
...,
[124, 128, 139],
[134, 138, 149],
[139, 143, 154]],
[[183, 190, 199],
[168, 175, 184],
[152, 161, 170],
...,
[147, 149, 159],
[160, 162, 173],
[167, 169, 180]]]
第一個數組可能有重復的 ID,但第二個沒有。
對於第一個數組中的每一行,我想檢查第二個數組是否具有相同的 id 和 class 名稱和 append 或獲取圖像數組。
這是一個簡化的示例,帶有一維數組。 只有一個類將匹配,在圖像數組中找不到另一個示例。 內連接將捕獲所需的匹配,您可以通過檢查圖像數據的數據類型看到它仍然是一個 ndarray。
import numpy as np
classes = np.array([['Mobile Phone', '000ad20b5e452b24','0.0196.800512617.6939.200512'],
['Mobile Phone', '000ad20b5e99999','0.0196.800512617.6939.200512']])
image_data = np.array([['Mobile Phone', '000ad20b5e452b24', np.array([183, 205, 210])],
['Mobile Phone', '000ad20b5e444444', np.array([183, 205, 210])]])
c = pd.DataFrame(classes, columns=['class','id','coordinates'])
i = pd.DataFrame(image_data, columns=['class','id','image'])
output = c.merge(i, on=['class','id'], how='inner')
print(output)
print(type(output['image'].iloc[0]))
Output
class id coordinates image
0 Mobile Phone 000ad20b5e452b24 0.0196.800512617.6939.200512 [183, 205, 210]
<class 'numpy.ndarray'>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.