From 18c9387ae9f7bb816143da86f6dc038fe34b7702 Mon Sep 17 00:00:00 2001 From: janezd Date: Sat, 22 Jul 2017 11:05:03 +0200 Subject: [PATCH] distances: Reformat old test_distances.py --- Orange/tests/test_distances.py | 917 ++++++++++++++++++++------------- 1 file changed, 555 insertions(+), 362 deletions(-) diff --git a/Orange/tests/test_distances.py b/Orange/tests/test_distances.py index 3697f7ad401..46693b2aafc 100644 --- a/Orange/tests/test_distances.py +++ b/Orange/tests/test_distances.py @@ -30,6 +30,7 @@ def setUpClass(cls): cls.iris = Table('iris') cls.dist = Euclidean(cls.iris) + # pylint: disable=unsubscriptable-object def test_submatrix(self): sub = self.dist.submatrix([2, 3, 4]) np.testing.assert_equal(sub, self.dist[2:5, 2:5]) @@ -38,8 +39,10 @@ def test_submatrix(self): def test_pickling(self): unpickled_dist = pickle.loads(pickle.dumps(self.dist)) np.testing.assert_equal(unpickled_dist, self.dist) - self.assertTrue(tables_equal(unpickled_dist.row_items, self.dist.row_items)) - self.assertTrue(tables_equal(unpickled_dist.col_items, self.dist.col_items)) + self.assertTrue(tables_equal(unpickled_dist.row_items, + self.dist.row_items)) + self.assertTrue(tables_equal(unpickled_dist.col_items, + self.dist.col_items)) self.assertEqual(unpickled_dist.axis, self.dist.axis) def test_deprecated(self): @@ -181,6 +184,7 @@ def test_save(self): self.assertEqual(m.axis, 0) +# noinspection PyTypeChecker class TestEuclidean(TestCase): @classmethod def setUpClass(cls): @@ -189,60 +193,83 @@ def setUpClass(cls): cls.dist = Euclidean def test_euclidean_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.iris[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[1]), np.array([[0.53851648071346281]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[1], axis=1), np.array([[0.53851648071346281]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[1]), + np.array([[0.53851648071346281]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[1], axis=1), + np.array([[0.53851648071346281]])) def test_euclidean_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.iris[:2]), - np.array([[0., 0.53851648], - [0.53851648, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], axis=0), - np.array([[0., 2.48394847, 5.09313263, 6.78969808], - [2.48394847, 0., 2.64007576, 4.327817], - [5.09313263, 2.64007576, 0., 1.69705627], - [6.78969808, 4.327817, 1.69705627, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[2], self.iris[:3]), - np. array([[0.50990195, 0.3, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], self.iris[3]), - np.array([[0.64807407], - [0.33166248]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], self.iris[:3]), - np.array([[0., 0.53851648, 0.50990195], - [0.53851648, 0., 0.3]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2]), + np.array([[0., 0.53851648], + [0.53851648, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], axis=0), + np.array([[0., 2.48394847, 5.09313263, 6.78969808], + [2.48394847, 0., 2.64007576, 4.327817], + [5.09313263, 2.64007576, 0., 1.69705627], + [6.78969808, 4.327817, 1.69705627, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[2], self.iris[:3]), + np. array([[0.50990195, 0.3, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], self.iris[3]), + np.array([[0.64807407], + [0.33166248]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], self.iris[:3]), + np.array([[0., 0.53851648, 0.50990195], + [0.53851648, 0., 0.3]])) def test_euclidean_distance_sparse(self): - np.testing.assert_almost_equal(self.dist(self.sparse), - np.array([[0., 3.74165739, 6.164414], - [3.74165739, 0., 4.47213595], - [6.164414, 4.47213595, 0.]])) - np.testing.assert_almost_equal(self.dist(self.sparse, axis=0), - np.array([[0., 4.12310563, 3.31662479], - [4.12310563, 0., 6.164414], - [3.31662479, 6.164414, 0.]])) - np.testing.assert_almost_equal(self.dist(self.sparse[:2]), - np.array([[0., 3.74165739], - [3.74165739, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.sparse), + np.array([[0., 3.74165739, 6.164414], + [3.74165739, 0., 4.47213595], + [6.164414, 4.47213595, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.sparse, axis=0), + np.array([[0., 4.12310563, 3.31662479], + [4.12310563, 0., 6.164414], + [3.31662479, 6.164414, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.sparse[:2]), + np.array([[0., 3.74165739], + [3.74165739, 0.]])) def test_euclidean_distance_numpy(self): - #np.testing.assert_almost_equal(self.dist(self.iris[0].x, self.iris[1].x, axis=1), - # np.array([[0.53851648071346281]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X), - np.array([[0., 0.53851648], - [0.53851648, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[2].x, self.iris[:3].X), - np. array([[0.50990195, 0.3, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X, self.iris[3].x), - np.array([[0.64807407], - [0.33166248]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X, self.iris[:3].X), - np.array([[0., 0.53851648, 0.50990195], - [0.53851648, 0., 0.3]])) - - + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X), + np.array([[0., 0.53851648], + [0.53851648, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[2].x, self.iris[:3].X), + np. array([[0.50990195, 0.3, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X, self.iris[3].x), + np.array([[0.64807407], + [0.33166248]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X, self.iris[:3].X), + np.array([[0., 0.53851648, 0.50990195], + [0.53851648, 0., 0.3]])) + + +# noinspection PyTypeChecker class TestManhattan(TestCase): @classmethod def setUpClass(cls): @@ -251,59 +278,86 @@ def setUpClass(cls): cls.dist = Manhattan def test_manhattan_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.iris[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[1]), np.array([[0.7]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[1], axis=1), np.array([[0.7]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[1]), + np.array([[0.7]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[1], axis=1), + np.array([[0.7]])) def test_manhattan_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.iris[:2]), - np.array([[0., 0.7], - [0.7, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], axis=0), - np.array([[0., 3.5, 7.2, 9.6], - [3.5, 0., 3.7, 6.1], - [7.2, 3.7, 0., 2.4], - [9.6, 6.1, 2.4, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[2], self.iris[:3]), - np.array([[0.8, 0.5, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], self.iris[3]), - np.array([[1.], - [0.5]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], self.iris[:3]), - np.array([[0., 0.7, 0.8], - [0.7, 0., 0.5]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2]), + np.array([[0., 0.7], + [0.7, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], axis=0), + np.array([[0., 3.5, 7.2, 9.6], + [3.5, 0., 3.7, 6.1], + [7.2, 3.7, 0., 2.4], + [9.6, 6.1, 2.4, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[2], self.iris[:3]), + np.array([[0.8, 0.5, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], self.iris[3]), + np.array([[1.], + [0.5]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], self.iris[:3]), + np.array([[0., 0.7, 0.8], + [0.7, 0., 0.5]])) def test_manhattan_distance_sparse(self): - np.testing.assert_almost_equal(self.dist(self.sparse), - np.array([[0., 6., 10.], - [6., 0., 6.], - [10., 6., 0.]])) - np.testing.assert_almost_equal(self.dist(self.sparse, axis=0), - np.array([[0., 5., 5.], - [5., 0., 10.], - [5., 10., 0.]])) - np.testing.assert_almost_equal(self.dist(self.sparse[:2]), - np.array([[0., 6.], - [6., 0.]])) + np.testing.assert_almost_equal( + self.dist(self.sparse), + np.array([[0., 6., 10.], + [6., 0., 6.], + [10., 6., 0.]])) + np.testing.assert_almost_equal( + self.dist(self.sparse, axis=0), + np.array([[0., 5., 5.], + [5., 0., 10.], + [5., 10., 0.]])) + np.testing.assert_almost_equal( + self.dist(self.sparse[:2]), + np.array([[0., 6.], + [6., 0.]])) def test_manhattan_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.iris[0].x, self.iris[1].x, axis=1), np.array([[0.7]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X), - np.array([[0., 0.7], - [0.7, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[2].x, self.iris[:3].X), - np.array([[0.8, 0.5, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X, self.iris[3].x), - np.array([[1.], - [0.5]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X, self.iris[:3].X), - np.array([[0., 0.7, 0.8], - [0.7, 0., 0.5]])) - - + np.testing.assert_almost_equal( + self.dist(self.iris[0].x, self.iris[1].x, axis=1), + np.array([[0.7]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X), + np.array([[0., 0.7], + [0.7, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[2].x, self.iris[:3].X), + np.array([[0.8, 0.5, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X, self.iris[3].x), + np.array([[1.], + [0.5]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X, self.iris[:3].X), + np.array([[0., 0.7, 0.8], + [0.7, 0., 0.5]])) + + +# noinspection PyTypeChecker class TestCosine(TestCase): @classmethod def setUpClass(cls): @@ -312,59 +366,85 @@ def setUpClass(cls): cls.dist = Cosine def test_cosine_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.iris[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[1]), np.array([[0.00142084]])) - np.testing.assert_almost_equal(self.dist(self.iris[0], self.iris[1], axis=1), np.array([[0.00142084]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0]), np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[1]), + np.array([[0.00142084]])) + np.testing.assert_almost_equal( + self.dist(self.iris[0], self.iris[1], axis=1), + np.array([[0.00142084]])) def test_cosine_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.iris[:2]), - np.array([[0., 1.42083650e-03], - [1.42083650e-03, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], axis=0), - np.array([[0.0, 1.61124231e-03, 1.99940020e-04, 1.99940020e-04], - [1.61124231e-03, 0.0, 2.94551450e-03, 2.94551450e-03], - [1.99940020e-04, 2.94551450e-03, 0.0, 0.0], - [1.99940020e-04, 2.94551450e-03, 0.0, 0.0]])) - np.testing.assert_almost_equal(self.dist(self.iris[2], self.iris[:3]), - np.array([[1.26527175e-05, 1.20854727e-03, 0.0]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], self.iris[3]), - np.array([[0.00089939], - [0.00120607]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2], self.iris[:3]), - np.array([[0.0, 1.42083650e-03, 1.26527175e-05], - [1.42083650e-03, 0.0, 1.20854727e-03]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2]), + np.array([[0., 1.42083650e-03], + [1.42083650e-03, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], axis=0), + np.array([[0.0, 1.61124231e-03, 1.99940020e-04, 1.99940020e-04], + [1.61124231e-03, 0.0, 2.94551450e-03, 2.94551450e-03], + [1.99940020e-04, 2.94551450e-03, 0.0, 0.0], + [1.99940020e-04, 2.94551450e-03, 0.0, 0.0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[2], self.iris[:3]), + np.array([[1.26527175e-05, 1.20854727e-03, 0.0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], self.iris[3]), + np.array([[0.00089939], + [0.00120607]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2], self.iris[:3]), + np.array([[0.0, 1.42083650e-03, 1.26527175e-05], + [1.42083650e-03, 0.0, 1.20854727e-03]])) def test_cosine_distance_sparse(self): - np.testing.assert_almost_equal(self.dist(self.sparse), - np.array([[0.0, 1.00000000e+00, 7.20627882e-01], - [1.00000000e+00, 0.0, 2.19131191e-01], - [7.20627882e-01, 2.19131191e-01, 0.0]])) - np.testing.assert_almost_equal(self.dist(self.sparse, axis=0), - np.array([[0.0, 7.57464375e-01, 1.68109669e-01], - [7.57464375e-01, 0.0, 1.00000000e+00], - [1.68109669e-01, 1.00000000e+00, 0.0]])) - np.testing.assert_almost_equal(self.dist(self.sparse[:2]), - np.array([[0.0, 1.00000000e+00], - [1.00000000e+00, 0.0]])) + np.testing.assert_almost_equal( + self.dist(self.sparse), + np.array([[0.0, 1.00000000e+00, 7.20627882e-01], + [1.00000000e+00, 0.0, 2.19131191e-01], + [7.20627882e-01, 2.19131191e-01, 0.0]])) + np.testing.assert_almost_equal( + self.dist(self.sparse, axis=0), + np.array([[0.0, 7.57464375e-01, 1.68109669e-01], + [7.57464375e-01, 0.0, 1.00000000e+00], + [1.68109669e-01, 1.00000000e+00, 0.0]])) + np.testing.assert_almost_equal( + self.dist(self.sparse[:2]), + np.array([[0.0, 1.00000000e+00], + [1.00000000e+00, 0.0]])) def test_cosine_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.iris[0].x, self.iris[1].x, axis=1), np.array([[0.00142084]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X), - np.array([[0., 1.42083650e-03], - [1.42083650e-03, 0.]])) - np.testing.assert_almost_equal(self.dist(self.iris[2].x, self.iris[:3].X), - np.array([[1.26527175e-05, 1.20854727e-03, 0.0]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X, self.iris[3].x), - np.array([[0.00089939], - [0.00120607]])) - np.testing.assert_almost_equal(self.dist(self.iris[:2].X, self.iris[:3].X), - np.array([[0.0, 1.42083650e-03, 1.26527175e-05], - [1.42083650e-03, 0.0, 1.20854727e-03]])) - - + np.testing.assert_almost_equal( + self.dist(self.iris[0].x, self.iris[1].x, axis=1), + np.array([[0.00142084]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X), + np.array([[0., 1.42083650e-03], + [1.42083650e-03, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.iris[2].x, self.iris[:3].X), + np.array([[1.26527175e-05, 1.20854727e-03, 0.0]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X, self.iris[3].x), + np.array([[0.00089939], + [0.00120607]])) + np.testing.assert_almost_equal( + self.dist(self.iris[:2].X, self.iris[:3].X), + np.array([[0.0, 1.42083650e-03, 1.26527175e-05], + [1.42083650e-03, 0.0, 1.20854727e-03]])) + + +# noinspection PyTypeChecker class TestJaccard(TestCase): @classmethod def setUpClass(cls): @@ -372,49 +452,73 @@ def setUpClass(cls): cls.dist = Jaccard def test_jaccard_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.titanic[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.titanic[0], self.titanic[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.titanic[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.titanic[0], self.titanic[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.titanic[0], self.titanic[2]), np.array([[0.5]])) - np.testing.assert_almost_equal(self.dist(self.titanic[0], self.titanic[2], axis=1), np.array([[0.5]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[0], self.titanic[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[0], self.titanic[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[0], self.titanic[2]), + np.array([[0.5]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[0], self.titanic[2], axis=1), + np.array([[0.5]])) def test_jaccard_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.titanic), - np.array([[0., 0., 0.5, 0.5], - [0., 0., 0.5, 0.5], - [0.5, 0.5, 0., 0.], - [0.5, 0.5, 0., 0.]])) - np.testing.assert_almost_equal(self.dist(self.titanic, axis=0), - np.array([[0., 1., 0.5], - [1., 0., 1.], - [0.5, 1., 0.]])) - np.testing.assert_almost_equal(self.dist(self.titanic[2], self.titanic[:3]), - np.array([[0.5, 0.5, 0.]])) - np.testing.assert_almost_equal(self.dist(self.titanic[:2], self.titanic[3]), - np.array([[0.5], - [0.5]])) - np.testing.assert_almost_equal(self.dist(self.titanic[:2], self.titanic[:3]), - np.array([[0., 0., 0.5], - [0., 0., 0.5]])) + np.testing.assert_almost_equal( + self.dist(self.titanic), + np.array([[0., 0., 0.5, 0.5], + [0., 0., 0.5, 0.5], + [0.5, 0.5, 0., 0.], + [0.5, 0.5, 0., 0.]])) + np.testing.assert_almost_equal( + self.dist(self.titanic, axis=0), + np.array([[0., 1., 0.5], + [1., 0., 1.], + [0.5, 1., 0.]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[2], self.titanic[:3]), + np.array([[0.5, 0.5, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[:2], self.titanic[3]), + np.array([[0.5], + [0.5]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[:2], self.titanic[:3]), + np.array([[0., 0., 0.5], + [0., 0., 0.5]])) def test_jaccard_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.titanic[0].x, self.titanic[2].x, axis=1), np.array([[0.5]])) - np.testing.assert_almost_equal(self.dist(self.titanic.X), - np.array([[0., 0., 0.5, 0.5], - [0., 0., 0.5, 0.5], - [0.5, 0.5, 0., 0.], - [0.5, 0.5, 0., 0.]])) - np.testing.assert_almost_equal(self.dist(self.titanic[2].x, self.titanic[:3].X), - np.array([[0.5, 0.5, 0.]])) - np.testing.assert_almost_equal(self.dist(self.titanic[:2].X, self.titanic[3].x), - np.array([[0.5], - [0.5]])) - np.testing.assert_almost_equal(self.dist(self.titanic[:2].X, self.titanic[:3].X), - np.array([[0., 0., 0.5], - [0., 0., 0.5]])) - - + np.testing.assert_almost_equal( + self.dist(self.titanic[0].x, self.titanic[2].x, axis=1), + np.array([[0.5]])) + np.testing.assert_almost_equal( + self.dist(self.titanic.X), + np.array([[0., 0., 0.5, 0.5], + [0., 0., 0.5, 0.5], + [0.5, 0.5, 0., 0.], + [0.5, 0.5, 0., 0.]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[2].x, self.titanic[:3].X), + np.array([[0.5, 0.5, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[:2].X, self.titanic[3].x), + np.array([[0.5], + [0.5]])) + np.testing.assert_almost_equal( + self.dist(self.titanic[:2].X, self.titanic[:3].X), + np.array([[0., 0., 0.5], + [0., 0., 0.5]])) + + +# noinspection PyTypeChecker class TestSpearmanR(TestCase): @classmethod def setUpClass(cls): @@ -422,58 +526,80 @@ def setUpClass(cls): cls.dist = SpearmanR def test_spearmanr_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1]), np.array([[0.5083333333333333]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1], axis=1), - np.array([[0.5083333333333333]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1]), + np.array([[0.5083333333333333]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1], axis=1), + np.array([[0.5083333333333333]])) def test_spearmanr_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.breast[:2]), - np.array([[0., 0.5083333333333333], - [0.5083333333333333, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:4]), - np.array([[0., 0.50833333, 0.075, 0.61666667], - [0.50833333, 0., 0.38333333, 0.53333333], - [0.075, 0.38333333, 0., 0.63333333], - [0.61666667, 0.53333333, 0.63333333, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], axis=0), - np.array([[0., 0.25, 0., 0.25, 0.25, 0.25, 0.75, 0.25, 0.25], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], - [0., 0.25, 0., 0.25, 0.25, 0.25, 0.75, 0.25, 0.25], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], - [0.75, 0.25, 0.75, 0.25, 0.25, 0.25, 0., 0.25, 1.], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], - [0.25, 0.75, 0.25, 0.75, 0.75, 0.75, 1., 0.75, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], self.breast[:4]), - np.array([[0., 0.50833333, 0.075, 0.61666667], - [0.50833333, 0., 0.38333333, 0.53333333], - [0.075, 0.38333333, 0., 0.63333333]])) - np.testing.assert_almost_equal(self.dist(self.breast[2], self.breast[:3]), - np. array([[0.075, 0.3833333, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], self.breast[2]), - np. array([[0.075], - [0.3833333], - [0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2]), + np.array([[0., 0.5083333333333333], + [0.5083333333333333, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:4]), + np.array([[0., 0.50833333, 0.075, 0.61666667], + [0.50833333, 0., 0.38333333, 0.53333333], + [0.075, 0.38333333, 0., 0.63333333], + [0.61666667, 0.53333333, 0.63333333, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], axis=0), + np.array([[0., 0.25, 0., 0.25, 0.25, 0.25, 0.75, 0.25, 0.25], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], + [0., 0.25, 0., 0.25, 0.25, 0.25, 0.75, 0.25, 0.25], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], + [0.75, 0.25, 0.75, 0.25, 0.25, 0.25, 0., 0.25, 1.], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.75], + [0.25, 0.75, 0.25, 0.75, 0.75, 0.75, 1., 0.75, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], self.breast[:4]), + np.array([[0., 0.50833333, 0.075, 0.61666667], + [0.50833333, 0., 0.38333333, 0.53333333], + [0.075, 0.38333333, 0., 0.63333333]])) + np.testing.assert_almost_equal( + self.dist(self.breast[2], self.breast[:3]), + np.array([[0.075, 0.3833333, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], self.breast[2]), + np.array([[0.075], + [0.3833333], + [0.]])) def test_spearmanr_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.breast[0].x, self.breast[1].x, axis=1), - np.array([[0.5083333333333333]])) - np.testing.assert_almost_equal(self.dist(self.breast[:2].X), - np.array([[0., 0.5083333333333333], - [0.5083333333333333, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[2].x, self.breast[:3].X), - np. array([[0.075, 0.3833333, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3].X, self.breast[2].x), - np. array([[0.075], - [0.3833333], - [0.]])) - - + np.testing.assert_almost_equal( + self.dist(self.breast[0].x, self.breast[1].x, axis=1), + np.array([[0.5083333333333333]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2].X), + np.array([[0., 0.5083333333333333], + [0.5083333333333333, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[2].x, self.breast[:3].X), + np.array([[0.075, 0.3833333, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3].X, self.breast[2].x), + np.array([[0.075], + [0.3833333], + [0.]])) + + +# noinspection PyTypeChecker class TestSpearmanRAbsolute(TestCase): @classmethod def setUpClass(cls): @@ -481,56 +607,76 @@ def setUpClass(cls): cls.dist = SpearmanRAbsolute def test_spearmanrabsolute_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1]), - np.array([[0.49166666666666664]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1], axis=1), - np.array([[0.49166666666666664]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1]), + np.array([[0.49166666666666664]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1], axis=1), + np.array([[0.49166666666666664]])) def test_spearmanrabsolute_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.breast[:2]), - np.array([[0., 0.49166667], - [0.49166667, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], axis=0), - np.array([[0., 0.25, 0., 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], - [0., 0.25, 0., 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], - [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0., 0.25, 0.], - [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], - [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0., 0.25, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], self.breast[:4]), - np.array([[0., 0.49166667, 0.075, 0.38333333], - [0.49166667, 0., 0.38333333, 0.46666667], - [0.075, 0.38333333, 0., 0.36666667]])) - np.testing.assert_almost_equal(self.dist(self.breast[3], self.breast[:4]), - np.array([[0.3833333, 0.4666667, 0.3666667, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:4], self.breast[3]), - np.array([[0.3833333], - [0.4666667], - [0.3666667], - [0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2]), + np.array([[0., 0.49166667], + [0.49166667, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], axis=0), + np.array([[0., 0.25, 0., 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], + [0., 0.25, 0., 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], + [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0., 0.25, 0.], + [0.25, 0., 0.25, 0., 0., 0., 0.25, 0., 0.25], + [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0., 0.25, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], self.breast[:4]), + np.array([[0., 0.49166667, 0.075, 0.38333333], + [0.49166667, 0., 0.38333333, 0.46666667], + [0.075, 0.38333333, 0., 0.36666667]])) + np.testing.assert_almost_equal( + self.dist(self.breast[3], self.breast[:4]), + np.array([[0.3833333, 0.4666667, 0.3666667, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:4], self.breast[3]), + np.array([[0.3833333], + [0.4666667], + [0.3666667], + [0.]])) def test_spearmanrabsolute_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.breast[0].x, self.breast[1].x, axis=1), - np.array([[0.49166666666666664]])) - np.testing.assert_almost_equal(self.dist(self.breast[:2].X), - np.array([[0., 0.49166667], - [0.49166667, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[3].x, self.breast[:4].X), - np.array([[0.3833333, 0.4666667, 0.3666667, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:4].X, self.breast[3].x), - np.array([[0.3833333], - [0.4666667], - [0.3666667], - [0.]])) - - + np.testing.assert_almost_equal( + self.dist(self.breast[0].x, self.breast[1].x, axis=1), + np.array([[0.49166666666666664]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2].X), + np.array([[0., 0.49166667], + [0.49166667, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[3].x, self.breast[:4].X), + np.array([[0.3833333, 0.4666667, 0.3666667, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:4].X, self.breast[3].x), + np.array([[0.3833333], + [0.4666667], + [0.3666667], + [0.]])) + + +# noinspection PyTypeChecker class TestPearsonR(TestCase): @classmethod def setUpClass(cls): @@ -538,53 +684,76 @@ def setUpClass(cls): cls.dist = PearsonR def test_pearsonr_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1]), np.array([[0.48462293898088876]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1], axis=1), - np.array([[0.48462293898088876]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1]), + np.array([[0.48462293898088876]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1], axis=1), + np.array([[0.48462293898088876]])) def test_pearsonr_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.breast[:2]), - np.array([[0., 0.48462294], - [0.48462294, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:20], axis=0), - np.array([[0., 0.10239274, 0.12786763, 0.13435117, 0.15580385, 0.27429811, 0.21006195, 0.24072005, 0.42847752], - [0.10239274, 0., 0.01695375, 0.10313851, 0.1138925, 0.16978203, 0.1155948, 0.08043531, 0.43326547], - [0.12786763, 0.01695375, 0., 0.16049178, 0.13692762, 0.21784201, 0.11607395, 0.06493949, 0.46590168], - [0.13435117, 0.10313851, 0.16049178, 0., 0.07181648, 0.15585667, 0.13891172, 0.21622332, 0.37404826], - [0.15580385, 0.1138925, 0.13692762, 0.07181648, 0., 0.16301705, 0.17324382, 0.21452448, 0.42283252], - [0.27429811, 0.16978203, 0.21784201, 0.15585667, 0.16301705, 0., 0.25512861, 0.29560909, 0.42766076], - [0.21006195, 0.1155948, 0.11607395, 0.13891172, 0.17324382, 0.25512861, 0., 0.14419442, 0.57976119], - [0.24072005, 0.08043531, 0.06493949, 0.21622332, 0.21452448, 0.29560909, 0.14419442, 0., 0.45930368], - [0.42847752, 0.43326547, 0.46590168, 0.37404826, 0.42283252, 0.42766076, 0.57976119, 0.45930368, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], self.breast[:4]), - np.array([[0., 0.48462294, 0.10133593, 0.5016744], - [0.48462294, 0., 0.32783865, 0.57317387], - [0.10133593, 0.32783865, 0., 0.63789635]])) - np.testing.assert_almost_equal(self.dist(self.breast[2], self.breast[:3]), - np.array([[0.10133593, 0.32783865, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], self.breast[2]), - np.array([[0.10133593], - [0.32783865], - [0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2]), + np.array([[0., 0.48462294], + [0.48462294, 0.]])) + # pylint: disable=line-too-long + # Because it looks better + np.testing.assert_almost_equal( + self.dist(self.breast[:20], axis=0), + np.array([[0., 0.10239274, 0.12786763, 0.13435117, 0.15580385, 0.27429811, 0.21006195, 0.24072005, 0.42847752], + [0.10239274, 0., 0.01695375, 0.10313851, 0.1138925, 0.16978203, 0.1155948, 0.08043531, 0.43326547], + [0.12786763, 0.01695375, 0., 0.16049178, 0.13692762, 0.21784201, 0.11607395, 0.06493949, 0.46590168], + [0.13435117, 0.10313851, 0.16049178, 0., 0.07181648, 0.15585667, 0.13891172, 0.21622332, 0.37404826], + [0.15580385, 0.1138925, 0.13692762, 0.07181648, 0., 0.16301705, 0.17324382, 0.21452448, 0.42283252], + [0.27429811, 0.16978203, 0.21784201, 0.15585667, 0.16301705, 0., 0.25512861, 0.29560909, 0.42766076], + [0.21006195, 0.1155948, 0.11607395, 0.13891172, 0.17324382, 0.25512861, 0., 0.14419442, 0.57976119], + [0.24072005, 0.08043531, 0.06493949, 0.21622332, 0.21452448, 0.29560909, 0.14419442, 0., 0.45930368], + [0.42847752, 0.43326547, 0.46590168, 0.37404826, 0.42283252, 0.42766076, 0.57976119, 0.45930368, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], self.breast[:4]), + np.array([[0., 0.48462294, 0.10133593, 0.5016744], + [0.48462294, 0., 0.32783865, 0.57317387], + [0.10133593, 0.32783865, 0., 0.63789635]])) + np.testing.assert_almost_equal( + self.dist(self.breast[2], self.breast[:3]), + np.array([[0.10133593, 0.32783865, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], self.breast[2]), + np.array([[0.10133593], + [0.32783865], + [0.]])) def test_pearsonr_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.breast[0].x, self.breast[1].x, axis=1), - np.array([[0.48462293898088876]])) - np.testing.assert_almost_equal(self.dist(self.breast[:2].X), - np.array([[0., 0.48462294], - [0.48462294, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[2].x, self.breast[:3].X), - np.array([[0.10133593, 0.32783865, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3].X, self.breast[2].x), - np.array([[0.10133593], - [0.32783865], - [0.]])) - - + np.testing.assert_almost_equal( + self.dist(self.breast[0].x, self.breast[1].x, axis=1), + np.array([[0.48462293898088876]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2].X), + np.array([[0., 0.48462294], + [0.48462294, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[2].x, self.breast[:3].X), + np.array([[0.10133593, 0.32783865, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3].X, self.breast[2].x), + np.array([[0.10133593], + [0.32783865], + [0.]])) + + +# noinspection PyTypeChecker class TestPearsonRAbsolute(TestCase): @classmethod def setUpClass(cls): @@ -592,51 +761,74 @@ def setUpClass(cls): cls.dist = PearsonRAbsolute def test_pearsonrabsolute_distance_one_example(self): - np.testing.assert_almost_equal(self.dist(self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0]), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[0], axis=1), np.array([[0]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1]), np.array([[0.48462293898088876]])) - np.testing.assert_almost_equal(self.dist(self.breast[0], self.breast[1], axis=1), - np.array([[0.48462293898088876]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0]), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[0], axis=1), + np.array([[0]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1]), + np.array([[0.48462293898088876]])) + np.testing.assert_almost_equal( + self.dist(self.breast[0], self.breast[1], axis=1), + np.array([[0.48462293898088876]])) def test_pearsonrabsolute_distance_many_examples(self): - np.testing.assert_almost_equal(self.dist(self.breast[:2]), - np.array([[0., 0.48462294], - [0.48462294, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:20], axis=0), - np.array([[0., 0.10239274, 0.12786763, 0.13435117, 0.15580385, 0.27429811, 0.21006195, 0.24072005, 0.42847752], - [0.10239274, 0., 0.01695375, 0.10313851, 0.1138925, 0.16978203, 0.1155948, 0.08043531, 0.43326547], - [0.12786763, 0.01695375, 0., 0.16049178, 0.13692762, 0.21784201, 0.11607395, 0.06493949, 0.46590168], - [0.13435117, 0.10313851, 0.16049178, 0., 0.07181648, 0.15585667, 0.13891172, 0.21622332, 0.37404826], - [0.15580385, 0.1138925, 0.13692762, 0.07181648, 0., 0.16301705, 0.17324382, 0.21452448, 0.42283252], - [0.27429811, 0.16978203, 0.21784201, 0.15585667, 0.16301705, 0., 0.25512861, 0.29560909, 0.42766076], - [0.21006195, 0.1155948, 0.11607395, 0.13891172, 0.17324382, 0.25512861, 0., 0.14419442, 0.42023881], - [0.24072005, 0.08043531, 0.06493949, 0.21622332, 0.21452448, 0.29560909, 0.14419442, 0., 0.45930368], - [0.42847752, 0.43326547, 0.46590168, 0.37404826, 0.42283252, 0.42766076, 0.42023881, 0.45930368, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:3], self.breast[:4]), - np.array([[0., 0.48462294, 0.10133593, 0.4983256], - [0.48462294, 0., 0.32783865, 0.42682613], - [0.10133593, 0.32783865, 0., 0.36210365]])) - np.testing.assert_almost_equal(self.dist(self.breast[2], self.breast[:3]), - np.array([[0.10133593, 0.32783865, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:2], self.breast[3]), - np.array([[0.4983256], - [0.42682613]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2]), + np.array([[0., 0.48462294], + [0.48462294, 0.]])) + # pylint: disable=line-too-long + # Because it looks better + np.testing.assert_almost_equal( + self.dist(self.breast[:20], axis=0), + np.array([[0., 0.10239274, 0.12786763, 0.13435117, 0.15580385, 0.27429811, 0.21006195, 0.24072005, 0.42847752], + [0.10239274, 0., 0.01695375, 0.10313851, 0.1138925, 0.16978203, 0.1155948, 0.08043531, 0.43326547], + [0.12786763, 0.01695375, 0., 0.16049178, 0.13692762, 0.21784201, 0.11607395, 0.06493949, 0.46590168], + [0.13435117, 0.10313851, 0.16049178, 0., 0.07181648, 0.15585667, 0.13891172, 0.21622332, 0.37404826], + [0.15580385, 0.1138925, 0.13692762, 0.07181648, 0., 0.16301705, 0.17324382, 0.21452448, 0.42283252], + [0.27429811, 0.16978203, 0.21784201, 0.15585667, 0.16301705, 0., 0.25512861, 0.29560909, 0.42766076], + [0.21006195, 0.1155948, 0.11607395, 0.13891172, 0.17324382, 0.25512861, 0., 0.14419442, 0.42023881], + [0.24072005, 0.08043531, 0.06493949, 0.21622332, 0.21452448, 0.29560909, 0.14419442, 0., 0.45930368], + [0.42847752, 0.43326547, 0.46590168, 0.37404826, 0.42283252, 0.42766076, 0.42023881, 0.45930368, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:3], self.breast[:4]), + np.array([[0., 0.48462294, 0.10133593, 0.4983256], + [0.48462294, 0., 0.32783865, 0.42682613], + [0.10133593, 0.32783865, 0., 0.36210365]])) + np.testing.assert_almost_equal( + self.dist(self.breast[2], self.breast[:3]), + np.array([[0.10133593, 0.32783865, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2], self.breast[3]), + np.array([[0.4983256], + [0.42682613]])) def test_pearsonrabsolute_distance_numpy(self): - np.testing.assert_almost_equal(self.dist(self.breast[0].x, self.breast[1].x, axis=1), - np.array([[0.48462293898088876]])) - np.testing.assert_almost_equal(self.dist(self.breast[:2].X), - np.array([[0., 0.48462294], - [0.48462294, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[2].x, self.breast[:3].X), - np.array([[0.10133593, 0.32783865, 0.]])) - np.testing.assert_almost_equal(self.dist(self.breast[:2].X, self.breast[3].x), - np.array([[0.4983256], - [0.42682613]])) - - + np.testing.assert_almost_equal( + self.dist(self.breast[0].x, self.breast[1].x, axis=1), + np.array([[0.48462293898088876]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2].X), + np.array([[0., 0.48462294], + [0.48462294, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[2].x, self.breast[:3].X), + np.array([[0.10133593, 0.32783865, 0.]])) + np.testing.assert_almost_equal( + self.dist(self.breast[:2].X, self.breast[3].x), + np.array([[0.4983256], + [0.42682613]])) + + +# noinspection PyTypeChecker # Pylint doesn't get magic __new__ operators # pylint: disable=not-callable class TestMahalanobis(TestCase): @@ -652,7 +844,8 @@ def test_correctness(self): d = scipy.spatial.distance.squareform(d) for i in range(self.n): for j in range(self.n): - self.assertAlmostEqual(d[i][j], mah(self.x[i], self.x[j]), delta=1e-5) + self.assertAlmostEqual(d[i][j], mah(self.x[i], self.x[j]), + delta=1e-5) def test_attributes(self): metric = MahalanobisDistance(self.x)