提取给定数组中的公共元素以创建一个新数组 [英] Extraction of common element in given arrays to make a new array

查看:57
本文介绍了提取给定数组中的公共元素以创建一个新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,data1,data2和data3是给定的数组.现在,我必须找出所有给定数组中存在的元素.然后,我必须制作仅包含那些公共元素的新数组,并将所有其他元素分配为nan值.

In the example below, data1, data2, and data3 are the given arrays. Now, I have to find out the element which exist in all the given arrays. Then, I have to make new array including only those common elements, and assigning all other elements as nan value.

将numpy导入为np

import numpy as np

data1 = np.array ([[1,2,33,4,33,6],[7,8,9,10,93,12]])
data2 = np.array ([[1,14,33,15,33,17],[18,19,20,21,93,23]])
data3 = np.array ([[24,25,33,26,1,28],[93,30,31,32,93,34]])

所需结果如下所示作为结果数组:只有值33和93可用于所有给定数组的相同位置(即它们应该重叠).换句话说,我只需要从这里的每个数组中找出具有相同值的重叠元素.

The required result is shown below as the result array: Only the values 33 and 93 are available into all the given arrays at the same position (i.e., they should be overlapped). In other words, I have to find out only overlapped elements with the same values from each array here.

result = np.array([[nan,nan,33,nan,nan,nan],[nan,nan,nan,nan,93,nan]])

我期待一种有效的计算方法.任何想法将不胜感激.

I am expecting an efficient way of calculating it. Any idea would be highly appreciated.

推荐答案

使用布尔索引

import numpy as np
result = np.empty_like(data1, dtype=float)
# Make an array of True-False values storing which indices are the same
indices = (data1==data2) * (data2==data3)
result[indices] = data1[indices]
result[~indices] = np.nan

如果您有更多的数组,则可以执行以下操作: (假设数组在列表中)

If you have more arrays, you can do something like this: (assuming the arrays are in a list)

# construct the list of arrays
data = []
for i in xrange(8):
    data.append(np.random.randint(0,2, (20, 20)))
indices = data[0] == data[1]
for i in xrange(2, 8):
    indices *= (data[0] == data[i])
result = np.empty_like(data[1], dtype=float)
result[indices] = data1[indices]
result[~indices] = np.nan
result

但是,如果每个数组只是3D数组的一部分,那就更好了. 在另一个答案中显示,给定几个数组,您可以根据它们创建3D数组,然后使用np.where. 这是一种有效的方法. 我将在这里展示如何通过阵列广播来做到这一点. 在这种情况下,我们可以执行以下操作

It would be much better, however, if each array were just a slice of a 3D array. It was shown in another answer that, given several arrays, you can make a 3D array from them and then use np.where. That is a valid approach. I'll show how to do this with array broadcasting here. In this case we can do the following

# construct 3D array with random data
# each data array will be a slice of this array, e.g. data[0], data[1], etc.
data = np.random.randint(0, 3, (3, 4, 5))
# make an empty array to store the results as before
result = np.empty_like(data[0], dtype=float)
# use broadcasting to test for equality
indices = np.all(data == data[0], axis=0)
# Do the assignment as before
result[indices] = data[0][indices]
result[~indices] = np.nan
result

这篇关于提取给定数组中的公共元素以创建一个新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