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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • URL 规范 整理

    URL 规范 整理

    2022年3月5日
    50
  • Spring Boot+Vue前后端分离项目案例

    Spring Boot+Vue前后端分离项目案例一、构建项目使用vue-cli创建项目:然后导入编辑器(我使用的是webstorm),先进行启动下,看能否访问到localhost:8080。能访问到表示使用vue-cli创建项目正常。二、进行前端代码编写记得添加修改config下 的index.js文件前端页面代码:Footer.vue<template> <…

    2022年6月13日
    25
  • js中倒计时_js倒计时特效

    js中倒计时_js倒计时特效利用setInterval制作简单的js倒计时,点击按钮将触发定时器且按钮不可用html:获取校验码JS: vari; functiongetCode(){ $(“#code”).attr({“disabled”:”disabled”}); i=self.setInterval(“countdown()”,1000); } varint=1

    2022年10月4日
    0
  • java inputstreamreader类详解_inputstreamreader是什么流

    java inputstreamreader类详解_inputstreamreader是什么流java底层从计算机读取的统统都是二进制,所以一开始我们读取的时候都是先简历连接通道,然后将文件数据以字节流的形式读取进入通道。#建立连接Filefile=newFile(“a.txt”);#二进制流的通道。FileInputStreamfileInputStream=newFileInputStream(file);#你想啊,fileInputStr…

    2022年9月26日
    0
  • h5py快速入门指南

    h5py是Python语言用来操作HDF5的模块。下面的文章主要介绍h5py的快速使用入门,翻译自h5py的官方文档:http://docs.h5py.org/en/latest/quick.html。该翻译仅为个人学习h5py为目的,如有翻译不当之处,请速联系读者或提供其它好的翻译。安装使用Anaconda或者Miniconda:condainstallh5py用Enthou…

    2022年4月4日
    100
  • Visual Studio 2015 移动跨平台开发初体验

    Visual Studio 2015 移动跨平台开发初体验

    2021年11月24日
    55

发表回复

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

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