python - 需要帮助编写余弦相似度的测试用例
问题描述
这是我在下面制作的功能。
def cosine_similarity(a,b):
"""
Determins the similarity between two vectors, by computing the cosine of the angle between them
:param Union[ndarray, Iterable, int, float] a: a non zero numeric vector
:param Union[ndarray, Iterable, int, float] b: a non zero numeric vector
:return: ndarray
"""
norm_a = numpy.linalg.norm(a)
norm_b = numpy.linalg.norm(b)
if norm_a ==0 or norm_b ==0:
raise ZeroDivisionError("Can not find the cosine between two vectors with following norms:{} {}".format(norm_a,norm_b))
return numpy.dot(a,b)/(norm_a * norm_b)
我在编写测试用例方面需要帮助 我已经开始了它,但是因为我从未编写过测试用例而迷失了方向
class TestCosineSimilarity(TestCase):
def test_cosine_similarity_with_zero(self):
解决方案
推荐阅读
- amazon-web-services - AWS Cognito 服务 API?放大 => Javascript SDK Angular 应用
- java - 如何在不禁用 load-external-dtd 功能的情况下使用 java 修改 xml 文件?
- java - 在新选项卡中打开 PDF 中的链接
- matlab - 轮廓坐标
- python - 多用户烧瓶应用程序的最佳实践
- spring-mvc - Spring Integration - Dispatcher 没有订阅者
- javascript - 直接使用 JS 源映射?
- go - 使用原始文件的共享外部包?
- java - 包装 byte[] 的 ImageInputStreamImpl 实现
- ios - 用 Objective-C 中 MKAnnotation 中的 customButton 替换 rightCalloutAccessoryView 中的 detailDisclosureButton