336 points · by rbanffy · 5y ago · 106 comments
EDIT: I'm always very surprised by the wide acceptance of python/numpy for numerical algorithms, as it seems a notion quite foreign and awkward for this language. Python has excellent native support for string processing and advanced data structures like dictionaries, but those are mostly useless for numerical computation. If you want to do math in Python, you need to write libraries in other languages and provide a Phython interface. This is what scipy does, and it is a very good job, but it does not feel "natural" from the point of view of the language. If Python was a good scientific programming language, the most natural way to implement scipy/numpy and all that would be using Python itself, but this is not at all the case.
Unfortunately most undergraduates get locked in early with free student versions and continue to use it in industry. Currently I’m trying to expose my students to both Matlab and Python/numpy in a matter of fact manner.
You can do algorithm development in MATLAB or R or Julia or whatever and figure out ways to either translate the logic to Python/C++ later or setup some kind of IPC for production software engineers to call (platform/orchestration stuff is usually Python or Java)...
...OR you can just develop your algorithms in Python and keep code comprehension and platform integration much simpler for everyone
What I've noticed is that the ones who have gained some proficiency in programming can switch to Python in a jiffy. So I don't think it's a life or death choice.
The use of Python in my work group is largely as a problem solving and prototyping tool. The product development teams that we serve have their own software departments, with their own choices of languages and so forth.
Not to mention the IDE is actually very good, and you almost never have to deal with package management etc.
Why no octave? Or julia?
Goes far beyond that
>So, there’s a total of three types of vectors in NumPy: 1D arrays, 2D row vectors, and 2D column vectors.
I think you can have 'vectors' of arbitrarily high dimension, by adding empty axes, or doing, say, .reshape(-1, 1, 1, 1).
I would also tie this point with the rule for broadcasting that dimensions which do not match need to have a value of one. I'm endlessly confused about broadcasting, and find the explicit rule useful.
This is fine, because with high numbers of dimensions you really can't afford to store a dense-matrix representation in RAM and 32 is plenty for any low-dimensional problem.
> Yes, broadcasting is a cool thing, but the docs mostly explains 'how' but not 'why' or 'what for'.
I'd say that, even for the 'how', thinking "OK, I have a [1, 1, 2, 2, 1] and a [3, 2, 1, 2, 5] arrays, which means that element-wise multiplication would result in a [3, 2, 2, 2, 5] array" is very useful in reasoning about broadcasting.
Just relying on simple examples and trying to build an intuition for the general case (which is the route favored in the numpy docs, I'd say) doesn't quite work, at least for me.
> The value of the axis argument is, as a matter of fact, the number of the index in question: The first index is axis=0, the second one is axis=1, and so on
If I'm not wrong - this statement is wrong - the axis does not represent index (or otherwise it would be axis 0 = row, axis 1 = column). The actual idea behind is a little unintuitive, but explained well here: https://aerinykim.medium.com/numpy-sum-axis-intuition-6eb949...
The axis argument gives the index of the axis along which your summate, and which therefore disappears after summation.
There might be a confusion about whether "axis 0" is rows or columns? Its length is the number of rows, but it "points" along columns.
I prefer just calling them axis 0, 1, 2, ... and avoid thinking about rows and columns in numpy, I've found that sometimes avoids confusion.
On the other hand einsum is Crystal clear and not prone to confusion.
> np.einsum("ij -> j", B) # sum along rows to create one column-like array
> np.einsum("ij -> i", B) # sum along columns to create one row-like array
Edit: More Einstein sum fun at https://stackoverflow.com/questions/26089893/understanding-n...
Both of them are of interest and after you were confused once about which one to supply to axis=..., there is no way back to clear the confusion. With einsum there is no confusion.
But why not use whatever suits your problem the best? I use both Python and Julia in my research, and sometimes R when I need some niche statistical packages. "My language is superior to your language" is such a self-limiting mindset.
People are excited about Julia, because frankly it's amazing and a breath of fresh air coming from python. Also, it's really now starting to pick up steam.
Curious though, how are limited by using Julia? Any python or r package is a pycall/rcall away.
Otherwise it's faster, more ergonomic and makes coding fun again
Personally I don't quite get why something like
a[a > 5] = 0
is possible (a is NumPy array).
I think it is confusing to use same name for element wise comparison and the array itself. Are comparison operators overloaded to generate this kind of predicate function or view in other contexts as well or is this some sort of special case that is handled by indexing (__getitem__ call)?
I also found it incredibly confusing the first time I saw this, but like all the numpy magic based on overloading, it’s confusing the first time you see it, but incredibly handy afterwards.
a[lambda i: i > 5] = 0
Probably would expect something like this
np.set(a, lambda i: i > 5, 0)
But that might be too cumbersome when combining values from multiple arrays.
[0] https://numpy.org/doc/stable/reference/random/index.html
Anyone have any idea why? I imagine if they could support it they would support it. I wonder if Python doesn't allow this kind of operator overloading?
https://www.python.org/dev/peps/pep-0335/
and deferred PEP for rich comparison chaining specifically
https://www.python.org/dev/peps/pep-0535/
The central issue is that `3<=a<=5` expands to `3<=a and a<=5`, and `and` coerces the variables to booleans in order to work.
>https://www.python.org/dev/peps/pep-0535/
Oh nice! Thank you for sharing. I'm sure the functionality will eventually come.
My response is that, in those cases, go ahead and declare the array you want to catch the result (and it’s shape), then use [:] etc. appropriately to ensure that if the size doesn’t match between computed and expected at the point of assignment, it will fail.
Edit: though now I see it is brought up later on in the matrix section.
For example, if I have a 2-dimensional array and I insert a scalar value by using axis=0, the scalar value needs to be broadcasted to match the column dimension of the array:
>>> a = np.array([[1,2],[3,4],[5,6]])
>>> np.insert(a, 1, 11, axis=0)
array([[ 1, 2],
[11, 11],
[ 3, 4],
[ 5, 6]])
[0] https://jakevdp.github.io/PythonDataScienceHandbook/02.05-co...This isn't true. I just tested it with Javascript disabled. After about 5 articles, the text is greyed out and you can't read the rest of of the article:
1. Taking screenshots of code.
2. Taking screenshots of code and putting the array values inside of blue boxes.
It’s so much more than that. This is not what HN community is about.
3. A thought-provoking text in between, but apparently didn't hit the target in your case.