question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gradient() fuction for bi-dimensional array returns first in y direction and then in x

See original GitHub issue

Seems the return of gradient() function, for a two dimensional array, is changed compared to matlab. The biggest problem is it’s not documented.

See, in MATLAB:

x = [1,2,3; 4,5,6];
[gx, gy] = gradient(x)

gx =
     1     1     1
     1     1     1

gy =
     3     3     3
     3     3     3

Now, in NumPy:

x = np.array([[1,2,3], [4,5,6]])
[gx, gy] = np.gradient(x)

gx
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.]])

gy
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

As we can see, the return of gradient() at NumPy is not in x direction and later in y. It is in y direction and then x. In my opinion that is ok this change. However, when you are trying to port a large code from matlab to python, like me, this behavior can be a pain. Also, the return is not well explained at docs, since there is only shown the gradient with a one-dimensional array.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rcatwoodcommented, Jul 11, 2018

Sorry but I still find the documentation that I can find, ambiguous. What does it mean to say ‘axis=0 is rows’ ? does that mean that the value of the coordinate, 0, changes as you move along a row or changes as you move across a row? likewise what is the gradient 'in the columns direction? is it dU/dx0 or dU/dx1 ?

It doesn’t make sense to me or agree with what I obtain . Here is a 2-dimensional dataset. xyplane [[ 0. 6. 12. 18. 24. 30.] [ 1. 7. 13. 19. 25. 31.] [ 2. 8. 14. 20. 26. 32.] [ 3. 9. 15. 21. 27. 33.] [ 4. 10. 16. 22. 28. 34.] [ 5. 11. 17. 23. 29. 35.]]

Clearly the gradient ‘in rows’ is 6 everywhere.

So if grad=numpy.gradient(xyplane) and it says that grad[0] is the gradient ‘in rows’ then it should be 6 everywhere. however it is 1, which is the gradient ‘in columns’. grad[0] [[ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.]] grad[1] [[ 6. 6. 6. 6. 6. 6.] [ 6. 6. 6. 6. 6. 6.] [ 6. 6. 6. 6. 6. 6.] [ 6. 6. 6. 6. 6. 6.] [ 6. 6. 6. 6. 6. 6.] [ 6. 6. 6. 6. 6. 6.]]

Could someone clarify in terms of mathematical symbols e.g. partial derivative of value with respect to coordinate zero , coordinate 1, etc. ? Dispense with mentioning ‘row’ and ‘column’ because the screen output is rather arbitrary?

0reactions
eric-wiesercommented, May 3, 2020

This problem is one only of documentation. The docs were improved, but it sounds like not by enough.

In your example, the problem is actually that meshgrid behaves inconsistently with gradient. Perhaps the gradient docstrinng should call that out as a common mistake in the examples section.

meshgrid(indexing='ij') would fix that example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy gradient function for bidimensional arrays returns first ...
Numpy gradient function for bidimensional arrays returns first the gradient component in y direction, and then in x direction.
Read more >
The gradient vector | Multivariable calculus (article)
The gradient stores all the partial derivative information of a multivariable function. But it's more than a mere storage device, it has several...
Read more >
Numerical gradient - MATLAB gradient - MathWorks
Numerical gradients, returned as arrays of the same size as F . The first output FX is always the gradient along the 2nd...
Read more >
Week 5: The image gradient
Now, if the function I(x,y) was continuous, what is the intensity I(x,y) along the direction θ? So, we are really asking what is...
Read more >
numpy.gradient — NumPy v1.24 Manual
For two dimensional arrays, the return will be two arrays ordered by axis. In this example the first array stands for the gradient...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found