Live data from Hacker News

From Python to Numpy

labri.fr

41–50 of 55 posts

Re: From Python to Numpy

#41
post #11

As someone who has used numpy for many years and written a great deal of production code using it, I was surprised when I read through this and saw some numpy tricks that I didn't know regarding the speeds of various operations! This is really a fantastic reference that provides a deeper level of understanding of what numpy does under the hood. One thing I will highlight that the author just touched on briefly, is th…

[deleted]

Re: From Python to Numpy

#42
post #11

As someone who has used numpy for many years and written a great deal of production code using it, I was surprised when I read through this and saw some numpy tricks that I didn't know regarding the speeds of various operations! This is really a fantastic reference that provides a deeper level of understanding of what numpy does under the hood. One thing I will highlight that the author just touched on briefly, is th…

Just to jump on the numba train, I've generally found it to reliably obtain C like performance from C-like Python code. This property also holds when you use Python as a preprocessor language for generating computational kernels, which provides a lot of flexibility not evident in the documentation. It also has simple-to-use openmp-like multicore parallelization, limited class support, AOT compilation and CUDA & AMD H…

Are you using python as a preprocessor to glue together text strings of numba code which you then eval? Or are you using python to generate the numba AST?

Re: From Python to Numpy

#43

I'd be very curious to know if there is any impact to choosing Numpy C ordered arrays or Fortran ordered arrays. As a long time Matlab user (since 1993) who moved to Python 3 years ago, I have always defaulted to Fortran order because it was what I was used to and seemed more intuitive. I did play with C ordered arrays but didn't find an advantage in my limited investigation.

I think it depends on whether an algorithm is forced to traverse an array in the cache-efficient direction or not. Oftentimes you can't choose whether to make your outer loop rows or columns so the performance could go either way.

Re: From Python to Numpy

#44
post #5

Earlier quoted context omitted.

I don't mind empty margins, but having 60% of my screen empty for no reason is excessive, and I do read slower because of it. If I want shorter lines, I'll just resize my browser, thanks. I agree with you about the font being too thin, but the line length annoyed me much more.

Optimizing for super-rare users who have a deliberate desire for long lines is less important than optimizing for the vastly-more-common case that the browser's size is large as an artifact of content the user was viewing on a previous site, but the user still prefer a sane line length for text content.

On the flip side you can use Reader Mode to make long lines shorter, but you can't use Anti-Reader Mode to make short lines longer.

Re: From Python to Numpy

#45
post #11

As someone who has used numpy for many years and written a great deal of production code using it, I was surprised when I read through this and saw some numpy tricks that I didn't know regarding the speeds of various operations! This is really a fantastic reference that provides a deeper level of understanding of what numpy does under the hood. One thing I will highlight that the author just touched on briefly, is th…

Can I ask how intensively you have used Numba and over what period? I'm interested in how Numba has progressed over the last few years, with a view to using it over Cython. My team and I looked at Numba a year ago or so for optimisation of a fairly large calculation, and found that the speed-ups were impressive where they worked, but were not consistent or predictable. We used Cython for large parts, and while there…

Sure. I've used numba for the past 1 1/2 years, and I've seen it grow quite a bit. When I first started using it, there was a separate product called numbapro that did all of the gpu jit, which they've now included in numba for example.

Regarding whether it would be appropriate vis-a-vis cython really depends on your application

First, Cython is fantastic as well, and my endorsement of numba doesn't take anything away from it. Cython is much more fully featured and mature, in the sense that you can really develop your own data structures and control flows. Pretty much anything you could do in C, you could do in Cython. I've written Cython and it also plays very nicely with numpy.

In comparison, Numba is much more limited. You are basically limited to using numpy arrays and matrixes as your data structures, and you really need to understand exactly what is going to be used prior to the jit loop or you won't be able to use it in nopython mode (which is where you get the most benefit). It also doesn't handle strings really at all. One fairly recent thing Numba does is allow you to use a list of a single type within nopython mode. Under the hood it handles the malloc for you.

