C1W4.LAB.Vector manipulation+Hash functions and multiplanes

理论课:C1W4.Machine Translation and Document Search

文章目录

  • Python 中的矢量操作
    • Transforming vectors
      • Example 1
      • Example 2
    • Frobenius Norm
  • Hash functions and multiplanes
    • Basic Hash tables
    • Planes
    • Hash Function with multiple planes
    • Random Planes
    • Document vectors

理论课: C1W4.Machine Translation and Document Search

Python 中的矢量操作

使用 NumPy 库完成一些数组和矩阵的高级操作。

Transforming vectors

向量的变换主要有三种:

  • 缩放
  • 平移
  • 旋转

前面的课程已经应用了前两种变换。下面学习如何使用向量的基本变换,即旋转。旋转操作改变了向量的方向,但不影响其维数和常模。

Example 1

import numpy as np                     # Import numpy for array manipulation
import matplotlib.pyplot as plt        # Import matplotlib for charts
from utils_nb import plot_vectors      # Function to plot vectors (arrows)
# Create a 2 x 2 matrix
R = np.array([[2, 0],[0, -2]])x = np.array([[1, 1]]) # Create a 1 x 2 matrix

向量与方阵之间的点乘会产生原始向量的旋转和缩放。推荐的在 Python 中获取点积的方法是 np.dot(a,b):

y = np.dot(x, R) # Apply the dot product between x and R
y

结果:
array([[ 2, -2]])
这样看不是很直观,借助utils_nb.py中的plot_vectors把结果绘制出来,先在笛卡尔平面中绘制 x ⃗ = [ 1 , 1 ] \vec x = [1, 1] x =[1,1],笛卡尔平面将以 [0,0]为中心,其 x 和 y 极限将介于 [-4,+4]之间。

plot_vectors([x], axes=[4, 4], fname='transform_x.svg')

在这里插入图片描述
在相同坐标系绘制要点积矩阵后的结果(蓝色):
R o = [ 2 0 0 − 2 ] Ro = \begin{bmatrix} 2 & 0 \\ 0 & -2 \end{bmatrix} Ro=[2002]

y = x ⋅ R o = [ [ 2 , − 2 ] ] y = x \cdot Ro = [[2, -2]] y=xRo=[[2,2]]

plot_vectors([x, y], axes=[4, 4], fname='transformx_and_y.svg')

在这里插入图片描述

Example 2

接下来使用 Pyplot 直观地检测旋转对二维向量的影响。数据由 2 个真实属性组成,属于 R × R R\times R R×R R 2 R^2 R2 空间。 R 2 R^2 R2空间中的旋转矩阵将会使给定向量 x ⃗ \vec x x 顺时针旋转一个角度 θ \theta θ,旋转矩阵可写为:
R o = [ c o s θ − s i n θ s i n θ c o s θ ] Ro = \begin{bmatrix} cos \theta & -sin \theta \\ sin \theta & cos \theta \end{bmatrix} Ro=[cosθsinθsinθcosθ]
顺时针旋转可写成:
y = x ⋅ R o y = x \cdot Ro y=xRo
逆时针旋转:
y = R o ⋅ x T y = Ro \cdot x^T y=RoxT
注意:在Numpy中使用的是弧度不是角度,例如:以下代码定义一个旋转矩阵,该矩阵可将向量旋转 10 0 o 100^o 100o

angle = 100 * (np.pi / 180) #convert degrees to radiansRo = np.array([[np.cos(angle), -np.sin(angle)],[np.sin(angle), np.cos(angle)]])x2 = np.array([2, 2]).reshape(1, -1) # make it a row vector
y2 = np.dot(x2, Ro)print('Rotation matrix')
print(Ro)
print('\nRotated vector')
print(y2)print('\n x2 norm', np.linalg.norm(x2))
print('\n y2 norm', np.linalg.norm(y2))
print('\n Rotation matrix norm', np.linalg.norm(Ro))

结果:
Rotation matrix
[[-0.17364818 -0.98480775]
[ 0.98480775 -0.17364818]]

Rotated vector
[[ 1.62231915 -2.31691186]]

x2 norm 2.8284271247461903

y2 norm 2.82842712474619

Rotation matrix norm 1.414213562373095
可视化后:

plot_vectors([x2, y2], fname='transform_02.svg')

在这里插入图片描述
注意:
1.输入向量的范数与输出向量的范数相同。旋转矩阵不会改变向量的范数,只会改变其方向。
2.任何 R 2 R^2 R2空间的旋转矩阵的范数均为: 2 = 1.414221 \sqrt 2 = 1.414221 2 =1.414221

Frobenius Norm

Frobenius范数是 R 2 R^2 R2向量的模一般化表达:
∥ a ⃗ ∥ = a ⃗ ⋅ a ⃗ \| \vec a \| = \sqrt {{\vec a} \cdot {\vec a}} a =a a
对于给定的 R 2 R^2 R2 矩阵 A,Frobenius范数的定义为:
∥ A ∥ F ≡ ∑ i = 1 m ∑ j = 1 n ∣ a i j ∣ 2 \|\mathrm{A}\|_{F} \equiv \sqrt{\sum_{i=1}^{m} \sum_{j=1}^{n}\left|a_{i j}\right|^{2}} AFi=1mj=1naij2
下面是手工计算:

A = np.array([[2, 2],[2, 2]])

先算各个元素平方:

A_squared = np.square(A)
A_squared

在累加后开方:

A_Frobenius = np.sqrt(np.sum(A_squared))
A_Frobenius

当然有现成的函数:np.linalg.norm()

print('Frobenius norm of the Rotation matrix')
print(np.sqrt(np.sum(Ro * Ro)), '== ', np.linalg.norm(Ro))

Hash functions and multiplanes

使用哈希函数进行查找的一个关键点是计算为给定条目分配的哈希密钥或桶 ID。下面我们将学习:

  • 基本哈希表
  • 多平面
  • 随机平面

Basic Hash tables

先导入包

import numpy as np                # library for array and matrix manipulation
import pprint                     # utilities for console printing 
from utils_nb import plot_vectors # helper function to plot vectors
import matplotlib.pyplot as plt   # visualization librarypp = pprint.PrettyPrinter(indent=4) # Instantiate a pretty printer

下面代码定义了一个直接用于整数的哈希函数。函数将接收一个整数列表和所需的散列数量。函数将生成一个以字典形式存储的哈希表,其中键包含哈希键,值提供输入列表中的哈希元素。
哈希函数只是每个元素与所需的桶数之间的整除余数。

def basic_hash_table(value_l, n_buckets):def hash_function(value, n_buckets):return int(value) % n_bucketshash_table = {i:[] for i in range(n_buckets)} # Initialize all the buckets in the hash table as empty listsfor value in value_l:hash_value = hash_function(value,n_buckets) # Get the hash key for the given valuehash_table[hash_value].append(value) # Add the element to the corresponding bucketreturn hash_table

测试:

value_l = [100, 10, 14, 17, 97] # Set of values to hash
hash_table_example = basic_hash_table(value_l, n_buckets=10)
pp.pprint(hash_table_example)

结果:
{ 0: [100, 10],
1: [],
2: [],
3: [],
4: [14],
5: [],
6: [],
7: [17, 97],
8: [],
9: []}
这里的桶键是每个数字最右边的数字。

Planes

多平面散列函数(Multi-plane Hashing,简称MPH)是一种哈希函数的设计方法,它通过将输入数据分割成多个部分,并在不同的平面上独立地对这些部分进行哈希处理,然后将结果组合起来生成最终的哈希值。这种方法在某些应用中可以提高哈希函数的性能和安全性。在下面的代码中,我们将展示多平面原理的最基本形式。首先是单平面:

P = np.array([[1, 1]]) # Define a single plane. 
fig, ax1 = plt.subplots(figsize=(8, 8)) # Create a plotplot_vectors([P], axes=[2, 2], ax=ax1) # Plot the plane P as a vector# Plot  random points. 
for i in range(0, 10):v1 = np.array(np.random.uniform(-2, 2, 2)) # Get a pair of random numbers between -2 and 2side_of_plane = np.sign(np.dot(P, v1.T)) # Color the points depending on the sign of the result of np.dot(P, point.T)if side_of_plane == 1:ax1.plot([v1[0]], [v1[1]], 'bo') # Plot blue pointselse:ax1.plot([v1[0]], [v1[1]], 'ro') # Plot red pointsplt.show()

结果:
在这里插入图片描述
上图中可以看到,定义平面的矢量并不是平面两边的边界。它标记的是找到平面 "正 "边的方向,这样看其实非常不直观。可用一条线来把这个面表示出来:

