# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import unittest
import numpy as np
import pandas as pd
import numpy.testing as np_testing
import pandas.testing as pd_testing
import os
import import_ipynb
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
import sys


class Test(unittest.TestCase):

    def _dirname_if_file(self, filename):
        if os.path.isdir(filename):
            return filename
        else:
            return os.path.dirname(os.path.abspath(filename))

    def setUp(self):    
        import Activity3_02
        self.activity = Activity3_02
       
        self.digits = load_digits()
        self.X = pd.DataFrame(self.digits.data)
        self.Y = pd.DataFrame(self.digits.target)

        self.X_train, self.X_test, self.Y_train, self.Y_test = train_test_split(self.X,self.Y, test_size=0.2, random_state=0)

        self.model = tree.DecisionTreeClassifier(random_state=0)
        self.model = self.model.fit(self.X_train, self.Y_train)
        self.Y_pred = self.model.predict(self.X_test)

        
    def test_input_frames(self):
        np_testing.assert_equal(self.activity.digits.data, self.digits.data)


    def test_model_prediction(self):     
        self.activity_Y_pred = self.activity.model.predict(self.X_test)

        np_testing.assert_almost_equal(self.Y_pred, self.activity_Y_pred)


    def test_model_precision(self):
        self.Y_test_2 = self.Y_test[:]
        self.Y_test_2[self.Y_test_2 != 6] = 1
        self.Y_test_2[self.Y_test_2 == 6] = 0

        self.Y_pred_2 = self.Y_pred
        self.Y_pred_2[self.Y_pred_2 != 6] = 1
        self.Y_pred_2[self.Y_pred_2 == 6] = 0

        self.precision = precision_score(self.Y_test_2, self.Y_pred_2)

        np_testing.assert_equal(self.precision, self.activity.precision)


    def test_model_recall(self):
        self.Y_test_2 = self.Y_test[:]
        self.Y_test_2[self.Y_test_2 != 6] = 1
        self.Y_test_2[self.Y_test_2 == 6] = 0

        self.Y_pred_2 = self.Y_pred
        self.Y_pred_2[self.Y_pred_2 != 6] = 1
        self.Y_pred_2[self.Y_pred_2 == 6] = 0
        self.recall = recall_score(self.Y_test_2, self.Y_pred_2)
        np_testing.assert_equal(self.recall, self.activity.recall)



if __name__ == '__main__':
    unittest.main(argv=['first-arg-is-ignored'], exit=False)

StarcoderdataJupyterScriptsDedupFiltered