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()

1 comment:

  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