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

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

## No comments:

## Post a Comment