Monday, December 6, 2010

Randomly assigning classes to rows in a table

This morning a coworker came to me with a problem he wanted to solve using the Field Calculator in ArcMap. He had a table and he wanted to randomly assign rows in the table to different classes, given a set of probabilities for each class. So for example if class1 had a probability of 0.5, class2 of 0.3, class 3 of 0.1, and class4 of 0.1, then half of the rows in the table would be assigned class1, 30% of them would be class2, and the remaining rows would be split evenly between class3 and class4.

This is the solution I gave him. Make sure the Field Calculator is set to use the Python parser, and put the following code in the Codeblock area:

import numpy
probs = {}
probs[1] = .5
probs[2] = .3
probs[3] = .1
probs[4] = .1
def setclass():
    r = numpy.random.random()
    t = 0
    for key in probs.keys():
        t += probs[key]
        if t > r:
            return key

You can add as many classes as you want using the probs variable, but the probabilities should add up to 1. I used integers for the classes (1-4) but they could also be strings enclosed in quotes. Just make sure that the field being calculated is a string field in that case.

The last step is putting this in the calculator part of the Field Calculator dialog.

setclass()

2 comments:

  1. Woori Casino No Deposit Bonus 2021 | Free Play in Demo
    Woori Casino offers https://sol.edu.kg/ a variety of free https://octcasino.com/ spins and no deposit bonuses, kadangpintar as well as regular wooricasinos.info promotions. As you can't claim this offer without being 바카라 사이트 registered

    ReplyDelete
  2. With 온라인 카지노 money odds, whenever there's a minus (-) the player lays that quantity to win $100; the place there is a plus (+) the player wins that quantity for each $100 wagered. Book – A establishment that accepts bets on the result result} of sporting events. A heated discussion about how much distinction there may be} between Saints quarterbacks Andy Dalton and Jameis Winston dominated the talk Thursday on the most recent episode of “Bayou Bets,” the sports activities betting present on Bet.NOLA.com. The latest edition of "Bayou Bets," the sports activities betting present on Bet.NOLA.com, came after a New Orleans Saints win that was sudden in many of} corners, notably within the trend of a 24-0 shutout. B. The Department shall examine all cheap allegations of prohibited conduct by a permit holder.

    ReplyDelete