• Register
1 vote
40 views

Problem:

I was doing my coding practice of python using numpy. I wrote this code:

def create_traini_data(): 
    
    trainingday_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(trainingday_data) 
  
       np.save('traini_data.npy', training_data) 

    return training_data 

    traini_data = create_traini_data() 
    traini = traini_data[:-400]

It throws error:

ValueError: setting an array element with a sequence

Can anybody help?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

You can solve this in two ways . The first way is:

X = np.array([i[0] for i in traini]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

And the secoend way is:

MyX=np.asarray( traini[0])
X = MyX.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 8 views
8 views
Problem: Throws this error message: File "mypath\mypythonscript.py", line 3484, in secondfunction RRDuringArray = p.array(UnFilteredDuringExSummaryOfMeansArray,dtype=p.float64)[1:,3] ValueError: setting an array element with a sequence. Can anyone show me what to do to fix the problem ... error message? EDIT: I did a print command to get the contents of the matrix, and this is what it printed out:
asked Feb 18 Mashhoodch 9.6k points
0 votes
1 answer 746 views
746 views
Problem : I am having below error in a title, and I don't know what's wrong. It was working when I used np.hstack instead of the np.append, but I want to make this faster, so using append. time_list the list of floats heights is the 1d np.array of ... <module> axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))]) TypeError: only integer arrays with one element can be converted to an index
asked Jan 24, 2020 jwilliam 3.9k points
0 votes
1 answer 40 views
0 votes
1 answer 65 views
65 views
Problem: Can any one help me in array programming? What happen when an individual element of an array is passed to a method?
asked Feb 22, 2020 maddi86 5.4k points
0 votes
1 answer 3 views
3 views
Problem: I want to define a 2D Array in Python without an initialized length. I've tried the code below, but it is wrong too: Matrix = [5][5]
asked 4 days ago Ashty 1.7k points
0 votes
2 answers 1.7K views
1.7K views
Problem : I got following rather cryptic error message: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
1 answer 47 views
47 views
Problem: Hi there! I am a new learner. I want to know how I can find the position of an element given by the user in an array. I tried some programs but could get the logic. The element, whose position is to be found, will be taken as input from the ... has to choose any element of that array. Can anybody here help me understand the logic of the program? Example program will be a plus. Thanks!
asked Jan 2 Code Learner 6k points
0 votes
1 answer 18 views
18 views
Problem: How to get most occurring element in an array. It is working well but the only problem is when there are two or more elements having the same number of occurrence? and equal to most occurring element, it just shows the first element scanned. solve this anyone.
asked Dec 23, 2020 Mashhoodch 9.6k points
0 votes
1 answer 1 view
1 view
Problem: How to fix this error: index of element in array python HELP! I need help understanding this error: index of element in array python Can someone please help fix this, I am new in python().
asked 17 hours ago Mashhoodch 9.6k points
0 votes
1 answer 28 views
28 views
Problem: Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python? list.index(x[, start[, end]]) Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.
asked Jan 10 Mashhoodch 9.6k points