@@ -18,7 +18,7 @@ def __init__(self, depth=5, min_leaf_size=5):
18
18
def mean_squared_error (self , labels , prediction ):
19
19
"""
20
20
mean_squared_error:
21
- @param labels: a one dimensional numpy array
21
+ @param labels: a one- dimensional numpy array
22
22
@param prediction: a floating point value
23
23
return value: mean_squared_error calculates the error if prediction is used to
24
24
estimate the labels
@@ -44,26 +44,47 @@ def mean_squared_error(self, labels, prediction):
44
44
def train (self , x , y ):
45
45
"""
46
46
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.
49
49
The contents of y are the labels for the corresponding X values
50
50
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
56
80
constraints
57
81
"""
58
82
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" )
61
84
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" )
64
86
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" )
67
88
68
89
if len (x ) < 2 * self .min_leaf_size :
69
90
self .prediction = np .mean (y )
0 commit comments