| 8 | import numpy as np |
| 9 | |
| 10 | class Decision_Tree: |
| 11 | def __init__(self, depth = 5, min_leaf_size = 5): |
| 12 | self.depth = depth |
| 13 | self.decision_boundary = 0 |
| 14 | self.left = None |
| 15 | self.right = None |
| 16 | self.min_leaf_size = min_leaf_size |
| 17 | self.prediction = None |
| 18 | |
| 19 | def mean_squared_error(self, labels, prediction): |
| 20 | """ |
| 21 | mean_squared_error: |
| 22 | @param labels: a one dimensional numpy array |
| 23 | @param prediction: a floating point value |
| 24 | return value: mean_squared_error calculates the error if prediction is used to estimate the labels |
| 25 | """ |
| 26 | if labels.ndim != 1: |
| 27 | print("Error: Input labels must be one dimensional") |
| 28 | |
| 29 | return np.mean((labels - prediction) ** 2) |
| 30 | |
| 31 | def train(self, X, y): |
| 32 | """ |
| 33 | train: |
| 34 | @param X: a one dimensional numpy array |
| 35 | @param y: a one dimensional numpy array. |
| 36 | The contents of y are the labels for the corresponding X values |
| 37 | |
| 38 | train does not have a return value |
| 39 | """ |
| 40 | |
| 41 | """ |
| 42 | this section is to check that the inputs conform to our dimensionality constraints |
| 43 | """ |
| 44 | if X.ndim != 1: |
| 45 | print("Error: Input data set must be one dimensional") |
| 46 | return |
| 47 | if len(X) != len(y): |
| 48 | print("Error: X and y have different lengths") |
| 49 | return |
| 50 | if y.ndim != 1: |
| 51 | print("Error: Data set labels must be one dimensional") |
| 52 | return |
| 53 | |
| 54 | if len(X) < 2 * self.min_leaf_size: |
| 55 | self.prediction = np.mean(y) |
| 56 | return |
| 57 | |
| 58 | if self.depth == 1: |
| 59 | self.prediction = np.mean(y) |
| 60 | return |
| 61 | |
| 62 | best_split = 0 |
| 63 | min_error = self.mean_squared_error(X,np.mean(y)) * 2 |
| 64 | |
| 65 | |
| 66 | """ |
| 67 | loop over all possible splits for the decision tree. find the best split. |