mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Added Pytests for Decission Tree mean_squared_error method (#1374)
* Added Pytests for Decission Tree Modified the mean_squared_error to be a static method Created the Test_Decision_Tree class Consists of two methods 1. helper_mean_squared_error_test: This method calculates the mean squared error manually without using numpy. Instead a for loop is used for the same. 2. test_one_mean_squared_error: This method considers a simple test case and compares the results by the helper function and the original mean_squared_error method of Decision_Tree class. This is done using asert keyword. Execution: PyTest installation pip3 install pytest OR pip install pytest Test function execution pytest decision_tree.py * Modified the pytests to be compatible with the doctest Added 2 doctest in the mean_squared_error method For its verification a static method helper_mean_squared_error(labels, prediction) is used It uses a for loop to calculate the error instead of the numpy inbuilt methods Execution ``` pytest .\decision_tree.py --doctest-modules ```
This commit is contained in:
parent
179284a41b
commit
4590363806
@ -21,6 +21,14 @@ class Decision_Tree:
|
||||
@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")
|
||||
@ -117,6 +125,27 @@ class Decision_Tree:
|
||||
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():
|
||||
"""
|
||||
@ -141,3 +170,6 @@ def main():
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
import doctest
|
||||
|
||||
doctest.testmod(name="mean_squarred_error", verbose=True)
|
||||
|
Loading…
Reference in New Issue
Block a user