MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / main

Function main

machine_learning/decision_tree.py:176–196  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

174
175
176def 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
199if __name__ == "__main__":

Callers 1

decision_tree.pyFile · 0.70

Calls 3

trainMethod · 0.95
predictMethod · 0.95
DecisionTreeClass · 0.85

Tested by

no test coverage detected