繁体   English   中英

Python AttributeError: 'numpy.ndarray' object 没有属性 'append'

[英]Python AttributeError: 'numpy.ndarray' object has no attribute 'append'

我正在尝试解析包含 csv 文件的文件夹(这些 csv 文件是像素图像位置)并将它们存储到 numpy 数组中。 当我尝试执行此操作时,出现错误: AttributeError: 'numpy.ndarray' object has no attribute 'append' 我知道 NumPy arrays 没有append()

但是在我的代码中我使用了方法: images.append(img)

你能告诉我我做的不好吗?

这是我的代码:

# Create an empty list to store the images
images = []

# Iterate over the CSV files in the img_test folder
for file in os.listdir("img_test"):
    if file.endswith(".txt"):
        # Read the CSV file into a dataframe
        df = pd.read_csv(os.path.join("img_test", file), delim_whitespace=True, header=None, dtype=float)

        # Convert the dataframe to a NumPy array
        image = df.to_numpy()

        # Extract the row and column indices and the values
        rows, cols, values = image[:, 0], image[:, 1], image[:, 2]

        # Convert the row and column indices to integers
        rows = rows.astype(int)
        cols = cols.astype(int)

        # Create a 2D array of the correct shape filled with zeros
        img = np.zeros((1024, 1024))

        # Assign the values to the correct positions in the array
        img[rows, cols] = values

        # Resize the image to 28x28
        img = cv2.resize(img, (28, 28))

        # Reshape the array to a 3D array with a single channel
        img = img.reshape(28, 28, 1)

        # Append the image to the list
        images.append(img)

    # Convert the list of images to a NumPy array
    images = np.concatenate(images, axis=0)

在外部 for 循环的末尾,您将图像从列表转换为 NumPy 数组

images = list()
for file in os.listdir("img_test"):
   if file.endswith(".txt"):
       ...
   images = np.concatenate(images, axis=0) # not a list anymore

您可能未对齐连接并希望在 for 循环结束后执行此操作。

最后一行的缩进是错误的。 您可能希望在 for 循环结束后连接

# Create an empty list to store the images
images = []

# Iterate over the CSV files in the img_test folder
for file in os.listdir("img_test"):
    if file.endswith(".txt"):
        # Read the CSV file into a dataframe
        df = pd.read_csv(
            os.path.join("img_test", file),
            delim_whitespace=True,
            header=None,
            dtype=float,
        )

        # Convert the dataframe to a NumPy array
        image = df.to_numpy()

        # Extract the row and column indices and the values
        rows, cols, values = image[:, 0], image[:, 1], image[:, 2]

        # Convert the row and column indices to integers
        rows = rows.astype(int)
        cols = cols.astype(int)

        # Create a 2D array of the correct shape filled with zeros
        img = np.zeros((1024, 1024))

        # Assign the values to the correct positions in the array
        img[rows, cols] = values

        # Resize the image to 28x28
        img = cv2.resize(img, (28, 28))

        # Reshape the array to a 3D array with a single channel
        img = img.reshape(28, 28, 1)

        # Append the image to the list
        images.append(img)

    # Convert the list of images to a NumPy array
    cobmined_images = np.concatenate(images, axis=0)

您正在将图像初始化为列表。 然后您将创建具有相同名称的 numpy 数组。 所以在第二次迭代中图像是 numpy 数组所以它不会有 append 属性。尝试像我一样使用不同的名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM