当前位置:网站首页>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
边栏推荐
- C#中+=的用法
- PHP序列化:eval
- numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- PyQt5快速开发与实战 10.1 获取城市天气预报
- 0X7FFFFFFF,0X80000000「建议收藏」
- docker部署完mysql无法连接
- C#控件 ToolStripProgressBar 用法
- Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
- 365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
猜你喜欢
Google Chrome(谷歌浏览器)安装使用
Invalid bound statement (not found)出现的原因和解决方法
阿里三面:MQ 消息丢失、重复、积压问题,怎么解决?
IDEA如何运行web程序
How to quickly split and merge cell data in Excel
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
Network layer key protocol - IP protocol
Batch大小不一定是2的n次幂!ML资深学者最新结论
Architecture Camp | Module 8
centos7安装mysql5.7步骤(图解版)
随机推荐
P5019 [NOIP2018 提高组] 铺设道路
NameNode (NN) 和SecondaryNameNode (2NN)工作机制
sqlalchemy determines whether a field of type array has at least one consistent data with an array
Centos7 install mysql5.7 steps (graphical version)
Flutter keyboard visibility
[RPI]树莓派监控温度及报警关机保护「建议收藏」
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
Optimization of five data submission methods
go使用makefile脚本编译应用
The operator,
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
模拟量差分和单端(iou计算方法)
Error IDEA Terminated with exit code 1
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
IDEA的database使用教程(使用mysql数据库)
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
CentOS7 —— yum安装mysql
Flutter键盘可见性
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
PyQt5 rapid development and actual combat 10.1 Get city weather forecast