def test_train_ovr(self): for k in range(2, 6): with self.subTest(k): X, Y = test_data.separable_circle_data_set(50, k) w, b = pvml.one_vs_rest_svm_train(X, Y, 0, lr=1e-1, steps=1000) Yhat = pvml.one_vs_rest_svm_inference(X, w, b)[0] self.assertListEqual(Y.tolist(), Yhat.tolist())
def test_knn_auto_k(self): for k in range(1, 5): with self.subTest(k): X, Y = test_data.separable_circle_data_set(24, k) k, _ = pvml.knn_select_k(X, Y, maxk=7) self.assertGreater(k, 0) self.assertLess(k, 8)
def test_batch(self): X, Y = test_data.separable_circle_data_set(10, 2) net = pvml.CNN([X.shape[1], 2, 2], [1, 1]) X = X.reshape(X.shape[0], 1, 1, X.shape[1]) net.train(X, Y, lr=1e-1, steps=1000, batch=10) Yhat, P = net.inference(X) self.assertListEqual(Y.tolist(), Yhat.tolist())
def test_heteroscedastic_gda(self): for k in range(1, 5): with self.subTest(k): X, Y = test_data.separable_circle_data_set(48, k) means, icovs, priors = pvml.hgda_train(X, Y) Yhat, scores = pvml.hgda_inference(X, means, icovs, priors) self.assertListEqual(Y.tolist(), Yhat.tolist()) self.assertListEqual(priors.tolist(), [1.0 / k] * k)
def test_train1(self): for k in range(1, 6): with self.subTest(k): X, Y = test_data.separable_circle_data_set(50, k) w, b = pvml.multinomial_logreg_train(X, Y, 0, lr=1e-1, steps=1000) P = pvml.multinomial_logreg_inference(X, w, b) Yhat = P.argmax(1) self.assertListEqual(Y.tolist(), Yhat.tolist())
def test_train_no_pruning(self): tree = pvml.ClassificationTree() div = ["gini", "entropy", "error"] for k in range(1, 6): with self.subTest(k): X, Y = test_data.separable_circle_data_set(50, k) tree.train(X, Y, diversity=div[k % 3], pruning_cv=0) Yhat = tree.inference(X)[0] self.assertListEqual(Y.tolist(), Yhat.tolist())
def test_train_ovo(self): for k in range(2, 6): with self.subTest(k): X, Y = test_data.separable_circle_data_set(50, k) a, b = pvml.one_vs_one_ksvm_train(X, Y, "rbf", 0.1, 0, lr=1e-1, steps=1000) Yhat = pvml.one_vs_one_ksvm_inference(X, X, a, b, "rbf", 0.1)[0] self.assertListEqual(Y.tolist(), Yhat.tolist())
def test_train1(self): X, Y = test_data.separable_circle_data_set(50, 2) w, b = pvml.svm_train(X, Y, 0, lr=1e-1, steps=1000) Yhat, P = pvml.svm_inference(X, w, b) self.assertListEqual(Y.tolist(), Yhat.tolist()) self.assertListEqual(Yhat.tolist(), (P > 0).tolist())
def test_knn_5(self): for k in range(1, 5): with self.subTest(k): X, Y = test_data.separable_circle_data_set(48, k) Yhat, P = pvml.knn_inference(X, X, Y, k=5) self.assertListEqual(Y.tolist(), Yhat.tolist())
def test_train1(self): X, Y = test_data.separable_circle_data_set(50, 2) alpha, b = pvml.ksvm_train(X, Y, "rbf", 0.1, 0, lr=1e-1, steps=1000) Yhat, P = pvml.ksvm_inference(X, X, alpha, b, "rbf", 0.1) self.assertListEqual(Y.tolist(), Yhat.tolist()) self.assertListEqual(Yhat.tolist(), (P > 0).tolist())
def test_train1(self): X, Y = test_data.separable_circle_data_set(50, 2) w, b = pvml.perceptron_train(X, Y) Yhat, Z = pvml.perceptron_inference(X, w, b) self.assertListEqual(Y.tolist(), Yhat.tolist())