MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / main

Function main

machine_learning/decision_tree.py:118–136  ·  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

116 return None
117
118def main():
119 """
120 In this demonstration we're generating a sample data set from the sin function in numpy.
121 We then train a decision tree on the data set and use the decision tree to predict the
122 label of 10 different test values. Then the mean squared error over this test is displayed.
123 """
124 X = np.arange(-1., 1., 0.005)
125 y = np.sin(X)
126
127 tree = Decision_Tree(depth = 10, min_leaf_size = 10)
128 tree.train(X,y)
129
130 test_cases = (np.random.rand(10) * 2) - 1
131 predictions = np.array([tree.predict(x) for x in test_cases])
132 avg_error = np.mean((predictions - test_cases) ** 2)
133
134 print("Test values: " + str(test_cases))
135 print("Predictions: " + str(predictions))
136 print("Average error: " + str(avg_error))
137
138
139if __name__ == '__main__':

Callers 1

decision_tree.pyFile · 0.70

Calls 3

trainMethod · 0.95
predictMethod · 0.95
Decision_TreeClass · 0.85

Tested by

no test coverage detected