1

I have two Numpy array whose size is 994 and 1000. As such I when I am doing the below operation:

X * Y

I get error that "ValueError: operands could not be broadcast together with shapes (994) (1000)"

Hence as per fix I am trying to pad extras / trailing zeros to the array which great size by below method:

padzero = 0
if(bw.size > w.size):
    padzero = bw.size - w.size
    w = np.pad(w,padzero, 'constant', constant_values=0)
if(bw.size < w.size):
    padzero = w.size - bw.size
    bw = np.pad(bw,padzero, 'constant', constant_values=0)

But now the issue comes that if the size difference is 6 then 12 0's are getting padded in the array - which exactly should be six in my case.

I tried many ways to achieve this but its not resulting to resolve the issue. If I try he below way:

bw = np.pad(bw,padzero/2, 'constant', constant_values=0)

ValueError: Unable to create correctly shaped tuple from 3.0 

How can I fix the issue?

1 Answer 1

1

a = np.array([1, 2, 3])

To insert zeros front:

np.pad(a,(2,0),'constant', constant_values=0)
array([0, 0, 1, 2, 3])

To insert zeros back:

np.pad(a,(0,2),'constant', constant_values=0)
array([1, 2, 3, 0, 0])

Front and back:

 np.pad(a,(1,1),'constant', constant_values=0)
 array([0, 1, 2, 3, 0])
Sign up to request clarification or add additional context in comments.

Comments

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.