P = np.array([[1, 2]])  # Define a single plane. You may change the direction# Get a new plane perpendicular to P. We use a rotation matrix
PT = np.dot([[0, 1], [-1, 0]], P.T).T  fig, ax1 = plt.subplots(figsize=(8, 8)) # Create a plot with custom sizeplot_vectors([P], colors=['b'], axes=[2, 2], ax=ax1) # Plot the plane P as a vector# Plot the plane P as a 2 vectors. 
# We scale by 2 just to get the arrows outside the current box
plot_vectors([PT * 4, PT * -4], colors=['k', 'k'], axes=[4, 4], ax=ax1)# Plot 20 random points. 
for i in range(0, 20):v1 = np.array(np.random.uniform(-4, 4, 2)) # Get a pair of random numbers between -4 and 4 side_of_plane = np.sign(np.dot(P, v1.T)) # Get the sign of the dot product with P# Color the points depending on the sign of the result of np.dot(P, point.T)if side_of_plane == 1:ax1.plot([v1[0]], [v1[1]], 'bo') # Plot a blue pointelse:ax1.plot([v1[0]], [v1[1]], 'ro') # Plot a red pointplt.show()

结果:
在这里插入图片描述
下面对几个点进行计算,判断其相对平面的位置:

P = np.array([[1, 1]])      # Single plane
v1 = np.array([[1, 2]])     # Sample point 1
v2 = np.array([[-1, 1]])    # Sample point 2
v3 = np.array([[-2, -1]])   # Sample point 3
np.dot(P, v1.T)

结果:array([[3]])

np.dot(P, v2.T)

结果:array([[0]])

np.dot(P, v3.T)

结果:array([[-3]])

把这块判断写成一个函数:

def side_of_plane(P, v):dotproduct = np.dot(P, v.T) # Get the dot product P * v'sign_of_dot_product = np.sign(dotproduct) # The sign of the elements of the dotproduct matrix sign_of_dot_product_scalar = sign_of_dot_product.item() # The value of the first itemreturn sign_of_dot_product_scalar

判断实例:

side_of_plane(P, v1) # In which side is [1, 2]

结果:1

side_of_plane(P, v2) # In which side is [-1, 1]

结果:0

side_of_plane(P, v3) # In which side is [-2, -1]

结果:-1

Hash Function with multiple planes

下面代码在二维平面上定义了三个平面:

P1 = np.array([[1, 1]])   # First plane 2D
P2 = np.array([[-1, 1]])  # Second plane 2D
P3 = np.array([[-1, -1]]) # Third plane 2D
P_l = [P1, P2, P3]  # List of arrays. It is the multi plane# Vector to search
v = np.array([[2, 2]])

然后可以定义函数,查找想所在平面,它接受两个参数:平面列表 P_l 和要搜索的向量 。

# 定义多平面散列函数
def hash_multi_plane(P_l, v):# 初始化哈希值hash_value = 0# 遍历每个平面for i, P in enumerate(P_l):# 计算向量v相对于平面P的位置,这里假设side_of_plane函数存在并返回一个整数# 该整数表示向量v在平面的哪一侧:1表示同侧,-1表示异侧,0表示在平面上sign = side_of_plane(P, v)# 根据sign的值,设置当前平面的哈希位为0或1hash_i = 1 if sign >= 0 else 0# 将当前平面的哈希位左移i位(i为当前平面的索引),然后加到总哈希值上# 这样做可以确保每个平面的贡献被编码到哈希值的不同位上hash_value += 2**i * hash_i# 返回计算得到的哈希值return hash_value# 调用多平面散列函数,传入平面列表P_l和向量v,计算向量v的哈希值
# 注意:这里side_of_plane函数需要被定义,否则代码将无法运行
print(hash_multi_plane(P_l, v))

结果:3

Random Planes

先创建3个随机平面:

np.random.seed(0)
num_dimensions = 2 # is 300 in assignment
num_planes = 3 # is 10 in assignment
random_planes_matrix = np.random.normal(size=(num_planes,num_dimensions))
print(random_planes_matrix)
# 定义一个向量v,它将用于计算它相对于每个平面的位置
v = np.array([[2, 2]])

结果:
[[ 1.76405235 0.40015721]
[ 0.97873798 2.2408932 ]
[ 1.86755799 -0.97727788]]

# 定义一个函数side_of_plane_matrix,它接受一个平面矩阵P和一个向量v
def side_of_plane_matrix(P, v):# 使用numpy的dot函数计算平面矩阵P和向量v的转置(即每个平面的法向量与向量v的点积)dotproduct = np.dot(P, v.T)# 使用numpy的sign函数获取点积的符号,这将返回一个与dotproduct形状相同的矩阵# 矩阵中的每个元素将是-1、0或1,分别表示点积是负数、零或正数sign_of_dot_product = np.sign(dotproduct) # 返回点积的符号矩阵return sign_of_dot_product

对向量[2, 2]判断其对以上随机平面的位置。

sides_l = side_of_plane_matrix(random_planes_matrix, v)
sides_l

结果:
array([[1.],
[1.],
[1.]])
使用上面函数定义多平面哈希函数:

def hash_multi_plane_matrix(P, v, num_planes):sides_matrix = side_of_plane_matrix(P, v) # Get the side of planes for P and vhash_value = 0for i in range(num_planes):sign = sides_matrix[i].item() # Get the value inside the matrix cellhash_i = 1 if sign >=0 else 0hash_value += 2**i * hash_i # sum 2^i * hash_ireturn hash_value

对向量v = [2, 2]进行测试:

hash_multi_plane_matrix(random_planes_matrix, v, num_planes)

结果:
7

Document vectors

思考下面代码什么作用?

word_embedding = {"I": np.array([1,0,1]),"love": np.array([-1,0,1]),"learning": np.array([1,0,1])}
words_in_document = ['I', 'love', 'learning', 'not_a_word']
document_embedding = np.array([0,0,0])
for word in words_in_document:document_embedding += word_embedding.get(word,0)print(document_embedding)

结果:[1 0 3]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3245644.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

苹果x怎么录屏?手把手教你操作

随着社交媒体和视频平台的兴起,人们越来越习惯于通过视频来分享生活点滴、传播信息。苹果X手机凭借其出色的性能和高清屏幕,成为了许多用户录制屏幕视频的首选设备。可苹果x怎么录屏呢?本文将详细介绍苹果x手机的内置录屏方法,通过…

Blender使用(二)点线面基本操作

Blender使用之点线面 1.编辑模式 tab键进行切换,为了方便菜单调出,可以设置键位映射为拖动时的饼菜单。 设置好后,按住tab键移动鼠标(注意不要点击鼠标),即可弹出编辑菜单。 默认是点模式,在左上角可进行点线面的切换…

【linux高级IO(三)】初识epoll

💓博主CSDN主页:杭电码农-NEO💓   ⏩专栏分类:Linux从入门到精通⏪   🚚代码仓库:NEO的学习日记🚚   🌹关注我🫵带你学更多操作系统知识   🔝🔝 Linux高级IO 1. 前言2. 初识e…

帕金森营养宝典,守护你的健康每一天!

👋 嗨,亲爱的小伙伴们!今天我们来聊聊一个有点“严肃”但超级重要的话题——帕金森患者应该补充哪些营养?🤔 💪 首先,我们要知道,帕金森是一种常见的神经系统疾病,它可能…

自制一个指定容量缓存,并实现最近使用的最后删除

