Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python shape (ValueError) error when trying to compute a derivative with numpy – diff

I’m interested on calculating a derivative using diff from numpy. For this, I use two arrays a and b with shape (N,) try to compute x

N=int(1e4)
a=np.ones(N)
b=9*np.ones(N)

x=np.zeros((N,1)) #shape (10000, 1)
for j in range(N):
    x[j]=a[j]/b[j]
y=3*x - 1

doty=np.diff(y)/np.diff(b)

I find a ValueError: operands could not be broadcast together with shapes (10000,0) (9999,).

Why does this happens?
How can I reshape b to match x?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

doty = np.diff(y.reshape(-1)) / np.diff(b)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading