当前位置:网站首页>Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
2022-07-31 13:16:00 【BRYTLEVSON】
numpySaving and loading of matrices and vectors,And use the saved vector for similarity calculation
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
使用 arange创建array数组
a = np.arange(0, 5)
print(a) # [0 1 2 3 4]
b = np.arange(5, 15)
print(b) # [ 5 6 7 8 9 10 11 12 13 14]
b = b.reshape(2, 5)
print(b)
""" [[ 5 6 7 8 9] [10 11 12 13 14]] """
使用列表创建ndarray
li = [1, 2, 3, 4]
li1 = [[1, 2, 3, 5], [4, 5, 6, 8]]
li_arr = np.array(li) # # ndarry
print(li_arr) # [1 2 3 4]
li1_arr = np.array(li1) # ndarry
print(li1_arr)
""" [[1 2 3 5] [4 5 6 8]] """
Save a matrix or vector
# 保存方式1 保存为npy
np.save('arr.npy', li1_arr)
# 保存方式2 保存为npz npzMultiple matrix vectors can be saved 其实就是将多个npyThe packaged object is saved Default is object 键从attr1开始
np.savez('arr.npz', li1_arr, li_arr)
Load and save a matrix or vector
npy_res = np.load('arr.npy')
print(npy_res)
""" [[1 2 3 5] [4 5 6 8]] """
Calculate similarity using the read vector
def cosine_similarity_(a, b):
""" Find cosine similarity or distance :param a: :param b: :return: """
res = cosine_similarity(a.reshape(1, -1), b.reshape(1, -1))
return res[0][0]
cal_arr = np.asarray([1, 3, 4, 5])
print(cal_arr) # [1 3 4 5]
""" 计算npy_res中的每个向量与cal_arr的相似度 """
for arr in npy_res:
print(cosine_similarity_(arr, cal_arr)) # 0.9865867645279247 0.9787763071201612
""" 计算npz中的arr1A vector AND in an objectcal_arr的相似度 """
for arr in npz_res['arr_0']:
print(arr)
print(cosine_similarity_(arr, cal_arr)) # 0.9865867645279247 0.9787763071201612
完整代码
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
""" 使用 arange创建array数组 """
a = np.arange(0, 5)
print(a) # [0 1 2 3 4]
b = np.arange(5, 15)
print(b) # [ 5 6 7 8 9 10 11 12 13 14]
b = b.reshape(2, 5)
print(b)
""" [[ 5 6 7 8 9] [10 11 12 13 14]] """
""" 使用列表创建ndarray """
li = [1, 2, 3, 4]
li1 = [[1, 2, 3, 5], [4, 5, 6, 8]]
li_arr = np.array(li) # # ndarry
print(li_arr) # [1 2 3 4]
li1_arr = np.array(li1) # ndarry
print(li1_arr)
""" [[1 2 3 5] [4 5 6 8]] """
# Save a matrix or vector
# 保存方式1 保存为npy
np.save('arr.npy', li1_arr)
# 保存方式2 保存为npz npzMultiple matrix vectors can be saved 其实就是将多个npyThe packaged object is saved Default is object 键从attr1开始
np.savez('arr.npz', li1_arr, li_arr)
# 加载保存的npy文件
npy_res = np.load('arr.npy')
print(npy_res)
""" [[1 2 3 5] [4 5 6 8]] """
# 加载保存的npz文件
npz_res = np.load('arr.npz')
npz_res = dict(npz_res)
print(npz_res)
""" {'arr_0': array([[1, 2, 3, 5], [4, 5, 6, 8]]), 'arr_1': array([1, 2, 3, 4])} """
""" Calculate the similarity of the read vectors """
def cosine_similarity_(a, b):
""" Find cosine similarity or distance :param a: :param b: :return: """
res = cosine_similarity(a.reshape(1, -1), b.reshape(1, -1))
return res[0][0]
cal_arr = np.asarray([1, 3, 4, 5])
print(cal_arr) # [1 3 4 5]
""" 计算npy_res中的每个向量与cal_arr的相似度 """
for arr in npy_res:
print(cosine_similarity_(arr, cal_arr)) # 0.9865867645279247 0.9787763071201612
""" 计算npz中的arr1A vector AND in an objectcal_arr的相似度 """
for arr in npz_res['arr_0']:
print(arr)
print(cosine_similarity_(arr, cal_arr)) # 0.9865867645279247 0.9787763071201612
边栏推荐
猜你喜欢
EXCEL如何快速拆分合并单元格数据
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
系统集成项目管理工程师(软考中级)知识点总结【挣值分析】【关键路径】
docker部署完mysql无法连接
The operator,
[CPU Design Practice] Simple Pipeline CPU Design
PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
电商rpa是什么意思?跟电商rpi是一个意思吗?
尚硅谷–MySQL–基础篇(P1~P95)
随机推荐
聊聊 SAP 产品 UI 上的消息显示机制
How IDEA runs web programs
IDEA找不到Database解决方法
FIFO深度计算学习记录(汇总)
操作符详解
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
NameNode (NN) 和SecondaryNameNode (2NN)工作机制
C# 中的Async 和 Await 的用法详解
跨境电商小知识之跨境电商物流定义以及方式讲解
五种数据提交方式的优化
行业案例 | 全面防护 赛宁助力能源工控安全建设
【CPU设计实战】简单流水线CPU设计
NPM 使用介绍
SAP e-commerce cloud Spartacus SSR Optimization Engine execution sequence of several timeouts
FastAPI 封装一个通用的response
尚硅谷–MySQL–基础篇(P1~P95)
ASM外部冗余是否可以替换磁盘
知名无人驾驶公司:文远知行内推
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期