You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术问询:统计两组数组相等数量及Numpy子数组差异数

Solution for Counting Equal and Differing Subarrays in NumPy Arrays

Got it, let's break down your two requirements clearly with actionable code and explanations.

1. Count the Number of Equal Subarrays

To count how many subarrays are identical between two NumPy arrays, we can leverage NumPy's vectorized operations to avoid manual looping:

  • First, validate that the two input arrays have the same dimensions (comparing subarrays doesn't make sense if shapes don't match).
  • Use (a == b).all(axis=1) to create a boolean array where each entry is True if the corresponding subarrays in a and b are completely identical (the axis=1 flag checks equality across all elements in each subarray).
  • Sum the boolean array—since True is treated as 1 and False as 0, this gives the total count of equal subarrays.

Example code:

import numpy as np

a = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]])
b = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,0,0]])

# Count equal subarrays
if np.array_equal(a.shape, b.shape):
    equal_count = (a == b).all(axis=1).sum()
    print(f"Number of equal subarrays: {equal_count}")  # Output: 4
else:
    print("Arrays have different shapes, cannot compare subarrays.")

2. Implement the diff_count Function

The diff_count function needs to count how many subarrays differ between the two input arrays, with no list comprehensions allowed. Here's a clean, efficient implementation using NumPy's built-in tools:

Approach

  1. First, check if the input arrays have matching shapes (raise an error if not, to avoid unexpected behavior).
  2. Use (a != b).any(axis=1) to generate a boolean mask: each entry is True if the corresponding subarrays have at least one differing element.
  3. Sum this mask to get the total number of differing subarrays.

Function Code

import numpy as np

def diff_count(a, b):
    # Validate input shapes match
    if not np.array_equal(a.shape, b.shape):
        raise ValueError("Input arrays must have the same shape.")
    # Create mask for subarrays with any differences
    diff_mask = (a != b).any(axis=1)
    # Count the number of differing subarrays
    return diff_mask.sum()

# Test with your example
a = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]])
b = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,0,0]])
y = diff_count(a, b)
print(y)  # Output: 1

Explanation

  • (a != b) creates a boolean array of the same shape as a/b, marking every element where the two arrays differ.
  • .any(axis=1) collapses each subarray into a single boolean value: True if any element in the subarray differs, False otherwise.
  • .sum() counts the number of True entries in the mask—this is exactly the number of subarrays that don't match between a and b.

内容的提问来源于stack exchange,提问作者Codevan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:49:36