Live data from Hacker News

Help us collect modern Python NumPy code solutions

github.com

31–40 of 41 posts

Re: Help us collect modern Python NumPy code solutions

#31
I'm starting to add type annotations to numerical code, and I wonder how to handle the disconnect between `float` (which is how floating-point literals are typed) and concrete NumPy types such as `numpy.float64` (which is the scalar type I get out of NumPy arrays).

I ended up defining an alias:

Float = Union[float, numpy.float64]

but I wonder if there is a more legitimate way to handle thiss.

Re: Help us collect modern Python NumPy code solutions

#32

Earlier quoted context omitted.

Fixed some of those. Simple case of just pushing the edit button and sending a PR.

The suggested solution https://github.com/Onelinerhub/onelinerhub/blob/34467e427cc6... still runs out of memory with many files. I believe the original intention behind using mode="a" was to append to the output file while reading the input files at the same time. This way, there is no need for an ever-growing string array. But there are still many other issues like using default platform string encoding instead of d…

For bonus points: how about properly handling UTF-8 byte order marks (BOM), too? The way this code is written if the files contain a BOM, you're going to pass the BOM right into the destination file. Fine for the first file, not so fine for any other file. Dropping a BOM mark in the middle of a file isn't going to result in valid UTF-8...

Re: Help us collect modern Python NumPy code solutions

#33

I learned J before NumPy and I'm glad I did, because it's much easier to see and learn the algorithms when they're short symbols. Compare np.sqrt(sum(np.square(x))) versus %: +/ *: x So if you can translate one of the array languages to NumPy, you can tap into the wealth of "idioms" collected over the years. For example: J Phrases[0] (organized by category), BQNcrate[1] and APLcart[2] (searchable). [0]: https://www.j…

Interesting suggestion since

    %: +/ *: x
Is impossible to read of course without learning the J language's main constructs. While the python function application is easier to guess from other languages.

Re: Help us collect modern Python NumPy code solutions

#34

Earlier quoted context omitted.

>>> arrays = [np.arange(1000)]*10 >>> zip_arrays(arrays) Traceback (most recent call last): File " ", line 1, in File " ", line 2, in zip_arrays2 File " ", line 2, in File " ", line 180, in concatenate ValueError: zero-dimensional arrays cannot be concatenated Try this: def zip_arrays(arrays): return np.concatenate([array[:l,...,None] for l in [min(array.shape[0] for array in arrays)] for array in arrays], axis=-1) O…

I think a more Pythonic solution would be np.array(arrays).T or maybe np.copy(arrays).T for one less character. The shortest solution is probably np.c_[arrays].T but likely causes a Google search on first read.

I'm used to numpy and I can't understand np.c_ even with the docs. It says "Translates slice objects to concatenation along the second axis." but the examples pass in a list of arrays and use no slice objects (the builtin slice, I'm assuming)?

If there was an example using just `np.c_[1:5, 11:15]` it would make sense. But clearly np.c_'s meaning has been extended beyond slices in some way. (Maybe I'm coming closer to understanding, but still miffed about the misleading doc.)

Re: Help us collect modern Python NumPy code solutions

#35

Earlier quoted context omitted.

That does not zip though. In fact, it creates a 1-dimensional array of objects because the arrays do not have the same number of items. >>> a = [np.arange(i) for i in range(1, 11)] >>> np.array(a).T array([array([0]), array([0, 1]), array([0, 1, 2]), array([0, 1, 2, 3]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4, 5]), array([0, 1, 2, 3, 4, 5, 6]), array([0, 1, 2, 3, 4, 5, 6, 7]), array([0, 1, 2, 3, 4, 5, 6, 7, 8])…

You are completely right. Nevertheless, when I zip over arrays of different length, it is usually a bug instead of being intentional. A code-golfed version for different array lengths: np.c_[[a[:min(map(len,arrays))].T for a in arrays]].T The trick to get a local variable within a list comprehension statement with for l in [foo()] from your zip_array function is very cool by the way, but I sacrificed it in favor of s…

I like your your solution, I think using `a[0:min(map(len,arrays))]` (ie with 0) is faster because it returns a slice and not a copy of the array.

I am glad you liked the trick!

Re: Help us collect modern Python NumPy code solutions

#36
I would love to see something like this for `pandas`. Also SQLAlchemy. EDIT: apparently there's a very limited set of oneliners for `pandas` already.

Why do so few libraries seem to understand that good documentation starts by providing meaningful examples of simple tasks with semantic labels that can be found via search? Reference-style documentation assumes that you already know the name of the thing you're looking for (you don't), or that you have time to read for several days and build up an entire mental model of the system in one go before getting anything productive done (also no).

Re: Help us collect modern Python NumPy code solutions

#37
post #33

I learned J before NumPy and I'm glad I did, because it's much easier to see and learn the algorithms when they're short symbols. Compare np.sqrt(sum(np.square(x))) versus %: +/ *: x So if you can translate one of the array languages to NumPy, you can tap into the wealth of "idioms" collected over the years. For example: J Phrases[0] (organized by category), BQNcrate[1] and APLcart[2] (searchable). [0]: https://www.j…

Interesting suggestion since %: +/ *: x Is impossible to read of course without learning the J language's main constructs. While the python function application is easier to guess from other languages.

I know English, so I'll just guess at what those non-English people are saying. What could go wrong?

Re: Help us collect modern Python NumPy code solutions

#38

I would love to see something like this for `pandas`. Also SQLAlchemy. EDIT: apparently there's a very limited set of oneliners for `pandas` already. Why do so few libraries seem to understand that good documentation starts by providing meaningful examples of simple tasks with semantic labels that can be found via search? Reference-style documentation assumes that you already know the name of the thing you're looking…

+1 absolutely - it's been one of my pet peeves. There really needs to be some thought put into "doc UX".

Re: Help us collect modern Python NumPy code solutions

#39
post #33

Earlier quoted context omitted.

Interesting suggestion since %: +/ *: x Is impossible to read of course without learning the J language's main constructs. While the python function application is easier to guess from other languages.

I know English, so I'll just guess at what those non-English people are saying. What could go wrong?

I know spanish so I understand some french sentences. It's actually quite easy to see what I don't understand. It doesn't usually go wrong.

Re: Help us collect modern Python NumPy code solutions

#40

For almost all issues, you can simply search for site:stackoverflow.com + the title of the issue and get a larger variety of solutions ranked by upvotes, so this repository seems strictly worse. Is there any advantage that this repository brings over just searching for the question on StackOverflow?

A lot of times the best solution is buried in the third or fourth solution in the 5th or 6th search result. I can’t even find the solution that I know I have googled before.
Post reply on HN