New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation for numpy.fromfunction induces an erroneous interpretation! #15726
Comments
There is a misunderstanding to fromfunction, you think that f(x,y) is called multiple times in each coordinate iteration, But actually, f(x,y) is invoked only once.The input parameter is the coordinate array of each dimension. in your case,
so the result is correct. |
I actually do think the docstring is a bit misleading:
The last sentence seems to be incorrect - if the function returns a scalar value, the shape of >>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]]) |
@rossbar may be this is more precise ? what do you think.
also we can add anoldmaninthesea's example to docstring to demostrate this situation. |
FYI: I've seen this confusion before, in a question on stackoverflow: https://stackoverflow.com/questions/27612288/unexpected-result-numpy-fromfunction-with-constant-functions |
@rossbar, that example is one where the function does not return a scalar value (because |
I would be pleased with a change in the documentation. The fromfunction can be a useful method as is... It's just the documentation that seems to induce the user into a mistake. |
I see - then this was definitely confusing to me. Whether the function It is additionally confusing in the context of the original example Thanks for clearing this up @eric-wieser , though I must say without your expert intervention I was clearly getting the wrong idea from the docstring itself. Also thanks @WarrenWeckesser for the SO link. I think incorporating part of your explanation into the docstring would go a long clearing up (at least my) issues:
|
@Qiyu8 I think adding an example of a Lines 1766 to 1767 in 68224f4
|
In addition to this, would it be a good idea to have a function that does what we expect |
I have another complaint. Consider the following example:
Then NumPy throws a ValueError:
So |
You should be using |
Hi, I'm having similar issues which might be related to this misunderstanding of the use of I want to accelerate the costruction a a matrix like this:
Using
The matrix has the right size, but the content is kind of random and is not equal to the los matrix computed manually. Am I misunderstanding the usage of the function or there's an actual bug? |
It seems that both broadcasting and vectorization are taking place in calling the supplied function, as in the example code at the top of this issue, same as in the following example: def distance(a ,b):
print('I have been called')
return abs(a - b)
def fill(stream: np.array, query: np.array):
return np.fromfunction(
lambda i, j: distance(stream[i], query[j]),
(len(stream), len(query)), dtype=int)
fill(np.array(range(3)), np.array(range(10))) The best way to understand it is looking at the source code of it, So for example in the above code, the lambda is called once, receiving the array of indices implied from the shape as a single numpy array, which is what Observing that To sum it up, a function performing arithmetic ( And it happens to be that despite this "plan of execution" assigning and using orders of magnitude more main memory for the matrices |
As it is actually related back to the documentation, when benchmarking, Is it really the case as its docs page says that ―
It seems that the use of the term vectorization in np.vectorize's documentation goes against numpy's documentation glossary of the term vectorization. I think either the documentation should be harmonized on this, or maybe more agressive compiler flags should be used inside As is, the flow for learning how to leverage performance vectorization ― around one's own python functions performing arithmetics ― feels almost trecherous as far as documentation and function names are concerned. Perhaps this can be made more enabling by more consistently harmonizing the documentation regarding the term It seems that numba have developed a pair of further vectorizing versions of the vectorize function concept, but I find that it will never be stable up until the time that numba becomes a core part of numpy, I have encountered numerous stability concerns (bad luck?) and can only guess that without a tight integration in the development and release cycles it's going to be shaky. |
The documentation related to numpy.fromfunction states:
However, when I run the following:
f=lambda m,n: (m,n)
np.fromfunction(f,(6,6),dtype=int)
I don't obtain an array from a list of tuples, but instead this:
The text was updated successfully, but these errors were encountered: