[issue18844] allow weights in random.choice
Steven Basart added the comment: Left in a line of code that was supposed to be removed. Fixed. -- Added file: http://bugs.python.org/file42393/weighted_choice_v5.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Changes by Steven Basart : Removed file: http://bugs.python.org/file42392/weighted_choice_v5.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: Re-implemented with suggested improvements taken into account. Thanks @mark.dickinson and @pitrou for the suggestions. I also removed the redundant "fast path" portion for this code since it doesn't deal with generators anyways. Let me know additional thoughts about it. -- Added file: http://bugs.python.org/file42392/weighted_choice_v5.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: I reuploaded the file. The spacing on the if amount < 1 was off. Hopefully its fixed now. -- Added file: http://bugs.python.org/file42386/weighted_choice_v4.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Changes by Steven Basart : Removed file: http://bugs.python.org/file42385/weighted_choice_v4.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: Okay so I added a few lines of code. One to make it return a single number if amount == 1 and the other to check that the amount > 1. The main difference I've noticed between this implementation and previous versions compared to say R is that in R they provide a boolean flag to ask if sampling with replacement. Here's there documentation and source code: https://github.com/wch/r-source/blob/e5b21d0397c607883ff25cca379687b86933d730/src/library/base/man/sample.Rd https://github.com/wch/r-source/blob/e5b21d0397c607883ff25cca379687b86933d730/src/library/base/R/sample.R Maybe someone else can comment more on the use cases. I can only say for myself that I've needed this function plenty of times when working with samples that have a non uniform distribution. -- Added file: http://bugs.python.org/file42385/weighted_choice_v3.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: Hey serhiy.storchaka I can edit the code to output just one value if called with simply a list and then return a list of values if called with the optional amount parameter. My code also needs to check that amount >= 1. My code was mostly just to restart this discussion as I personally like the idea of the function for weighted choice and would like it to be standard in the random library. I have no qualms with adding both weighted_choice and weighted_choice_generator but my concern is mostly that you are asking too much and it won't go through by trying to add two functions at the same time. The other thing is that I believe that weighted_choice could suffice with just one function call. I just think my last concern is that generators are different from the other functions in random.py. Whereas they are more intuitive and accepted in the builtins like map and zip etc. There isn't any other functions in the random library that return that type of object when called. They instead return a numerical result. Those are my concerns and hence why I rewrote the code. -- ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: Hello rhettinger. I filled out the form thanks for letting me know about it. Is there anything else I have to do? Hey serhiy.storchaka There were several things "wrong" with the previous implementation in my opinion. 1st they tried to add too much. Which would if allowed would clutter up the random library if every function had both it's implementation as well as an accompanied generator. The other problem being that both were attempted to be made as callable to the public API. I would prefer the generator if present to be hidden and would also have to be more sophisticated to be able to check if it was being called with new input. 2nd by adding in the generator to the pulbic API of the random library it makes it far more confusing and obfuscates the true purpose of this function anyways which is to get a weighted choice. So basically there is nothing wrong with generators but they don't necessarily belong here so I removed it to try to get back to the core principles of what the function should be doing, by making it simpler. -- ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: The entire function of weighted choice. I removed the generator and replaced it by adding an optional argument to specify an amount by which you want to call this function. -- Added file: http://bugs.python.org/file42323/weighted_choice_v3.patch ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18844] allow weights in random.choice
Steven Basart added the comment: Reopen this idea but removing the generator from weighted choice. -- nosy: +progressive-o Added file: http://bugs.python.org/file42322/weighted_choice_v3.diff ___ Python tracker <http://bugs.python.org/issue18844> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com