Click here to Skip to main content
15,908,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is train variable of list type, which conatains two elements - array of img type variables and the array of labels. I need to write the first array(of imgs) to X and then process it with reshape. I used NumPy library.

The code below is for understanding what train variable is.
Python
training_data.append([np.array(img), np.array(label)]) 
   ... 
 train = train_data[:-500]


I have tried to do it two ways.
First i have had the error:
TypeError: 'int'object is not subscriptable

For the second way i have had the error:
ValueError: setting an array element as a sequence

Update: the code, where training data list was create. The train is the slice of training_data
Python
def create_train_data(): 
    
    training_data = [1100] 
  
   
    for img in tqdm(os.listdir(TRAIN_DIR)): 
  
       
        label = label_img(img) 
  
        path = os.path.join(TRAIN_DIR, img) 
  
        training_data.append([np.array(img), np.array(label)]) 
  
        shuffle(training_data) 
  
       np.save('train_data.npy', training_data) 

    return training_data 

    train_data = create_train_data() 
    train = train_data[:-500]


There is what print(train) gives
https://ibb.co/RQ0H89r

What I have tried:

First way:
Python
X = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)


Second way:
Python
MyX=np.asarray( train[0])
X = MyX.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
Posted
Updated 23-Jun-20 18:46pm
v3
Comments
Richard MacCutchan 6-Jun-19 3:30am    
Add some print statements to your code so you can see exactly what is in train before you try to use it. The code you have shown is incomplete, and we have no idea what data exists in any of the arrays.

1 solution

I found this on the wb because was having the same error as you, it might help, take car and have a good day!

Numpy error – ValueError: setting an array element with sequence · World Hacks[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900