当前位置:网站首页>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
边栏推荐
- golang-gin - graceful restart
- Productivity Tools and Plugins
- 集群的安全模式
- 分布式锁有哪些,怎么实现(分布式锁的三种实现的对比)
- PyQt5 rapid development and actual combat 10.1 Get city weather forecast
- 365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
- C# 中的Async 和 Await 的用法详解
- Flutter keyboard visibility
- Selenium IDE for Selenium Automation Testing
- TensorRT安装及使用教程「建议收藏」
猜你喜欢
C#控件StatusStrip使用
IDEA的database使用教程(使用mysql数据库)
Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"
C#控件ListView用法
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
PyQt5快速开发与实战10.2 复利计算 && 10.3 刷新博客点击量
MATLAB | 我也做了一套绘图配色可视化模板
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
centos7安装mysql5.7
随机推荐
生产力工具和插件
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
模拟量差分和单端(iou计算方法)
PHP Serialization: eval
golang八股文整理(持续搬运)
AMBA APB学习记录(AMBA 3/4)
FastAPI encapsulates a generic response
FIFO深度计算学习记录(汇总)
PyQt5快速开发与实战10.2 复利计算 && 10.3 刷新博客点击量
anaconda虚拟环境安装pytorch gpu版本
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
The operator,
ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
Flutter键盘可见性
0X7FFFFFFF,0X80000000「建议收藏」
深入浅出边缘云 | 4. 生命周期管理
golang中使用泛型
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
Spark学习:为Spark Sql添加自定义优化规则