Skip to content

Added Pytests for Decission Tree mean_squared_error method #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions machine_learning/decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def mean_squared_error(self, labels, prediction):
@param labels: a one dimensional numpy array
@param prediction: a floating point value
return value: mean_squared_error calculates the error if prediction is used to estimate the labels
>>> tester = Decision_Tree()
>>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10])
>>> test_prediction = np.float(6)
>>> assert tester.mean_squared_error(test_labels, test_prediction) == Test_Decision_Tree.helper_mean_squared_error_test(test_labels, test_prediction)
>>> test_labels = np.array([1,2,3])
>>> test_prediction = np.float(2)
>>> assert tester.mean_squared_error(test_labels, test_prediction) == Test_Decision_Tree.helper_mean_squared_error_test(test_labels, test_prediction)

"""
if labels.ndim != 1:
print("Error: Input labels must be one dimensional")
Expand Down Expand Up @@ -117,6 +125,27 @@ def predict(self, x):
print("Error: Decision tree not yet trained")
return None

class Test_Decision_Tree:
"""Decision Tres test class
"""

@staticmethod
def helper_mean_squared_error_test(labels, prediction):
"""
helper_mean_squared_error_test:
@param labels: a one dimensional numpy array
@param prediction: a floating point value
return value: helper_mean_squared_error_test calculates the mean squared error
"""
squared_error_sum = np.float(0)
for label in labels:
squared_error_sum += ((label-prediction) ** 2)

return np.float(squared_error_sum/labels.size)





def main():
"""
Expand All @@ -141,3 +170,6 @@ def main():

if __name__ == "__main__":
main()
import doctest

doctest.testmod(name="mean_squarred_error", verbose=True)