在这里,我们将给大家分享关于检查两个3Dnumpy数组是否包含重叠的2D数组的知识,让您更了解检查两个数组是否相等的本质,同时也会涉及到如何更有效地c#–检查表是否包含重叠的时间跨度、numpy中的3
在这里,我们将给大家分享关于检查两个3D numpy数组是否包含重叠的2D数组的知识,让您更了解检查两个数组是否相等的本质,同时也会涉及到如何更有效地c# – 检查表是否包含重叠的时间跨度、numpy中的3D数组是否轴= 3?、numpy:索引3D数组,将最后一个轴的索引存储在2D数组中、PHP检查数组是否包含另一个数组的所有数组值的内容。
本文目录一览:- 检查两个3D numpy数组是否包含重叠的2D数组(检查两个数组是否相等)
- c# – 检查表是否包含重叠的时间跨度
- numpy中的3D数组是否轴= 3?
- numpy:索引3D数组,将最后一个轴的索引存储在2D数组中
- PHP检查数组是否包含另一个数组的所有数组值
检查两个3D numpy数组是否包含重叠的2D数组(检查两个数组是否相等)
我有两个非常大的numpy数组,它们都是3D的。我需要找到一种有效的方法来检查它们是否重叠,因为首先将它们都转换为集合会花费很长时间。我尝试使用在此找到的另一个解决方案来解决相同的问题,但适用于2D阵列,但是我没有设法使其适用于3D。这是2D解决方案:
nrows, ncols = A.shapedtype={''names'':[''f{}''.format(i) for i in range(ndep)], ''formats'':ndep * [A.dtype]}C = np.intersect1d(A.view(dtype).view(dtype), B.view(dtype).view(dtype))# This last bit is optional if you''re okay with "C" being a structured array...C = C.view(A.dtype).reshape(-1, ndep)
(其中A和B是2D数组)我需要找到重叠的numpy数组的数量,而不是特定的数组。
答案1
小编典典我们可以利用views
我在一些问答中使用过的辅助功能来发挥作用。要获得子数组的存在,我们可以np.isin
在视图上使用或使用更加费力的视图np.searchsorted
。
方法1: 使用np.isin
-
# https://stackoverflow.com/a/45313353/ @Divakardef view1D(a, b): # a, b are arrays a = np.ascontiguousarray(a) b = np.ascontiguousarray(b) void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1])) return a.view(void_dt).ravel(), b.view(void_dt).ravel()def isin_nd(a,b): # a,b are the 3D input arrays to give us "isin-like" functionality across them A,B = view1D(a.reshape(a.shape[0],-1),b.reshape(b.shape[0],-1)) return np.isin(A,B)
方法2: 我们也可以利用np.searchsorted
于views
-
def isin_nd_searchsorted(a,b): # a,b are the 3D input arrays A,B = view1D(a.reshape(a.shape[0],-1),b.reshape(b.shape[0],-1)) sidx = A.argsort() sorted_index = np.searchsorted(A,B,sorter=sidx) sorted_index[sorted_index==len(A)] = len(A)-1 idx = sidx[sorted_index] return A[idx] == B
因此,这两个解决方案为我们提供了a
in中每个子数组的存在掩码b
。因此,为了获得所需的计数,它应该是-isin_nd(a,b).sum()
或isin_nd_searchsorted(a,b).sum()
。
样品运行-
In [71]: # Setup with 3 common "subarrays" ...: np.random.seed(0) ...: a = np.random.randint(0,9,(10,4,5)) ...: b = np.random.randint(0,9,(7,4,5)) ...: ...: b[1] = a[4] ...: b[3] = a[2] ...: b[6] = a[0]In [72]: isin_nd(a,b).sum()Out[72]: 3In [73]: isin_nd_searchsorted(a,b).sum()Out[73]: 3
大型阵列上的时间-
In [74]: # Setup ...: np.random.seed(0) ...: a = np.random.randint(0,9,(100,100,100)) ...: b = np.random.randint(0,9,(100,100,100)) ...: idxa = np.random.choice(range(len(a)), len(a)//2, replace=False) ...: idxb = np.random.choice(range(len(b)), len(b)//2, replace=False) ...: a[idxa] = b[idxb]# Verify outputIn [82]: np.allclose(isin_nd(a,b),isin_nd_searchsorted(a,b))Out[82]: TrueIn [75]: %timeit isin_nd(a,b).sum()10 loops, best of 3: 31.2 ms per loopIn [76]: %timeit isin_nd_searchsorted(a,b).sum()100 loops, best of 3: 1.98 ms per loop
c# – 检查表是否包含重叠的时间跨度
我想检查一下我的table.i.e中是否有任何重复的记录
From Date To Date ---------------------- 9/01/2012 9/16/2012 8/23/2012 8/24/2012 8/25/2012 8/25/2012 8/5/2012 8/6/2012 8/26/2012 8/27/2012 9/15/2012 9/23/2012
该表包含重复记录,因为它们的日期范围是映射的
From Date To Date ---------------------- 9/01/2012 9/16/2012 9/15/2012 9/23/2012
它应该返回false.
解决方法
var query = from row in dt.AsEnumerable() from row1 in dt.AsEnumerable() where ( ( DateTime.Parse(row1.Field<string>("fromDate")) >= DateTime.Parse(row.Field<string>("fromDate")) && DateTime.Parse(row1.Field<string>("fromDate")) <= DateTime.Parse(row.Field<string>("toDate")) ) || ( DateTime.Parse(row1.Field<string>("toDate")) >= DateTime.Parse(row.Field<string>("fromDate")) && DateTime.Parse(row1.Field<string>("toDate")) <= DateTime.Parse(row.Field<string>("toDate")) ) ) select new { fromDate = DateTime.Parse(row1.Field<string>("fromDate")),toDate = DateTime.Parse(row1.Field<string>("toDate")) }; //This lst contains the dates which are overlapping var lst = query.distinct().ToList();
numpy中的3D数组是否轴= 3?
您在通话中创建4D阵列
c = np.mean([a,b],axis=3) # [a,b] adds 4th dimension
np.array([a,b]).shape
输出
(2,3,3)
numpy:索引3D数组,将最后一个轴的索引存储在2D数组中
我有一个ndarray
的shape(z,y,x)
含值。我想这个指数阵列的另一个ndarray
的shape(y,x)
包含我感兴趣的值的z-
index的。
import numpy as npval_arr = np.arange(27).reshape(3,3,3)z_indices = np.array([[1,0,2], [0,0,1], [2,0,1]])
由于我的数组很大,因此我尝试使用它np.take
来避免不必要的数组副本,但无法用它来索引3维数组。
如何索引val_arr
以z_indices
在所需的z轴位置获得值?预期结果将是:
result_arr = np.array([[9,1,20], [3,4,14], [24,7,17]])
答案1
小编典典您可以choose
用来进行选择:
>>> z_indices.choose(val_arr)array([[ 9, 1, 20], [ 3, 4, 14], [24, 7, 17]])
该功能choose
非常有用,但要理解它可能有些棘手。本质上,给定一个数组(val_arr
),我们可以z_indices
沿第一个轴从每个n维切片中进行一系列选择()。
另外:任何花式索引操作都会创建一个新数组,而不是原始数据视图。这是不可能的指标val_arr
有z_indices
没有创建一个全新的阵列。
PHP检查数组是否包含另一个数组的所有数组值
$all = array( 0 => 307, 1 => 157, 2 => 234, 3 => 200, 4 => 322, 5 => 324);$search_this = array( 0 => 200, 1 => 234);
我想找出$ all是否包含所有$ search_this值并返回true或false。有什么想法吗?
答案1
小编典典看一下array_intersect()。
$containsSearch = count(array_intersect($search_this, $all)) == count($search_this);
今天关于检查两个3D numpy数组是否包含重叠的2D数组和检查两个数组是否相等的讲解已经结束,谢谢您的阅读,如果想了解更多关于c# – 检查表是否包含重叠的时间跨度、numpy中的3D数组是否轴= 3?、numpy:索引3D数组,将最后一个轴的索引存储在2D数组中、PHP检查数组是否包含另一个数组的所有数组值的相关知识,请在本站搜索。
本文标签: