numpy 基于数值比较,在3D数据矩阵中的参数之间查找匹配的2D交集?

inkz8wg9  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(115)

我有一个三维矩阵

import numpy as np
matrix = np.random.randn(100,10,500)
  • X尺寸:数据样本
  • Y尺寸:参数/变量类型
  • Z尺寸:位置

在上面的矩阵中,10个变量在500个位置上有100个样本。例如,如果我索引变量#1,那么我在500个位置中的每个位置上都有该变量的100个样本(跨度为20秒)。
我需要确定哪些数据样本和哪些位置符合特定条件。(为特定变量/参数编制索引),并成对出现,说明样本和条件匹配的位置。例如,上述3D矩阵的匹配将在sample = 50location = 31处。我通常将其返回为tuplesarray,其中每个tuple包含样本和位置编号。
标准可以规定一个或多个:

  • 数值范围:例如在-1.0和5.5之间
  • 个体值:例如,值必须== 1.39

可以为一个或多个指定这些范围和单个值:

  • 变量/参数

例如:

  • 参数#1(Y轴索引= 0):(-1.0至5.5)(10.3至12.1)20.32
  • 参数#5(Y指数= 4):10.0(1.0至800.0)
  • 参数#8(Y轴索引= 7):(50至100)

此外,我还需要反转条件的能力,例如:

  • 参数#1(Y轴索引= 0):非((-1.0至5.5)(10.3至12.1)20.32)

我将需要具有指示样本索引(X轴)和位置索引(Z轴)的元组列表,其中参数#1参数#5参数#8匹配它们在3D矩阵中的条件。
我一直在研究intersect1D,也一直在循环中使用np.where,这非常非常低效,例如:

import numpy as np
matrix = np.random.randn(100,10,500)

net_array = None
for parameter in parameters: 

    total_result = None
    
    for lower_range_value, upper_range_value in range_values[parameter]:
        result = np.where( (matrix[:,parameter,:] >= lower_range_value) & (matrix[:,parameter,:] <= upper_range_value)
        
        if result[0].size > 0: 
            if type(total_result) == type(None):
                total_result = result
            else: 
                concat_0 = np.concatenate( (total_result[0], result[0]) )
                concat_1 = np.concatenate( (total_result[1], result[1]) )
                total_result = (concat_0,concat_1)
            
    for discrete_value in discrete_values[parameter]:
        result = np.where( matrix[:,parameter,:] == threshold )
        
        if result[0].size > 0:
            if type(total_result) == type(None):
                total_result = result
            else: 
                concat_0 = np.concatenate( (total_result[0], result[0]) )
                concat_1 = np.concatenate( (total_result[1], result[1]) )
                total_result = (concat_0,concat_1)

    if type(total_result) != type(None): 
        if type(net_array) == type(None): 
            net_array = np.stack( [ total_result[0] , total_result[1] ] , axis = -1) 
        else: 
            stacked_total_result = np.stack( [ total_result[0] , total_result[1] ] , axis=-1 )
            match_indexes = (net_array[:,None] == stacked_total_result).all(-1).any(1)
            net_array = net_array[match_indexes]
            if np.any(match_indexes) == False: 
                break

是否有一种有效的方法来查找样本索引(X轴)和位置索引(Z轴),其中一个或多个参数(Y轴)均匹配其标准?

svmlkihl

svmlkihl1#

我觉得你想要这样的东西?

import numpy as np

(n_samples, n_vars, n_locs) = (100, 10, 500)
matrix = np.random.randn(n_samples, n_vars, n_locs)

param_idx2ranges = {
    0: [(-2.0, -2.5), (0.5, 0.75), (2.32, 2.32)],
    4: [(1.0, 1.0), (2.0, 40.0)],
    7: [(1.5, 2.0)],
}
final_mask = np.ones((n_samples, n_locs), dtype="bool")
for (param_idx, ranges) in param_idx2ranges.items():
    param_mask = np.zeros((n_samples, n_locs), dtype="bool")
    for (min_val, max_val) in ranges:
        param_mask |= (min_val <= matrix[:, param_idx]) & (
            matrix[:, param_idx] <= max_val
        )

    final_mask &= param_mask

idxs = np.argwhere(final_mask)
print(matrix[idxs[:, 0], :, idxs[:, 1]])

求反只涉及在需要的地方应用~ operator

相关问题