如何将指定CSV转为两个numpy数组并调整其中数组的形状?
Got it, let's walk through exactly how to do this using the csv module and numpy. Here's a step-by-step implementation that fits your needs perfectly:
First, import the necessary modules:
import csv import numpy as np
Next, we'll read the CSV file, extract the l1 column and the p1 to p784 columns, then convert them into numpy arrays. Using csv.DictReader makes this clean because we can reference columns by their header names directly:
# Initialize empty lists to collect our data labels = [] pixel_data = [] # Replace 'your_data.csv' with the actual path to your CSV file with open('your_data.csv', 'r') as file: # Create a reader that uses the first row as column headers csv_reader = csv.DictReader(file) for row in csv_reader: # Grab the 'l1' value for our labels array labels.append(row['l1']) # Collect all p1 to p784 values, converting them to numbers (adjust dtype if needed) pixels = [float(row[f'p{i}']) for i in range(1, 785)] pixel_data.append(pixels) # Convert the lists to numpy arrays l_array = np.array(labels) r_array = np.array(pixel_data)
Let's verify the shapes:
l_array.shapewill return(27454,), which is your desired 1D label array.r_array.shapewill return(27454, 784), matching your original pixel data structure.
Finally, reshaping the r_array to (27454, 28, 28) is straightforward with numpy's reshape method. The -1 tells numpy to automatically calculate the first dimension (since 27454 * 28 * 28 equals exactly the number of elements in your original array):
r_reshaped = r_array.reshape(-1, 28, 28)
Check r_reshaped.shape and you'll see (27454, 28, 28)—ideal for image data!
A few quick notes:
- If your pixel values are integers instead of floats, replace
floatwithintin the list comprehension. - If you prefer
csv.readeroverDictReader, skip the header row first and index columns by position (e.g.,row[0]forl1,row[1:785]for pixels). - The default reshape order is row-major (
'C'), standard for most image formats, but you can addorder='F'if you need column-major ordering.
内容的提问来源于stack exchange,提问作者luckyCasualGuy

