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

如何完成NumPy一维数组转指定二维数组的两步变换?

Hey there! Let's work through how to solve this with NumPy—it's all about smart reshaping and stacking, which is way more efficient than looping through arrays manually. First, let's fix a small issue in your example code (you can't call .reshape() directly on a Python list) then break down both your sample scenario and your real-world use case.


Step 1: Walkthrough of Your Small Example

Let's start with your 16 arrays of 4 elements each:

import numpy as np

# Generate 16 1D arrays with 4 elements each
arr_list = [np.arange(4) for _ in range(16)]
# Convert to a NumPy array and reshape to 3D: 16 individual (2,2) arrays
arr_3d = np.array(arr_list).reshape(16, 2, 2)

Step 1: Merge Corresponding Rows Across All Arrays

We need to stitch together the first row of every (2,2) array into one long row, and the second row into another long row. Use np.concatenate for this:

# Stitch all first rows of the (2,2) arrays into a single long row
row0 = np.concatenate(arr_3d[:, 0, :])
# Stitch all second rows into another long row
row1 = np.concatenate(arr_3d[:, 1, :])
# Combine into the 2-row result you described
step1_result = np.vstack([row0, row1])

This gives you the shape (2, 32) array you wanted:

[[0 1 0 1 ... 0 1]
 [2 3 2 3 ... 2 3]]

Step 2: Split into Rows of Length 8

To split these two long rows into the 4-row result you showed, we'll use shape transformations to avoid manual looping:

# Reshape the 2-row array into (2, 4, 8) — split each long row into 4 chunks of 8
split_rows = step1_result.reshape(2, 4, 8)
# Transpose to reorder chunks: (4, 2, 8) — 4 groups, each with 2 rows of 8 elements
transposed = split_rows.transpose(1, 0, 2)
# Flatten into a 4-row array of 8 elements each
step2_result = transposed.reshape(4, 8)

This gives you exactly the output you described:

[[0 1 0 1 0 1 0 1]
 [2 3 2 3 2 3 2 3]
 [0 1 0 1 0 1 0 1]
 [2 3 2 3 2 3 2 3]]

Step 2: Scale to Your Real-World 416×416 Goal

First, let's confirm the numbers: your final array is 416×416 (total elements = 416*416 = 173056). Each starting 1D array has 64 elements, so you'll need 173056 / 64 = 2704 starting arrays (perfectly divisible, which makes reshaping clean).

Step 1: Merge Corresponding Rows

Assume you have a list arr_list containing 2704 1D arrays of length 64:

# Convert to 3D array: 2704 individual (8,8) arrays
arr_3d = np.array(arr_list).reshape(-1, 8, 8)
# Stitch together corresponding rows from all (8,8) arrays
step1_rows = [np.concatenate(arr_3d[:, row_idx, :]) for row_idx in range(8)]
# Combine into an 8-row array (each row is 2704*8 = 21632 elements long)
step1_result = np.vstack(step1_rows)

Step 2: Reshape to 416×416

Now we split each of the 8 long rows into 52 chunks of 416 elements (since 21632 / 416 = 52), then reorder the chunks to form the 416-row final array:

# Split each long row into 52 chunks of 416 elements: shape (8, 52, 416)
split_rows = step1_result.reshape(8, 52, 416)
# Reorder to group chunks together: shape (52, 8, 416)
transposed = split_rows.transpose(1, 0, 2)
# Flatten into the final 416×416 array
final_array = transposed.reshape(416, 416)

Quick Optimization Notes

  • All operations use NumPy's vectorized functions, which are way faster than Python loops for large datasets.
  • You can verify intermediate steps using the .shape attribute (e.g., final_array.shape should return (416, 416)).

内容的提问来源于stack exchange,提问作者john-mueller

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 19:37:49