Python 3.6下使用Numpy将二维数组转换为三维数组的技术问题
Hey there, let's break down what's going on with your reshaping step and get this sorted out. First, let's start with the most common pitfalls when converting 2D merged data to a 3D array in NumPy.
First: Verify Your Input Data's Consistency
The biggest issue with reshaping is almost always a mismatch between the total number of elements in your 2D array and the dimensions you're trying to reshape into. Let's start by checking that:
Add these print statements right before your np.reshape line to debug:
print(f"Combined data shape: {data.shape}") print(f"Expected total elements for (37,6,11): {37*6*11}") print(f"Actual total elements: {data.size}") print(f"Number of data files: {len(data_files)}")
You need two things to be true here:
len(data_files)must be exactly 37 (since your target shape's first dimension is 37)data.sizemust equal37*6*11 = 2442
If either of these is off, your problem starts earlier:
- If the file count is wrong: Double-check your
globpattern—are there extra/missing.TXTfiles in your directory? - If element count is wrong: Add a print inside your loop to check each file's data shape:
This will tell you if some files have more/fewer rows than expected (I'm guessing each file should have 11 rows of 6 columns, since 37*11=407 rows total in your merged 2D array).for i in data_files: item=np.genfromtxt(i,skip_header=3) print(f"File {i} shape: {item.shape}") # Check if every file has the same shape data=np.r_[data,item]
Fixing the Reshaping Logic
If your element counts check out, the issue is likely how order="F" is arranging your data. Fortran-order (column-first) reshaping can be counterintuitive if you're used to NumPy's default C-order (row-first).
Instead of merging everything into a 2D array first (which can muddle the structure), a cleaner approach is to build your 3D array directly by stacking each file's data:
import numpy as np import os import glob os.chdir('/Users/basilowen/Documents/Physics Degree/Fourth Year /Project/dielectric raw data/B22 full temp scan') data_files = glob.glob('**/*.TXT',recursive=True) data_files.sort() # Initialize a list to hold each file's data as a 2D array data_slices = [] for file_path in data_files: # Read each file, skip header rows file_data = np.genfromtxt(file_path, skip_header=3) # Verify shape (adjust if your files have a different structure) assert file_data.shape == (11,6), f"File {file_path} has unexpected shape {file_data.shape}" data_slices.append(file_data) # Stack the list into a 3D array: (number_of_files, rows_per_file, columns_per_file) data_3d = np.stack(data_slices, axis=0) # If you specifically need the shape (37,6,11) instead of (37,11,6), transpose the last two axes data_3d = data_3d.transpose(0, 2, 1) print(f"Final 3D array shape: {data_3d.shape}")
This approach is more readable and gives you direct control over how your dimensions map to the original files. The transpose step swaps the row and column dimensions from each file to get your target shape.
Why Your Original Reshape Might Be Misbehaving
If you still want to use your original merged 2D approach, let's clarify what order="F" does. Your merged data is a (407,6) array (37 files × 11 rows each). When you reshape to (37,6,11) with order="F", NumPy fills the array column-first:
- It takes the first column of your 2D data and fills the first "column" of the 3D array across all 37 files and 11 positions.
This is almost certainly not the structure you want (you probably want each file's data to stay grouped together in the first dimension).
Using the stacking method above avoids this ambiguity entirely.
内容的提问来源于stack exchange,提问作者Basil Owen

