-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathExercise 00 - Clever decision tree unittests.py
31 lines (23 loc) · 1.24 KB
/
Exercise 00 - Clever decision tree unittests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import unittest
from exercise0 import CleverDecisionTree
import numpy as np
class TestCleverDecisionTree(unittest.TestCase):
def setUp(self):
self.X = np.tile([[0.9615202, 4.43376825], [1.99127366, 0.80487522], [-1.6656056, 2.88982984]], (70, 1))
np.random.seed(0)
self.X += np.random.normal(0, .7, size=self.X.shape)
self.y = np.tile([0, 1, 2], 70)
self.clf = CleverDecisionTree()
def test_instantiation(self):
self.assertTrue(hasattr(self.clf, 'predict'), "You need a predict method for making predictions.")
self.assertTrue(hasattr(self.clf, 'score'), "You need a score method for evaluating the classifier.")
def test_several_predictions(self):
pred = self.clf.predict(self.X)
self.assertIsInstance(pred, np.ndarray, "predict should return a numpy array")
self.assertEqual(pred.ndim, 1, "predict() should return a flat vector (try using .ravel() )")
def test_score(self):
score = self.clf.score(self.X, self.y)
self.assertIsInstance(score, float, "score should return the accuracy as a float between 0 and 1.")
self.assertGreater(score, .90, "The accuracy is not high enough.")
if __name__ == '__main__':
unittest.main(verbosity=2)