In this demonstration we're generating a sample data set from the sin function in numpy. We then train a decision tree on the data set and use the decision tree to predict the label of 10 different test values. Then the mean squared error over this test is displayed.
()
| 174 | |
| 175 | |
| 176 | def main(): |
| 177 | """ |
| 178 | In this demonstration we're generating a sample data set from the sin function in |
| 179 | numpy. We then train a decision tree on the data set and use the decision tree to |
| 180 | predict the label of 10 different test values. Then the mean squared error over |
| 181 | this test is displayed. |
| 182 | """ |
| 183 | x = np.arange(-1.0, 1.0, 0.005) |
| 184 | y = np.sin(x) |
| 185 | |
| 186 | tree = DecisionTree(depth=10, min_leaf_size=10) |
| 187 | tree.train(x, y) |
| 188 | |
| 189 | rng = np.random.default_rng() |
| 190 | test_cases = (rng.random(10) * 2) - 1 |
| 191 | predictions = np.array([tree.predict(x) for x in test_cases]) |
| 192 | avg_error = np.mean((predictions - test_cases) ** 2) |
| 193 | |
| 194 | print("Test values: " + str(test_cases)) |
| 195 | print("Predictions: " + str(predictions)) |
| 196 | print("Average error: " + str(avg_error)) |
| 197 | |
| 198 | |
| 199 | if __name__ == "__main__": |
no test coverage detected