Skip to content

Instantly share code, notes, and snippets.

@ahollenbach
Created February 9, 2015 16:03
Show Gist options
  • Select an option

  • Save ahollenbach/59e7f3b48997e4ea7fe9 to your computer and use it in GitHub Desktop.

Select an option

Save ahollenbach/59e7f3b48997e4ea7fe9 to your computer and use it in GitHub Desktop.
Sample Mitigation Data Generator
import random
# CLASSES
# one - reduced by 25%
# two - reduced by 50%
# three - lowest possible fine
# neither - no mitigation
data = []
def saveToFile():
with open('test.data', 'w') as f:
for instance in data:
f.write(",".join(toStr(inst) for inst in instance) + "\n")
def toStr(o):
# normally we wouldn't do this, but we want to convert bools to look like WEKA's
if type(o) is not bool:
return str(o)
if o:
return "TRUE"
return "FALSE"
def main():
for i in range(1000):
sick = random.random() <= 0.2
emergency = random.random() <= 0.1
longDuration = random.random() <= 0.15
weekend = random.random() <= 0.28
firstTime = random.random() <= 0.3
mainRoad = random.random() <= 0.2
# the threshold is used to determine which mitigation
# the person receives.
# It starts out with a slightly random element to not
# create definitive 100% cases.
thresh = random.random()/10.0
if sick: thresh += 0.15
if emergency: thresh += 0.4
if longDuration: thresh -= 0.1
if weekend: thresh += 0.1
if firstTime: thresh += 0.4
if mainRoad: thresh -= 0.1
classif = ""
if thresh >= 0.8: classif = "three"
elif thresh >= 0.4: classif = "two"
elif thresh >= 0.2: classif = "one"
else: classif = "neither"
data.append([weekend,emergency,longDuration,mainRoad,firstTime,sick,classif])
saveToFile()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment