当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Spark学习:为Spark Sql添加自定义优化规则
Two methods of NameNode failure handling
NameNode (NN) 和SecondaryNameNode (2NN)工作机制
Talk about the message display mechanism on the SAP product UI
pytorch gpu版本安装最新
365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
Centos7 install mysql5.7
sqlalchemy determines whether a field of type array has at least one consistent data with an array
基本语法(二)
深入浅出边缘云 | 4. 生命周期管理
JSP中如何借助response对象实现页面跳转呢?
基于高阶微分器的无模型滑模控制器及其在自动电压调节器中的应用
golang-gin-pprof-使用以及安全问题
IDEA的database使用教程(使用mysql数据库)
基于改进YOLOv5的轻量化航空目标检测方法
清除浮动的四种方式及其原理理解
ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
PHP Serialization: eval
golang中使用泛型
C#控件CheckBox的使用









