yablog: calculate cosine with python numpy

yablog: calculate cosine with python numpy

yablog: calculate cosine with python numpy

calculate cosine with python numpy

purpose

Calculate “cosine” determined by pair of vectors using python and its package named numpy. Firstly I show you the definition of cosine in linear space, and Secondly I share sample python code for calculating cosine.

definition of cosine in linear space


yablog: calculate cosine with python numpy


yablog: calculate cosine with python numpy


yablog: calculate cosine with python numpy

python code for calculating cosine

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import
numpy
 
def
get_cosine(v1, v2):
    
""" calculate cosine and returns cosine """
    
n1
=
get_norm_of_vector(v1)
    
n2
=
get_norm_of_vector(v2)
    
ip
=
get_inner_product(v1, v2)
    
return
ip
/
(n1
*
n2)
 
def
get_inner_product(v1, v2):
    
""" calculate inner product """
    
return
numpy.dot(v1, v2)
 
def
get_norm_of_vector(v):
    
""" calculate norm of vector """
    
return
numpy.linalg.norm(v)
 
def
get_radian_from_cosine(cos):
    
return
numpy.arccos(cos)
 
def
get_degrees_from_radian(cos):
    
return
numpy.degrees(cos)
 
def
main():
    
v1
=
numpy.array([
1
,
0
])
    
v2
=
numpy.array([
1
, numpy.sqrt(
3
)])
    
cosine
=
get_cosine(v1, v2)
    
radian
=
get_radian_from_cosine(cosine)
    
print
get_degrees_from_radian(radian)
 
if
__name__
=
=
"__main__"
:
    
main()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110267.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从零开始搭建 web 聊天室(一)

    从零开始搭建 web 聊天室(一)本篇将介绍如何快速、简便地使用socket.io库搭建一个web在线聊天室。前端并没有使用任何框架。后端使用express框架搭建简易的后端。socket.io库本质上是基于websocket上进行封装。改变了以往只能前端发送请求,后端才能返回给前端信息,这样的一问一答形式。实现了前后端双向通信,即后端也可以主动push信息到前端。websocket尤其适用于在线聊天或者实时交互的场景。已经广泛用于直播平台、视频平台等。本篇实现:最基本的前后端信息交互。代码地址:https

    2022年6月22日
    21
  • shell脚本之awk数组

    shell脚本之awk数组一.数组格式数组是一个包含一系列元素的表.格式如下:abc[1]=”xiaohong”abc[2]=”xiaolan”解释:abc:为数组名称[1]、[2]:为数组元素下标,可以理解为数组的第1个元素、数组的第2个元素xiaohong、xiaolan:元素值例子1:定义数组,并且打印数组元素[root@tab0~]#awk’BEGIN{a[0]=”xiaohong”;a[1]=”xiaolan”;printa[0]}’xiaohong[root@tab0~]#awk

    2022年7月19日
    21
  • windows的host文件的位置和作用建议收藏

    在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\

    2021年12月20日
    74
  • 网页游戏开发入门教程一(webgame+design)

    网页游戏开发入门教程一(webgame+design)网页游戏开发入门教程一(webgame+design) 一、简单的程序框架。webgame程序构成:三大部分。第一是数据流程。第二是程序。第三是美术。其中,数据流程包括了功能。也只有在功能中才能体现数据流程。数据流程相当的麻烦,后面再讨论。比如最简单的卖买产品。要实现这个功能。那么需要有产品基础表、产品详细表、商店表、背包表。如果扩展性更强,相应

    2022年5月2日
    53
  • kali命令大全

    kali命令大全arch显示机器的处理器架构(1)uname-m显示机器的处理器架构(2)uname-r显示正在使用的内核版本dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/sda在磁盘上执行测试性读取操作cat/proc/cpuinfo显示CPUinfo的信息c…

    2022年6月9日
    68
  • css3元素简单的闪烁效果(html5 jquery)

    css3Animation:@-webkit-keyframestwinkling{/*透明度由0到1*/0%{opacity:0;/*透明度为0*/}100%{opacity:1

    2021年12月20日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号