def setUp(self): self.n_queries = 2 self.n_neighbors = 3 self.n_classifiers = 5 from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression lr = LogisticRegression() self.bag = Bagging(lr, pool_size=self.n_classifiers) self.X, self.y = load_iris(return_X_y=True) np.random.seed(5) self.predictions = np.random.randint(low=np.min(self.y), high=np.max(self.y), size=(self.n_queries, self.n_neighbors, self.n_classifiers)) self.predictions_query = np.random.randint(low=np.min(self.y), high=np.max(self.y), size=(self.n_queries, self.n_classifiers)) self.bag.fit(self.X, self.y) self.selector = LCA(self.X, self.y, n_neighbors=self.n_neighbors)
class TestBaseSelection(unittest.TestCase): def setUp(self): self.true_labels = np.array([1, 1, 0, 0, 1, 0]) self.predictions1 = np.array([1, 0, 0, 0, 0, 0]) self.predictions2 = np.array([0, 1, 0, 0, 1, 1]) self.predictions3 = np.array([1, 0, 0, 0, 0, 1]) from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression lr = LogisticRegression() self.bag = Bagging(lr, pool_size=5) self.X, self.y = load_iris(return_X_y=True) self.bag.fit(self.X, self.y) self.selector = BaseSelection(self.X, self.y, n_neighbors=3) def test_predict_nearest(self): predictions, actual_values = self.selector.predict_nearest( self.X[0:2, :], self.bag) self.assertEqual(predictions.shape, (2, 3, 1)) self.assertEqual(actual_values.shape, (2, 3))
def setUp(self): from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression lr = LogisticRegression() self.single = Bagging(lr, pool_size=1) self.classifier = Bagging(lr, pool_size=5) self.X, self.y = load_iris(return_X_y=True)
def setUp(self): self.true_labels = np.array([1, 1, 0, 0, 1, 0]) self.predictions1 = np.array([1, 0, 0, 0, 0, 0]) self.predictions2 = np.array([0, 1, 0, 0, 1, 1]) self.predictions3 = np.array([1, 0, 0, 0, 0, 1]) from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression lr = LogisticRegression() self.bag = Bagging(lr, pool_size=5) self.X, self.y = load_iris(return_X_y=True) self.bag.fit(self.X, self.y) self.selector = BaseSelection(self.X, self.y, n_neighbors=3)
class test_ola(unittest.TestCase): def setUp(self): self.n_queries = 2 self.n_neighbors = 3 self.n_classifiers = 5 from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression lr = LogisticRegression() self.bag = Bagging(lr, pool_size=self.n_classifiers) self.X, self.y = load_iris(return_X_y=True) np.random.seed(5) self.predictions = np.random.randint(low=np.min(self.y), high=np.max(self.y), size=(self.n_queries, self.n_neighbors, self.n_classifiers)) self.predictions_query = np.random.randint(low=np.min(self.y), high=np.max(self.y), size=(self.n_queries, self.n_classifiers)) self.bag.fit(self.X, self.y) self.selector = LCA(self.X, self.y, n_neighbors=self.n_neighbors) def test_local_class_accuracy(self): _, actual_labels = self.selector.predict_nearest( self.X[:self.n_queries, :], self.bag) a = self.selector.best_local_class_accuracy(self.predictions, self.predictions_query, actual_labels) self.assertEqual(a.shape[0], self.n_queries) def test_select_and_predict(self): predictions = self.selector.select_and_predict(self.X[:3], self.bag) self.assertEqual(predictions.shape, (3, ))
class TestBagging(TestCase): def setUp(self): from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression lr = LogisticRegression() self.single = Bagging(lr, pool_size=1) self.classifier = Bagging(lr, pool_size=5) self.X, self.y = load_iris(return_X_y=True) def test_fit(self): self.single.fit(self.X, self.y) self.classifier.fit(self.X, self.y) self.assertTrue(self.classifier.has_been_fit, msg='Classifier fit') def test_predict(self): self.classifier.fit(self.X, self.y) y_pred = self.classifier.predict(self.X) self.assertEqual(len(y_pred), len(self.X))