Пример #1
0
    def test_generate_models(self):

        degs_msg = "generate_models should return one model for each given degree"
        list_type_msg = "generate_models should return a list of models"
        array_type_msg = (
            "each model returned by generate_models should be of type pylab.array"
        )
        coefficient_mismatch = "coefficients of returned model are not as expected"

        # simple y = x case.
        x = pylab.array(range(50))
        y = pylab.array(range(50))
        degrees = [1]
        models = ps5.generate_models(x, y, degrees)

        self.assertEqual(len(models), len(degrees), degs_msg)
        self.assertIsInstance(models, list, list_type_msg)
        self.assertIsInstance(models[0], pylab.ndarray, array_type_msg)
        self.assertListEqual(list(models[0]), list(pylab.polyfit(x, y, 1)),
                             coefficient_mismatch)

        # two models for y = 2x case
        y = pylab.array(range(0, 100, 2))
        degrees = [1, 2]
        models = ps5.generate_models(x, y, degrees)
        self.assertEqual(len(models), len(degrees), degs_msg)
        self.assertIsInstance(models, list, list_type_msg)
        for m in models:
            self.assertIsInstance(m, pylab.ndarray, array_type_msg)
        for i in range(2):
            self.assertListEqual(
                list(models[i]),
                list(pylab.polyfit(x, y, degrees[i])),
                coefficient_mismatch,
            )

            # three models
        degrees = [1, 2, 20]
        models = ps5.generate_models(x, y, degrees)
        self.assertEqual(len(models), len(degrees), degs_msg)
        self.assertIsInstance(models, list, list_type_msg)
        for m in models:
            self.assertIsInstance(m, pylab.ndarray, array_type_msg)
        for i in range(3):
            self.assertListEqual(
                list(models[i]),
                list(pylab.polyfit(x, y, degrees[i])),
                coefficient_mismatch,
            )
 def test_evaluate_models_on_testing(self):
     x = np.array(range(50))
     y = np.array(range(0, 100, 2))
     degrees = [1, 2]
     models = ps5.generate_models(x, y, degrees)
     rmse = ps5.evaluate_models_on_testing(x, y, models, False)
     correct_rmse = [0, 0]
     self.assertEqual(len(rmse), len(correct_rmse),
                      "RMSE values did not match number of models")
     for index in range(len(correct_rmse)):
         good_enough = math.isclose(correct_rmse[index], rmse[index])
         self.assertTrue(good_enough, "Returned incorrect RMSE values")
 def test_evaluate_models_on_training(self):
     x = np.array(range(50))
     y = np.array(range(0, 100, 2))
     degrees = [1, 2]
     models = ps5.generate_models(x, y, degrees)
     r2 = ps5.evaluate_models_on_training(x, y, models, False)
     correct_r2 = [1, 1]
     self.assertEqual(len(r2), len(correct_r2),
                      "Returned incorrect r^2 values")
     for index in range(len(correct_r2)):
         good_enough = math.isclose(correct_r2[index], r2[index])
         self.assertTrue(good_enough, "Returned incorrect r^2 values")