Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107020
| Path | csiph.com!feeder.erje.net!2.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | justin walters <walters.justin01@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Looking for feedback on weighted voting algorithm |
| Date | Thu, 14 Apr 2016 19:11:37 -0700 |
| Lines | 100 |
| Message-ID | <mailman.1.1460686772.26128.python-list@python.org> (permalink) |
| References | <CAO1D73EFoLoJm=4shNfgEsJinGHrM82J_BsTRMnm7XX4tW1h+A@mail.gmail.com> <alpine.LSU.2.11.1604140859230.18407@znpeba.jbaqresebt.arg> <CAO1D73GG2Rfp6VK1Tr6fL6Ok4sJo55anYXsXK=PVWRfkNKVBrA@mail.gmail.com> <CAGgTfkN8RCzZW2uWuCS+A7hXunP9Fc-TUAZRM3dhdfMhV9YDnQ@mail.gmail.com> <CAO1D73EoWG5zc8R1F7ANFvkNqquTVY5uaR7wzChh4yf9WCgYzQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de jjJ/tAz7cQT4T88ut0KGigIRIOGI8EwYsbv1G3Kvh8Ag== |
| Return-Path | <walters.justin01@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'essentially': 0.04; 'cache': 0.05; 'float': 0.05; '(1,': 0.09; '3),': 0.09; 'integers': 0.09; 'loop.': 0.09; 'wrong,': 0.09; 'zero.': 0.09; 'stored': 0.10; 'python': 0.10; 'suggest': 0.15; 'thu,': 0.15; '*other*': 0.16; '1),': 0.16; '1:48': 0.16; '2),': 0.16; '2016': 0.16; '4),': 0.16; 'anyways,': 0.16; 'downstream': 0.16; 'enough.': 0.16; 'inputs': 0.16; 'integers,': 0.16; 'justin': 0.16; 'loops': 0.16; 'michael.': 0.16; 'nan': 0.16; 'nans': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sure.': 0.16; 'traceback.': 0.16; 'write.': 0.16; 'wrote:': 0.16; 'alternate': 0.18; 'expanded': 0.18; 'try:': 0.18; '>': 0.18; 'email addr:gmail.com>': 0.18; 'input': 0.18; '>>>': 0.20; 'algorithm': 0.20; 'work,': 0.21; 'martin': 0.22; '31,': 0.22; 'am,': 0.23; 'code.': 0.23; 'wrote': 0.23; 'header:In-Reply-To:1': 0.24; 'wondering': 0.25; 'example': 0.26; 'helpful': 0.27; 'error': 0.27; 'handling': 0.27; 'mostly': 0.27; 'message- id:@mail.gmail.com': 0.27; '14,': 0.27; 'data,': 0.27; 'correct': 0.28; 'function': 0.28; 'went': 0.28; 'regular': 0.29; 'clever': 0.29; 'division': 0.29; 'voting.': 0.29; 'raise': 0.29; "i'm": 0.30; 'print': 0.30; 'that.': 0.30; 'code': 0.30; "i'd": 0.31; 'error.': 0.31; 'anyone': 0.32; 'realize': 0.32; 'michael': 0.33; "i'll": 0.33; 'except': 0.34; 'handle': 0.34; 'add': 0.34; 'list': 0.34; 'advice': 0.35; 'gets': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'clear': 0.35; 'skip:> 10': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'skip:& 10': 0.37; 'thanks': 0.37; 'skip:z 10': 0.38; 'received:209': 0.38; 'wrong': 0.38; 'anything': 0.38; 'someone': 0.38; 'mean': 0.38; 'means': 0.39; 'sure': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'watch': 0.62; 'more': 0.63; 'safe': 0.63; 'believe': 0.66; 'skip:\xc2 10': 0.67; 'sum': 0.69; 'greetings': 0.71; '26,': 0.72; 'transferred': 0.72; 'overall': 0.72; 'score': 0.76; 'counts': 0.81; 'heavy': 0.81; 'clarity.': 0.84; 'walters': 0.84; 'approach.': 0.91; 'sorry.': 0.91; 'fun!': 0.95 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=eIimu235CxLQjdgHZNU3jdDWqN4RT2M58zpFZhcsGmY=; b=MqvIfQp5RjRv5F8rQBfWLjF4IohfAyKYw+5NnXGLgoXG8hDePfs8a4kqb+obe7wHF3 eIF5t5iQRvUxlKgVX2xbgsnF5Fy5SonZjb0vB94fnEPtag250ajwBhVoSBnqu164wiFJ rBt3XrZD9mEXqbzkLEc9LKwLVFrR3x/MDw35EC/GunmqJPsCQNrwbH7/upOGf1ApiBIo AaTpLAHUUpyx5w1Zj2OJ7zSadV5/6Dx6aiE6VWn74Lahwpl7OW5nLI7qIUVKolgclRUl 3iWO0rlT0v1N1bsRCSWF8Lhu/9P8jKDkpNkuM683MvPR3kxJ8dIeeCaHb7ZFte3XcKx2 0WOA== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=eIimu235CxLQjdgHZNU3jdDWqN4RT2M58zpFZhcsGmY=; b=SCg/4Y/ZZToDdVRKeH6VR/j8oBwL5Q5sn8YKvHRL80Q+uSlJNn1j/gwXrx/x9rj9yB hFrnejfQ2VhrmT6J4kw5LQa+/T1yTNsXbtQIoVt3nQDThnlbLdDB21Qyv9xDkgYBeLJd BpCmSIEG8Wb3AWfwE9dxjmq47cz9t1MHcNmgAMgJcjpWOedpmHe7dbtcTsvvYqrnbGpS 0iWvycYjN2Goba4Daz7T+B53k6JsuaMfo1r3d18P1axVVlwU3T8K6LQU+8T6MFDRlMPU bUW9nw4J4eDIO3OTGyXC77uOoa3HNvQJqPw5ZfwGKoV/35suXd6GS8E/MBNzhXg9AUfP Bj4g== |
| X-Gm-Message-State | AOPr4FVT51pgYziIUjnsE/+F0yN1/FTa2Qss6ez0otRm1jmKQZU2VrBQXL2PNR1Jn2hzNctbJkbVpqvRqrPfUQ== |
| X-Received | by 10.112.62.165 with SMTP id z5mr6602366lbr.89.1460686297448; Thu, 14 Apr 2016 19:11:37 -0700 (PDT) |
| In-Reply-To | <CAGgTfkN8RCzZW2uWuCS+A7hXunP9Fc-TUAZRM3dhdfMhV9YDnQ@mail.gmail.com> |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.21 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CAO1D73EoWG5zc8R1F7ANFvkNqquTVY5uaR7wzChh4yf9WCgYzQ@mail.gmail.com> |
| X-Mailman-Original-References | <CAO1D73EFoLoJm=4shNfgEsJinGHrM82J_BsTRMnm7XX4tW1h+A@mail.gmail.com> <alpine.LSU.2.11.1604140859230.18407@znpeba.jbaqresebt.arg> <CAO1D73GG2Rfp6VK1Tr6fL6Ok4sJo55anYXsXK=PVWRfkNKVBrA@mail.gmail.com> <CAGgTfkN8RCzZW2uWuCS+A7hXunP9Fc-TUAZRM3dhdfMhV9YDnQ@mail.gmail.com> |
| Xref | csiph.com comp.lang.python:107020 |
Show key headers only | View raw
Thanks for the advice and the code Michael.
That's definitely a clever way to do it.
The algorithm is going to be used for a web application, so the inputs
should be sanitized for security reasons. It will be trivial to make sure
the inputs are integers and are in the correct range. I will raise a
DivisionByZero error for clarity.
I plan to have the result stored in the cache and then transferred to the
db after every new vote, so this algorithm could see some heavy use for
short periods of time. Therefore, performance is important for me. I
believe the algorithm that I provided is O(n) or O(n+1), but I never really
studied Compsci, so I'm not sure. Either way, I think it should perform
well enough.
On Thu, Apr 14, 2016 at 1:48 PM, Michael Selik <michael.selik@gmail.com>
wrote:
>
>
> On Thu, Apr 14, 2016, 7:37 PM justin walters <walters.justin01@gmail.com>
> wrote:
>
>> On Apr 14, 2016 9:41 AM, "Martin A. Brown" <martin@linux-ip.net> wrote:
>> >
>> >
>> > Greetings Justin,
>> >
>> > > score = sum_of_votes/num_of_votes
>> >
>> > >votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2),
>> (91, 1)]
>> >
>> > >Specifically, I'm wondering if this is a good algorithm for
>> > >weighted voting. Essentially a vote is weighted by the number of
>> > >votes it counts as. I realize that this is an extremely simple
>> > >algorithm, but I was wondering if anyone had suggestions on how to
>> > >improve it.
>> >
>> > I snipped most of your code. I don't see anything wrong with your
>> > overall approach. I will make one suggestion: watch out for
>> > DivisionByZero.
>> >
>> > try:
>> > score = sum_of_votes / num_of_votes
>> > except ZeroDivisionError:
>> > score = float('nan')
>> >
>> > In your example data, all of the weights were integers, which means
>> > that a simple mean function would work, as well, if you expanded the
>> > votes to an alternate representation:
>> >
>> > votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
>> > 31, 31, 31, 68, 68, 91]
>> >
>> > But, don't bother!
>> >
>> > Your function can handle votes that have a float weight:
>> >
>> > >>> weight([(4, 1.3), (1, 1),])
>> > 2.695652173913044
>> >
>> > Have fun!
>> >
>> > -Martin
>> >
>> > --
>> > Martin A. Brown
>> > http://linux-ip.net/
>>
>> Thanks Martin!
>>
>> I'll add the check for division by zero. Didn't think about that. I think
>> I'm going to sanitize input anyways, but always better to be safe than
>> sorry.
>>
>
> I suggest not worrying about sanitizing inputs. If someone provides bad
> data, Python will do the right thing: stop the program and print an
> explanation of what went wrong, often a more helpful message than one you'd
> write. Use error handling mostly for when you want to do something *other*
> than stop the program.
>
> I'm not sure I'd use NaN instead of raise division by zero error. NaNs can
> be troublesome for downstream code that might not notice until it gets
> confusing. A div-by-zero error is clear and easier to track down because of
> the traceback.
>
> What do you think of using list comprehensions?
>
> weighted_sum = sum(rating * weight for rating, weight in votes)
> total_weights = sum(weight for rating, weight in votes)
> score = weighted_sum / total_weights
>
> It's two loops as I wrote it, which is instinctively slower, but it might
> actually execute faster because of the built-in sum vs a regular for loop.
> Not sure.
>
>>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Looking for feedback on weighted voting algorithm justin walters <walters.justin01@gmail.com> - 2016-04-14 19:11 -0700
csiph-web