Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Decision Tree scikit implementation. #85

Merged
merged 3 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions methods/scikit/dtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self, dataset, timeout=0, verbose=True):
self.dataset = dataset
self.timeout = timeout
self.model = None
self.predictions = None
self.build_opts = {}
'''
Build the model for the Decision Tree Classifier.
Expand Down Expand Up @@ -104,18 +105,23 @@ def RunDTCScikit(q):
with totalTimer:
self.model = self.BuildModel(trainData, labels)
# Run Decision Tree Classifier on the test dataset.
self.model.predict(testData)
self.predictions = self.model.predict(testData)
except Exception as e:
Log.Debug(str(e))
q.put(-1)
return -1

time = totalTimer.ElapsedTime()
q.put(time)
q.put((time, self.predictions))

return time

return timeout(RunDTCScikit, self.timeout)
result = timeout(RunDTCScikit, self.timeout)
# Check for error, in this case the tuple doesn't contain extra information.
if len(result) > 1:
self.predictions = result[1]

return result[0]

'''
Perform the Decision Tree Classifier. If the method has been
Expand All @@ -142,20 +148,14 @@ def RunMetrics(self, options):

if len(self.dataset) >= 3:
# Check if we need to create a model.
if not self.model:
trainData, labels = SplitTrainData(self.dataset)
self.model = self.BuildModel(trainData, labels)

testData = LoadDataset(self.dataset[1])
truelabels = LoadDataset(self.dataset[2])
predictedlabels = self.model.predict(testData)

confusionMatrix = Metrics.ConfusionMatrix(truelabels, predictedlabels)
confusionMatrix = Metrics.ConfusionMatrix(truelabels, self.predictions)
metrics['ACC'] = Metrics.AverageAccuracy(confusionMatrix)
metrics['MCC'] = Metrics.MCCMultiClass(confusionMatrix)
metrics['Precision'] = Metrics.AvgPrecision(confusionMatrix)
metrics['Recall'] = Metrics.AvgRecall(confusionMatrix)
metrics['MSE'] = Metrics.SimpleMeanSquaredError(truelabels, predictedlabels)
metrics['MSE'] = Metrics.SimpleMeanSquaredError(truelabels, self.predictions)

return metrics

3 changes: 3 additions & 0 deletions tests/benchmark_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def test_Constructor(self):
def test_RunMetrics(self):
result = self.instance.RunMetrics({})
self.assertTrue(result["Runtime"] > 0)
self.assertTrue(result["ACC"] > 0)
self.assertTrue(result["Precision"] > 0)
self.assertTrue(result["Recall"] > 0)

'''
Test the shogun Decision Tree Prediction script.
Expand Down