二维数组的相似性

二维数组的相似性

本文介绍了比较两个 numpy 二维数组的相似性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2D numpy array1 只包含 0255

I have 2D numpy array1 that contains only 0 and 255 values

 ([[255,   0, 255,   0,   0],
   [  0, 255,   0,   0,   0],
   [  0,   0, 255,   0, 255],
   [  0, 255, 255, 255, 255],
   [255,   0, 255,   0, 255]])

和一个 array2,它的大小和形状与 array1 相同,并且也只包含 0255

and an array2 that is identical in size and shape as array1 and also contains only 0 and 255 values

 ([[255,   0, 255,   0, 255],
   [  0, 255,   0,   0,   0],
   [255,   0,   0,   0, 255],
   [  0,   0, 255, 255, 255],
   [255,   0, 255,   0,   0]])

如何比较 array1array2 以确定相似度百分比?

How can I compare array1 to array2 to determine a similarity percentage?

推荐答案

由于您只有两个可能的值,我建议使用此算法进行相似性检查:

As you only have two possible values, I would propose this algorithm for similarity-checking:

import numpy as np
A = np.array([[255,   0, 255,   0,   0],
   [  0, 255,   0,   0,   0],
   [  0,   0, 255,   0, 255],
   [  0, 255, 255, 255, 255],
   [255,   0, 255,   0, 255]])

B = np.array([[255,   0, 255,   0, 255],
   [  0, 255,   0,   0,   0],
   [255,   0,   0,   0, 255],
   [  0,   0, 255, 255, 255],
   [255,   0, 255,   0,   0]])

number_of_equal_elements = np.sum(A==B)
total_elements = np.multiply(*A.shape)
percentage = number_of_equal_elements/total_elements

print('total number of elements: \t\t{}'.format(total_elements))
print('number of identical elements: \t\t{}'.format(number_of_equal_elements))
print('number of different elements: \t\t{}'.format(total_elements-number_of_equal_elements))
print('percentage of identical elements: \t{:.2f}%'.format(percentage*100))

计算相等元素并计算相等元素占元素总数的百分比

It counts equal elements and calculates the percentage of the equal elements to the total number of elements

这篇关于比较两个 numpy 二维数组的相似性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:13