Ejemplo n.º 1
0
    def test_cosine_similarity_specific_values(self, cosine_similarity):
        arr0 = np.array([[0, 0.5, -1., 3, 0]])
        arr1 = 2 * arr0
        arr2 = -1 * arr0

        assert np.isclose(cosine_similarity(arr0, arr1), 1.)
        assert np.isclose(cosine_similarity(arr0, arr2), -1.)
Ejemplo n.º 2
0
    def test_cosine_similarity_incompatible_shapes(self, cosine_similarity):
        arr0 = np.zeros((100, 45))
        arr1 = np.zeros((200, 46))
        with pytest.raises(ValueError) as exc:
            cosine_similarity(arr0, arr1)

        assert str(exc.value) == "Incompatible shapes: 45 vs 46."
Ejemplo n.º 3
0
    def test_cosine_similarity_specific_values_2d(self, cosine_similarity):
        arr0 = np.array([
            [0, 0.5, -1., 3, 0],
            [0, 0.5, -1., 3, 0],
        ])
        arr1 = np.array([[-1], [1]]) * arr0

        assert np.allclose(cosine_similarity(arr0, arr1), [-1., 1])
Ejemplo n.º 4
0
 def test_cosine_similarity_comparison_scipy(self, cosine_similarity, vec):
     arr0 = vec[:20].reshape(1, -1)
     arr1 = vec[20:].reshape(1, -1)
     result = cosine_similarity(arr0, arr1)
     expected = 1 - cosine(arr0, arr1)
     assert np.isclose(result, expected)
Ejemplo n.º 5
0
    def test_cosine_similarity_arity_1(self, cosine_similarity):
        arr = np.random.random((50, 10))
        result_arity_1 = cosine_similarity(arr)
        result_arity_2 = cosine_similarity(arr, arr)

        assert np.allclose(result_arity_1, result_arity_2)
Ejemplo n.º 6
0
    def test_cosine_similarity_different_num_samples(self, cosine_similarity):
        arr0 = np.random.random((50, 10))
        arr1 = np.random.random((40, 10))
        result = cosine_similarity(arr0, arr1)

        assert result.shape == (50, 40)