示例#1
0
def do_work(csv_file, cancer_value, save_file):
    data, headers = load_data(csv_file)
    X, y, X_headers = split_class(data, headers)
    y_copy = [(x) for x in y]
    print("converting cancer values")
    convert_values(lambda x: x > cancer_value, y)
    dt = DecisionTree()
    print("creating decision tree")
    dt.train(X, y, X_headers)
    print("saving decision tree")
    with open(save_file, "w") as modelfile:
        dt.save(modelfile)
    modelfile.close()
    print("done")
    return dt, data, headers
示例#2
0
X = balancedata[balancedata.columns[1:]]

trainlen = int(len(X) * 0.8)
train_X = X[:trainlen]
test_X = X[trainlen:]
train_y = y[:trainlen]
test_y = y[trainlen:]

print("A little taste of the training data.")
print(train_X[:10])
print(train_y[:10])

# Train the model using the basic features of DecisionTree
dt = DecisionTree()
dt.train(X, y, cols[1:])
#print(x)
print("The model looks like:")
print(dt)
print("Testing it out.")

dt.test(test_X, test_y, display=True)

# Demonstrate saving and loading the model.
with open("whatever.model", "wb") as modelfile:
    dt.save(modelfile)
with open("whatever.model", "rb") as modelfile:
    dt2 = DecisionTree(load_from=modelfile)
    print(dt2)

print("All done!")