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

移位numpy数组的线性和及特定尺寸numpy数组构造技术问询

How to Compute Shifted Linear Sums to Build a (m-1, n-1) NumPy Array from a (m, n) Array

Got it, let's break this down clearly. You've got a (m, n) NumPy array A, and you need to create a smaller (m-1, n-1) array B where each B[i,j] is a linear sum of shifted elements from A. I'll start with the most common 2x2 neighborhood sum case (since that's the typical scenario for this shape reduction), and show you how to adapt it for any linear sum rule.

1. Clarify the Shifted Sum Logic

First, let's define the calculation with an example. For every valid index i (0 ≤ i ≤ m-2) and j (0 ≤ j ≤ n-2), we'll assume:

B[i,j] = A[i,j] + A[i,j+1] + A[i+1,j] + A[i+1,j+1]

This is the sum of the 2x2 block in A starting at position (i,j). If your linear sum uses weighted coefficients (like averages or custom weights), we can adjust this easily later.

2. Efficient Implementations with NumPy

NumPy is built for vectorized operations—no slow Python loops needed. Here are two solid approaches:

Approach 1: Slice Indexing (Simple & Fast)

This is the most intuitive method for straightforward sums. We slice four shifted versions of A and add them together:

import numpy as np

# Example (3,3) input array
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

# Calculate B using sliced arrays
B = A[:-1, :-1] + A[:-1, 1:] + A[1:, :-1] + A[1:, 1:]

print(B)
# Output:
# [[12 16]
#  [24 28]]

Let's break down the slices:

  • A[:-1, :-1]: All rows except the last, all columns except the last (top-left quadrant)
  • A[:-1, 1:]: Top rows, columns shifted right by 1 (top-right quadrant)
  • A[1:, :-1]: Rows shifted down by 1, left columns (bottom-left quadrant)
  • A[1:, 1:]: Rows down, columns right (bottom-right quadrant)

Approach 2: Convolution (For Custom Weights)

If your linear sum uses non-uniform weights (e.g., 0.25*A[i,j] + 0.75*A[i+1,j+1]), convolution is the way to go. For the 2x2 sum case, we use a kernel of ones:

from scipy.signal import convolve2d

# Define your weight kernel (adjust values for custom linear sums)
kernel = np.ones((2, 2))

# Compute convolution with "valid" mode to get (m-1, n-1) shape
B = convolve2d(A, kernel, mode="valid")

print(B)
# Same output as above:
# [[12 16]
#  [24 28]]

The mode="valid" parameter ensures we only keep results where the kernel fully overlaps with A—exactly the shape we need.

3. Adapt to Your Specific Linear Sum

If your rule isn't the 2x2 sum, just tweak the slices or kernel. For example:

  • If B[i,j] = 2*A[i,j] + 3*A[i+1,j+1]:
    B = 2 * A[:-1, :-1] + 3 * A[1:, 1:]
    
  • If you need a weighted average:
    kernel = np.array([[0.1, 0.2], [0.3, 0.4]])
    B = convolve2d(A, kernel, mode="valid")
    

4. Performance Tips

  • Slice indexing is faster for simple sums since it avoids convolution overhead.
  • Convolution is better for complex weight patterns or larger kernels (like 3x3 or bigger).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:53:01