def setUpClass(cls): cls.n, cls.d = 500, 10 np.random.seed(7) x_class1 = np.random.normal(0, 5, cls.n * cls.d).reshape(cls.n, cls.d) x_class2 = np.random.normal(0, 10, cls.n * cls.d).reshape(cls.n, cls.d) w = np.random.uniform(1, 5, cls.d).reshape(cls.d, 1) y_class1 = np.full((cls.n, 1), -1) y_class2 = np.full((cls.n, 1), 1) x = np.concatenate((x_class1, x_class2)) y = np.concatenate((y_class1, y_class2)) x_train, x_test, cls.y_train, cls.y_test = train_test_split( x, y, random_state=7, test_size=0.25) x_standardizer = Standardizer(x_train) cls.x_train_std = x_standardizer.standardize(x_train) cls.x_test_std = x_standardizer.standardize(x_test) cls.model = LogisticRegression(cls.x_train_std, cls.y_train, lamd=1) cls.model.train()
def setUpClass(cls): cls.n, cls.d = 1000, 10 np.random.seed(7) x = np.random.uniform(0, 50, cls.n * cls.d).reshape(cls.n, cls.d) eps = np.random.normal(loc=0, scale=2, size=cls.n * cls.d).reshape(cls.n, cls.d) w = np.random.uniform(1, 5, cls.d).reshape(cls.d, 1) y = (x + eps).dot(w) x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=7, test_size=0.25) x_standardizer = Standardizer(x_train) cls.x_train_std = x_standardizer.standardize(x_train) cls.x_test_std = x_standardizer.standardize(x_test) y_standardizer = Standardizer(y_train) cls.y_train_std = y_standardizer.standardize(y_train) cls.y_test_std = y_standardizer.standardize(y_test) cls.model = LassoRegression(cls.x_train_std, cls.y_train_std, lambd=0.5 / 2) cls.model.cycliccoorddescent() cls.model.randcoorddescent()
def test_zero_std_standardization(self): data = np.ones(shape=(3, 10)) standardizer = Standardizer(data) self.assertFalse(np.any(standardizer.train_sd == 0.0))
def test_label_standardization(self): y_standardizer = Standardizer(self.labels) labels_standardized = y_standardizer.standardize(self.labels) self.assertTrue( np.array_equal(labels_standardized, self.labels_standardized_expected))
def test_feature_standardization(self): x_standardizer = Standardizer(self.features) features_standardized = x_standardizer.standardize(self.features) self.assertTrue( np.array_equal(features_standardized, self.features_standardized_expected))
def test_labels_mean_shape(self): y_standardizer = Standardizer(self.labels) self.assertTupleEqual(y_standardizer.train_mean.shape, (1, 1))
def test_features_std_shape(self): x_normalizer = Standardizer(self.features) self.assertTupleEqual(x_normalizer.train_sd.shape, (1, self.d))
def test_invalid_shape(self): with self.assertRaises(ValueError): Standardizer(self.labels.flatten())
def test_type_labels(self): y_standardizer = Standardizer(self.labels) self.assertIsInstance(y_standardizer, Standardizer)
def test_type_features(self): x_standardizer = Standardizer(self.features) self.assertIsInstance(x_standardizer, Standardizer)