Skip to content

Commit d688108

Browse files
Suyashd999cclauss
andcommitted
Added doctest to decision_tree.py (TheAlgorithms#11143)
* Added doctest to decision_tree.py * Update decision_tree.py * Update machine_learning/decision_tree.py * Update machine_learning/decision_tree.py * raise ValueError() * Update decision_tree.py --------- Co-authored-by: Christian Clauss <[email protected]>
1 parent 9f040a0 commit d688108

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

Diff for: machine_learning/decision_tree.py

+35-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, depth=5, min_leaf_size=5):
1818
def mean_squared_error(self, labels, prediction):
1919
"""
2020
mean_squared_error:
21-
@param labels: a one dimensional numpy array
21+
@param labels: a one-dimensional numpy array
2222
@param prediction: a floating point value
2323
return value: mean_squared_error calculates the error if prediction is used to
2424
estimate the labels
@@ -44,26 +44,47 @@ def mean_squared_error(self, labels, prediction):
4444
def train(self, x, y):
4545
"""
4646
train:
47-
@param x: a one dimensional numpy array
48-
@param y: a one dimensional numpy array.
47+
@param x: a one-dimensional numpy array
48+
@param y: a one-dimensional numpy array.
4949
The contents of y are the labels for the corresponding X values
5050
51-
train does not have a return value
52-
"""
53-
54-
"""
55-
this section is to check that the inputs conform to our dimensionality
51+
train() does not have a return value
52+
53+
Examples:
54+
1. Try to train when x & y are of same length & 1 dimensions (No errors)
55+
>>> dt = DecisionTree()
56+
>>> dt.train(np.array([10,20,30,40,50]),np.array([0,0,0,1,1]))
57+
58+
2. Try to train when x is 2 dimensions
59+
>>> dt = DecisionTree()
60+
>>> dt.train(np.array([[1,2,3,4,5],[1,2,3,4,5]]),np.array([0,0,0,1,1]))
61+
Traceback (most recent call last):
62+
...
63+
ValueError: Input data set must be one-dimensional
64+
65+
3. Try to train when x and y are not of the same length
66+
>>> dt = DecisionTree()
67+
>>> dt.train(np.array([1,2,3,4,5]),np.array([[0,0,0,1,1],[0,0,0,1,1]]))
68+
Traceback (most recent call last):
69+
...
70+
ValueError: x and y have different lengths
71+
72+
4. Try to train when x & y are of the same length but different dimensions
73+
>>> dt = DecisionTree()
74+
>>> dt.train(np.array([1,2,3,4,5]),np.array([[1],[2],[3],[4],[5]]))
75+
Traceback (most recent call last):
76+
...
77+
ValueError: Data set labels must be one-dimensional
78+
79+
This section is to check that the inputs conform to our dimensionality
5680
constraints
5781
"""
5882
if x.ndim != 1:
59-
print("Error: Input data set must be one dimensional")
60-
return
83+
raise ValueError("Input data set must be one-dimensional")
6184
if len(x) != len(y):
62-
print("Error: X and y have different lengths")
63-
return
85+
raise ValueError("x and y have different lengths")
6486
if y.ndim != 1:
65-
print("Error: Data set labels must be one dimensional")
66-
return
87+
raise ValueError("Data set labels must be one-dimensional")
6788

6889
if len(x) < 2 * self.min_leaf_size:
6990
self.prediction = np.mean(y)

0 commit comments

Comments
 (0)