def test_response_column(self): data_ = data.BrainTreeData(np.zeros((25, 3)), np.zeros((25, 2))) data_.to_dmatrix(1) with self.assertRaises(ValueError): data_.to_dmatrix(-1) with self.assertRaises(ValueError): data_.to_dmatrix(2)
def test_split_matches(self): # Ensure that predictors and responses are still correctly paired after the split data_ = data.BrainTreeData(np.arange(15), np.arange(15)) data_.shuffle() self.assertListEqual(data_.predictors.tolist(), data_.responses.tolist()) self.assertListEqual(data_.predictors.tolist(), data_.responses.tolist())
def test_split_fraction_validation(self): # Ensure that the validation on split_fraction works as planned. data_ = data.BrainTreeData(np.zeros(10), np.zeros(10)) with self.assertRaises(ValueError): data_.split(-3) with self.assertRaises(ValueError): data_.split(1.1) with self.assertRaises(ValueError): # This yields zero rows in the first data set, and so is disallowed. data_.split(0.08)
def test_default_standardization(self): data_ = data.BrainTreeData(concrete[:, :7], concrete[:, 7:]) data_.standardize() np.testing.assert_almost_equal(np.mean(data_.predictors, axis=0), [0] * 7) np.testing.assert_almost_equal(np.mean(data_.responses, axis=0), [0] * 3) np.testing.assert_almost_equal(np.std(data_.predictors, axis=0), [1] * 7) np.testing.assert_almost_equal(np.std(data_.responses, axis=0), [1] * 3)
def test_generator(self): data_ = data.BrainTreeData( self._x2_data(0, 12).reshape([12, 2]), np.arange(12)) batch_1, batch_2, batch_3 = data_.to_array_generator(4) # Check predictors np.testing.assert_array_equal(batch_1[0], self._x2_data(0, 4)) np.testing.assert_array_equal(batch_2[0], self._x2_data(4, 8)) np.testing.assert_array_equal(batch_3[0], self._x2_data(8, 12)) # Check responses np.testing.assert_array_equal(batch_1[1], np.arange(0, 4)) np.testing.assert_array_equal(batch_2[1], np.arange(4, 8)) np.testing.assert_array_equal(batch_3[1], np.arange(8, 12))
def test_alternate_standardization(self): # Compute true means and sds for reference data_ = data.BrainTreeData(concrete[:, :7], concrete[:, 7:]) predictor_means = np.mean(data_.predictors, axis=0) predictor_sds = np.std(data_.predictors, axis=0) response_means = np.mean(data_.responses, axis=0) response_sds = np.std(data_.responses, axis=0) # Standardize with forced values data_.standardize({ "predictor_means": [10] * 7, "predictor_sds": [5] * 7, "response_means": [10] * 3, "response_sds": [5] * 3 }) # Assert that the standardization used the supplied parameters np.testing.assert_almost_equal(np.mean(data_.predictors, axis=0), (predictor_means - 10) / 5) np.testing.assert_almost_equal(np.std(data_.predictors, axis=0), predictor_sds / 5) np.testing.assert_almost_equal(np.mean(data_.responses, axis=0), (response_means - 10) / 5) np.testing.assert_almost_equal(np.std(data_.responses, axis=0), response_sds / 5)
import unittest import numpy as np from context import tree from context import neural from context import data from context import concrete concrete_data = data.BrainTreeData(concrete[:, :7], concrete[:, 7:]) concrete_data.shuffle() concrete_train, concrete_test = concrete_data.split(0.7) class NeuralTest(unittest.TestCase): def test_model_construction(self): model_ = neural.NeuralModel(30) self.assertIsNotNone(model_) def test_model_predictions_default(self): model_ = neural.NeuralModel(7) predictions = model_.predict(concrete_train) self.assertListEqual(predictions.tolist(), [0.] * 72) def test_model_predictions_initial(self): starting_tree = tree.TreeModel(max_depth=4, num_trees=25, default_split_strength=30) starting_tree.fit(concrete_train.to_dmatrix(), concrete_test.to_dmatrix()) model_ = neural.NeuralModel(7, max_depth=4, num_trees=25)
def test_mismatched_lengths(self): with self.assertRaises(ValueError): data.BrainTreeData(np.zeros(10), np.zeros(15))
def test_3d_input(self): # #-D Input in either position should be rejected out of hand. with self.assertRaises(ValueError): data.BrainTreeData(np.zeros((3, 2, 5)), np.zeros(5)) with self.assertRaises(ValueError): data.BrainTreeData(np.zeros((3, 2)), np.zeros((5, 3, 7)))
def test_2d_coercion(self): # 1-D inputs should be coerced to 2-D. data_ = data.BrainTreeData(np.zeros(20), np.ones(20)) self.assertListEqual(list(data_.predictors.shape), [20, 1]) self.assertListEqual(list(data_.responses.shape), [20, 1])
def test_split_dimensions(self): data_ = data.BrainTreeData(np.zeros(10), np.ones(10)) train, test = data_.split(0.7) self.assertEqual(train.predictors.shape[0], 7) self.assertEqual(test.predictors.shape[0], 3)
def setUpClass(cls): cls.predictors = np.zeros((10, 4)) cls.responses = np.zeros((10, 2)) cls.data = data.BrainTreeData(cls.predictors, cls.responses) cls.xgb_data = cls.data.to_dmatrix(1)