0

I want to save a value into an array using numpy. All is working fine but when I try to save the values in the array I am getting an error. Here is what I've done so far:

import numpy as np
batchSize = 2
numClasses = 2
maxSeqLength = 10 #Maximum length of sentence

labels = []
arr = np.zeros([batchSize, maxSeqLength])

for i in range(batchSize):
    num = randint(1,17)
    labels.append([1,0])
    # print(num)
    print(labels)
    print(ids[num-1:num])
    arr[i]=ids[num-1:num]

When I print ids matrix I am getting these values

[[1, 0]]
[[   nan 11501.   420.  5842.    nan  4245.    nan     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]]
[[1, 0], [1, 0]]
[[   nan    nan  7624.  3936. 11501.  8064.  5842.    nan    nan  1087.
     37. 11516.    nan 10936.    nan 12111.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]]

and this is the error I am getting when saving the arr array

could not broadcast input array from shape (30) into shape (10)
6
  • You define numClasses but don't ever use it. You use maxSeqLength but it is never defined. Commented Oct 17, 2018 at 16:12
  • He might have defined but, did not show it here. If he did not define it how is he printing those? Commented Oct 17, 2018 at 16:15
  • noofClasses is binary 1 0 represents positive classes Commented Oct 17, 2018 at 16:20
  • The error is pretty clear, you're trying to insert a array of 30 elements into an array of 10 elements. It means arr[i] has maxSeqLength while ids[num-1:num] has a different length. Commented Oct 17, 2018 at 16:20
  • @RockyLi Can you make an answer please. Commented Oct 17, 2018 at 16:21

1 Answer 1

2

The error is pretty clear, you're trying to insert a array of 30 elements into an array of 10 elements. It means arr[i] has maxSeqLength=10 while ids[num-1:num] has a different length=30

You can fix this by setting maxSeqLength=30 before you generate the np.zero matrix.

Sign up to request clarification or add additional context in comments.

4 Comments

Should i exceed the max lenth to 30?
when i have set to maxlenth=30 i am getting 0 on every index of array
like this [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
values are not saving i guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.