def test_predict_feature_dimensions(self): X = np.random.rand(10, 5) y = np.random.randint(0, 2, 10) clf = Stree() clf.fit(X, y) with self.assertRaises(ValueError): clf.predict(X[:, :3])
def test_single_vs_multiple_prediction(self): """Check if predicting sample by sample gives the same result as predicting all samples at once """ X, y = load_dataset(self._random_state) for kernel in self._kernels: clf = Stree( kernel=kernel, multiclass_strategy="ovr" if kernel == "liblinear" else "ovo", random_state=self._random_state, ) clf.fit(X, y) # Compute prediction line by line yp_line = np.array([], dtype=int) for xp in X: yp_line = np.append(yp_line, clf.predict(xp.reshape(-1, X.shape[1]))) # Compute prediction at once yp_once = clf.predict(X) self.assertListEqual(yp_line.tolist(), yp_once.tolist())
def test_simple_muticlass_dataset(self): for kernel in self._kernels: clf = Stree( kernel=kernel, multiclass_strategy="ovr" if kernel == "liblinear" else "ovo", random_state=self._random_state, ) px = [[1, 2], [5, 6], [9, 10]] py = [0, 1, 2] clf.fit(px, py) self.assertEqual(1.0, clf.score(px, py)) self.assertListEqual(py, clf.predict(px).tolist()) self.assertListEqual(py, clf.classes_.tolist())
def test_mask_samples_weighted_zero(self): X = np.array([ [1, 1], [1, 1], [1, 1], [2, 2], [2, 2], [2, 2], [3, 3], [3, 3], [3, 3], ]) y = np.array([1, 1, 1, 2, 2, 2, 5, 5, 5]) yw = np.array([1, 1, 1, 1, 1, 1, 5, 5, 5]) w = [1, 1, 1, 0, 0, 0, 1, 1, 1] model1 = Stree().fit(X, y) model2 = Stree().fit(X, y, w) predict1 = model1.predict(X) predict2 = model2.predict(X) self.assertListEqual(y.tolist(), predict1.tolist()) self.assertListEqual(yw.tolist(), predict2.tolist()) self.assertEqual(model1.score(X, y), 1) self.assertAlmostEqual(model2.score(X, y), 0.66666667) self.assertEqual(model2.score(X, y, w), 1)
def test_score_binary(self): X, y = load_dataset(self._random_state) accuracies = [ 0.9506666666666667, 0.9493333333333334, 0.9606666666666667, 0.9433333333333334, 0.9153333333333333, ] for kernel, accuracy_expected in zip(self._kernels, accuracies): clf = Stree( random_state=self._random_state, multiclass_strategy="ovr" if kernel == "liblinear" else "ovo", kernel=kernel, ) clf.fit(X, y) accuracy_score = clf.score(X, y) yp = clf.predict(X) accuracy_computed = np.mean(yp == y) self.assertEqual(accuracy_score, accuracy_computed) self.assertAlmostEqual(accuracy_expected, accuracy_score)