需求 实现一个缓存key-value结构,并设定容量最大值,当put进去的时候,如果超过最大容量,则将最早放进去的删除当get的时候,返回key值,并将key放到后面(表示最近使用过,最后删除&…

mp3音乐剪辑软件大盘点,安利7款小白必备的免费mp3剪辑软件(详解)

mp3现在是最常见的音频格式,无处不在,包括下载音乐、播放播客、保存有声读物等等。因此,拥有一款强大的mp3音乐剪辑软件至关重要。使用mp3剪辑软件,你可以修剪、合并、压缩,甚至转换mp3为其他常见音频格式。所以&#…

openstack设置IP直接登录,不需要加dashboard后缀

openstack 实验环境,openstack-t版,centos2009 修改配置文件 [rootcontroller ~]# vim /WEBROOT /etc/openstack-dashboard/local_settings #将dashboard去掉 WEBROOT /dashboard/ #改为 WEBROOT /[rootcontroller ~]# vim /etc/httpd/conf.d/openst…

智能优化算法之禁忌搜索(Tabu Search, TS)

上图展示的是使用禁忌搜索算法解决电力系统孤岛问题。该问题旨在在发生严重扰动后将电力系统划分为几个不同的孤岛,目标是在将相关发电机放置在同一孤岛中并保持每个孤岛的连通性的同时,最小化所有孤岛的总发电负荷不平衡状态。 禁忌搜索(Ta…

ARM架构(一)—— ARMV8V9基础概念

目录 1.ARMCore的时间线2.ARM术语小结2.1 A64和arrch642.2ARM架构现在的5个系列2.3 微架构2.4 PE2.5 Banked2.6 ARM文档术语2.7 IMPLEMENTATION DEFINFD 和 DEPRECATED2.8 EL1t和EL1h 3 ARMv7的软件架构4 安全状态切换模型4.1 Secure state和Non-secure state介绍 5 Interproce…

C++(week11): C++基础 第六章:关联式容器 set、map

文章目录 第六章:关联式容器1.set(1)set的特点(2)set的构造(3)set的查找操作 (set访问元素)(4)set的插入操作、pair(5)set的遍历 2.map(1)map的特点(2)map的构造(3)map的查找操作(4)map的插入操作(5)map的下标操作 (重点)(5)map的遍历 第六章…

入度与出度在数据结构中的应用

文章目录 应用案例1. 邻接矩阵2. 邻接链表3. 邻接集(字典实现)4. 入度列表(基于邻接链表计算) 特别补充3. 邻接集计算入度(补充)4. 邻接多重表(概念介绍) 入度和出度是图论中的概念&…

手机误删图片怎么办?2个照片恢复大师来帮忙,轻松找回

手机照片早已成为我们日常生活中的一部分,记录着欢笑、泪水等各种瞬间。但有时候,因为各种原因,它们会突然消失,让人痛心疾首。照片恢复有哪些方法呢?别急,今天就给大家带来2位照片恢复大师,它们…

【手写数据库内核组件】0501多线程并发模型,任务分发多工作者执行架构实现,多线程读写状态时volatile存储类型使用技巧

0501 多线程管理 ​专栏内容: postgresql使用入门基础手写数据库toadb并发编程 个人主页:我的主页 管理社区:开源数据库 座右铭:天行健,君子以自强不息;地势坤,君子以厚德载物. 文章目录 0501 多…

[C++] 由浅入深理解面向对象思想的组成模块

文章目录 (一) 类的默认成员函数(二) 构造函数构造函数的特征构造函数示例无参构造带参构造 冲突:全缺省参数的构造函数与无参构造函数 (三)析构函数特性析构函数的析构过程解析 (四)拷贝构造函数什么是拷贝构造?特性为…

数据结构——单链表详解(超详细)(2)

前言: 上一篇文章小编简单的介绍了单链表的概念和一些函数的实现,不过为了保证文章的简洁,小编把它分成了两篇来写,这一篇小编紧接上一篇文章继续写单链表函数功能的实现: 目录: 1.单链表剩余函数的编写 1.…

MBR40150FCT-ASEMI无人机专用MBR40150FCT

编辑:ll MBR40150FCT-ASEMI无人机专用MBR40150FCT 型号:MBR40150FCT 品牌:ASEMI 封装:TO-220F 批号:最新 最大平均正向电流(IF):40A 最大循环峰值反向电压(VRRM&a…

小程序-视图与逻辑

一、页面导航 声明式导航 编程式导航 导航传参 1.声明式导航传参 2.编程式导航传参 3.在onload中接收导航参数 二、页面事件 下拉刷新 上拉触底 三、生命周期 分类 生命周期函数分类 1.应用的生命周期函数 2.页面的生命周期函数 四、WXS脚本 基础语法 wxs的特点 五、案…

智能硬件——0-1开发流程

文章目录 流程图1. 市场分析具体分析 2. 团队组建2. 团队组建早期团队配置建议配置一:基础型团队 (4人)配置二:扩展型团队 (6人)配置三:全面型团队 (7人) 3. 产品需求分析4. ID设计(Industrial Design, 工业设计)5. 结…

MathType加载项被word禁用怎么办 MathType加载到Word不能用

Word中的MathType加载项是指将MathType软件与Word文档进行关联的一项功能。它允许用户在Word中直接使用MathType的功能,方便地输入和编辑数学公式等内容。通过加载项,MathType的强大数学公式编辑能力可以与Word的文档处理功能相结合,提高工作…

谷歌账号异常的常见8种状态,前三种有望立刻恢复,越往后越难(1)

生搬硬套列夫•托尔斯泰的一句话名言“幸福的家庭都是相似的,不幸的家庭各有各的不幸。” 工作、学习、娱乐中需要使用谷歌账号的朋友可能会发现,幸福的人就是谷歌账号一直都好好的,每次登陆都如所愿,丝滑无比。但是“不幸”的人就会发现&am…