Arrays 比较两个numpy 2D数组的相似性

Arrays 比较两个numpy 2D数组的相似性,arrays,numpy,python-2.x,Arrays,Numpy,Python 2.x,我有2D numpyarray1,它只包含0和255值 ([[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]]) ([[255, 0, 255, 0, 255], [ 0, 255, 0, 0, 0], [255,

我有2D numpy
array1
,它只包含
0
255

 ([[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]])
 ([[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]])
以及一个
array2
,其大小和形状与
array1
相同,并且只包含
0
255

 ([[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]])
 ([[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]])

如何将
array1
array2
进行比较以确定相似性百分比?

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

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))

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

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

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))

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

到目前为止您尝试了什么?那么你如何定义数组的相似性呢?你可以将数组彼此相减,然后在得到的数组平方上求和。到目前为止你做了哪些尝试?那么如何定义数组的相似性呢?您可以将数组彼此相减,然后将得到的数组平方相加:main:1:DeprecationWarning:elementwise comparison failed;这将在将来引发一个错误。@skrhee,我有numpy
1.19.2
,这似乎是最新的版本,但我没有收到任何错误。如果不推荐使用elementwise比较,我会感到惊讶。我想当我的数组大小不同时,会出现错误。当我重写代码以循环元素时,它似乎工作得很好。谢谢你的帮助和帖子!您是否曾经得到:main:1:DeprecationWarning:elementwise比较失败;这将在将来引发一个错误。@skrhee,我有numpy
1.19.2
,这似乎是最新的版本,但我没有收到任何错误。如果不推荐使用elementwise比较,我会感到惊讶。我想当我的数组大小不同时,会出现错误。当我重写代码以循环元素时,它似乎工作得很好。谢谢你的帮助和帖子!