I'm a beginner who has just started programming.
I'd like to set up a diagnostic imaging program, but I get the following error.
I'm using google collateral and I'm trying to upload some of them from kaggle's Chest X-Ray Images (Pneumonia) dataset to google drive for use.
I looked at other people's questions, but I couldn't keep up with them...
I would appreciate it if you could tell me how to solve it.
(I put np.append instead of append, but there was also an error.)
import numpy as np
import tensorflow as tf
from tensorflow import keras
import glob
x_train=[ ]
y_train=[]
x_test=[ ]
y_test=[ ]
for inglob.glob("/content/drive/MyDrive/kaggle/image/*/*.jpeg"):
img_data=tf.io.read_file(f)
img_data=tf.io.decode_jpeg(img_data)
img_data=tf.image.resize(img_data,(100,100))
if f.split("/")[6]=="train":
x_train.append(img_data)
y_train.append(int(f.split("/")[7].split("_")[0]))
eliff.split("/")[6]=="test":
x_test.append(img_data)
y_test.append(int(f.split("/")[7].split("_")[0]))
x_train=np.array(x_train)/255.0
y_train=np.array(y_train)
x_test=np.array(x_test)/255.0
y_test=np.array(y_test)
AttributeError Traceback (most recent call last)
<ipython-input-75-44f0c41d960e>in<module>()
18y_train.append(int(f.split("/")[7].split("_")[0]))
19 eliff.split("/")[6]=="test":
--- >20x_test.append(img_data)
21y_test.append(int(f.split("/")[7].split("_")[0]))
22
AttributeError: 'numpy.ndarray' object has no attribute'append'
When I looked closely at the coding work image of the video introduced, I saw that the indentation of the part was reduced and moved out of the for
loop for a moment.
19:00
·20:26
neighborhood or 21:52
or later
Therefore, it will work if you remove the indentation from the for
loop.
x_train=np.array(x_train)/255.0
y_train=np.array(y_train)
x_test=np.array(x_test)/255.0
y_test=np.array(y_test)
Simply do this
x_train=np.array(x_train)/255.0
y_train=np.array(y_train)
x_test=np.array(x_test)/255.0
y_test=np.array(y_test)
The following is the source code before the video was introduced.
Just in case something else happens, I'll leave it as a precaution.
I'm not sure if the link to the model article or source code information will be added, but from the current source code, the problem is that the following code has converted the variables defined as Python's basic list into numpy.ndarray.
For example, it is similar to this article.
Understanding Errors in 'numpy.ndarray' object has no attribute 'append'
Declaring and Initializing as a List
x_train=[]
y_train=[]
x_test=[ ]
y_test=[ ]
Processing in for loop converts to ndarray
x_train=np.array(x_train)/255.0
y_train=np.array(y_train)
x_test=np.array(x_test)/255.0
y_test=np.array(y_test)
Therefore, the following process works fine for the first time in the for loop, but after the second time, it is an error because it is ndarray instead of a list.
if f.split("/")[6]=="train":
x_train.append(img_data)
y_train.append(int(f.split("/")[7].split("_")[0]))
eliff.split("/")[6]=="test":
x_test.append(img_data)
y_test.append(int(f.split("/")[7].split("_")[0]))
Either declare/initialize as ndarray from the beginning and use np.append, or rewrite the process of converting the entire list in the second half of the loop to ndarray to convert only the necessary data to ndarray.
© 2025 OneMinuteCode. All rights reserved.