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

如何将指定CSV转为两个numpy数组并调整其中数组的形状?

Solution to Convert CSV Data to Numpy Arrays and Reshape

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.shape will return (27454,), which is your desired 1D label array.
  • r_array.shape will 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 float with int in the list comprehension.
  • If you prefer csv.reader over DictReader, skip the header row first and index columns by position (e.g., row[0] for l1, row[1:785] for pixels).
  • The default reshape order is row-major ('C'), standard for most image formats, but you can add order='F' if you need column-major ordering.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 11:02:48