My endorsement of Numba really boils down to ease of integration with existing python codebase. For me the "killer feature" was the ability to simply comment out the @jit or @njit decorator and step through the code like I would step through normal python code, then just turn it back on again when I needed it. The other was that numba gained the ability early in our adoption to chain functions together, so while you can't generate a numpy array in the nopython mode, for instance, you can generate a numpy array in a @jit function (object mode) outside of the nopython mode, then call the looping function (nopython mode) from that jitted function, and numba handles that seamlessly and cuts out a lot of the overhead. For us, our speed of development of a custom algorithm has really been helped greatly by Numba.

The other thing I will mention is that when I first started, getting LLVM to work with numba was, initially, a nightmare on different OS's. That has completely gone away with improvements in conda package manager now.

All that said, you cannot go wrong with Cython, it just has a little more of a learning curve and was a little tricker to implement in our codebase.

>> once we decided that Cython was our go-to tools, we often wrote simple looping code rather than vectorised code because it was simpler to transition to Cython, alá Julia.

If you're used to doing this with cython, you might find it even easier to do this with numba. This is how I develop all the time with numba now. I find that it's incredibly beneficial to step through it as though it was just regular python initially during algorithm and test development, then once the algorithm is right and tests pass, turn on the jit when ready. You sort of get a sense for what numba will accept and still have performant no-python mode jitting after a while, and knowing those limitations actually tends to cause me to write more modular code to take advantage of the speed boosts.

Re: From Python to Numpy

#46
post #26

Earlier quoted context omitted.

Citation that these users are super rare? Almost everyone I've had conversations with on the subject bemoan too large margins on websites.

That's odd. I wonder if these people that like looong lines are mostly younger people that have read more from screens than books (but not so young that they've read more from phones than computer screens :))? A book with very long lines would be super weird.

Agreed. I'm specifically referencing wasted space. As I've gotten older, that "wasted" space gets replaced with zoomed fonts using ctrl+=. I've found this attitude and use isn't all that unique anecdotally, so I found the assertion that it's "rare" to be contrary to my own experience. I have no citation the other way though!

Re: From Python to Numpy

#47

Does anyone have a recommendation for something similar to this but for Python itself? I have been trying to find something that is not necessarily an intro or crash course book but a book with tips, great explanations, and neat examples (which this e-book(?)/site has). I see that the author has responded to a couple comments here. Thank you for your great work! It's always great to have a nice reference material wit…

I've been using http://www.oreilly.com/programming/free/a-whirlwind-tour-of-... as an intermediate programmer familiar with many languages.

Next I plan on doing https://github.com/CamDavidsonPilon/Probabilistic-Programmin... or http://www.composingprograms.com/

Re: From Python to Numpy

#48
post #33

Earlier quoted context omitted.

I'm skimming through Effective Python, and so far I think it's a pretty good "best practices" guide for intermediate Python developers.

Seconded. `Effective Python` is a fantastic resource detailing a lot of the ways Python differs from other languages and how to write to the language's strengths. I'm no hot shit, but it definitely moved me from 'middling hobbyist' to 'semi-capable dev'.

Becoming a "semi-capable dev" is exactly what I'm trying to aim for. I started learning Python at a relatively young age and I picked up a set of bad habits. I ended up becoming a Mechanical Engineer so I never had the time or opportunity to code more to become more proficient and write better code. I'm currently taking the self-driving car course through Udacity. I'm hoping this would be a good opportunity to correct my bad habits and become a better coder.

Re: From Python to Numpy

#50

Does anyone have a recommendation for something similar to this but for Python itself? I have been trying to find something that is not necessarily an intro or crash course book but a book with tips, great explanations, and neat examples (which this e-book(?)/site has). I see that the author has responded to a couple comments here. Thank you for your great work! It's always great to have a nice reference material wit…

Hey I work at Dataquest (dataquest.io) and we have a lot of intermediate & advanced Python content. It's all done through an in-browser coding environment which lets us do answer checking and so on.

[deleted]
Post reply on HN