简体   繁体   English

如何基于值比较python中的两个数组

[英]How to compare two arrays in python based on a value

I am doing classification. 我在做分类。 I have two arrays, the first is 'Actual', and the second is 'Predicted'. 我有两个数组,第一个是'Actual',第二个是'Predicted'。 I want to compare these two arrays. 我想比较这两个数组。 Suppose the first array is: 假设第一个数组是:

Actual = [1, 1, 2, 3, 1]

this tells us that the the first, second, and the last indexes are corresponding to class 1 . 这告诉我们第一个,第二个和最后一个索引对应于类1

The 'Predicted' array is: 'Predicted'数组是:

Predicted = [1, 1, 1, 3, 3]

this tells us that the first and second indexes have been predicted accurately. 这告诉我们第一和第二个索引已经准确预测。

I want the output the tells us just those indexes that accurately predicted as 1 , like this: 我希望输出告诉我们那些准确预测为1的索引,如下所示:

output = [True, True, False, False, False]

Update I want to evaluate just based on value 1 . 更新我想仅根据值1进行评估。 If you see, the forth predicted value is accurately predicted by 3 , but I do not want that, because I want evaluate 1 value. 如果你看到,第四个预测值是由3准确预测的,但我不希望这样,因为我想要评估1值。

Presuming length of two lists are same: 假设两个列表的length相同:

>>> [(x == y == 1) for x, y in zip(Actual, Predicted)]
[True, True, False, False, False]

To feel safe; 感到安全;

>>> from itertools import izip_longest
>>> [(x == y == 1) for x, y in izip_longest(Actual, Predicted, fillvalue=0)]
[True, True, False, False, False]

If you do not mind using numpy library, then this can be done very easily - 如果你不介意使用numpy库,那么这可以很容易地完成 -

In [10]: import numpy as np

In [11]: Actual=[1,1,2,3,1]

In [12]: ActualNp = np.array(Actual)

In [13]: Predicted=[1,1,1,3,3]

In [15]: PredictedNp = np.array(Predicted)

In [20]: (ActualNp == PredictedNp) & (PredictedNp == 1)
Out[20]: array([ True,  True, False, False, False], dtype=bool)

If not, assumming that you only want to check till the length of the smallest list (If they are of different lengths), you can use zip - 如果没有,假设您只想检查直到最小列表的长度(如果它们的长度不同),您可以使用zip -

>>> Actual=[1,1,2,3,1]
>>> Predicted=[1,1,1,3,3]
>>> output = [a == b == 1 for a,b in zip(Actual,Predicted)]
>>> output
[True, True, False, False, False]

A one liner simple approach would be 一个单一的简单方法就是

def get_prediction_results(prediction, actual, predicted):
 return [a == predicted[i] == prediction for i, a in enumerate(actual)]

>>> get_prediction_results(1, [1,1,2], [1,1,2])
[True, True, False]

First, some array basics: 首先,一些数组基础知识:

  1. To get the number of elements in an array, use len : 要获取数组中的元素数,请使用len

      x = ['a', 'b', c'] y = len(x) # y == 3 
  2. To access the i th element of an array, use [] : 要访问数组的第i个元素,请使用[]

      x = ['a','b', 'c'] y = x[1] # y == 'b' 
  3. To obtain an iterator with values 0, 1, ..., n-1 ,use range(n) : 要获取值为0,1,...,n-1的迭代器,请使用range(n)

      x = list(range(3)) # x = [0, 1, 2] 
  4. To iterate through the values of an array, use for ... in : 要遍历数组的值,请使用for ... in

      x = ['a', 'b', 'c'] for value in x: process(value) # called for 'a', 'b', and 'c' 
  5. To compare items for equality, use == (or != for inequality). 要比较项目是否相等,请使用== (或!= for notquality)。

Putting this altogether,now: 完全放下这个,现在:

def ComputeArrayDifference(a, b):
   alen = len(a)
   blen = len(b)
   if alen != blen:
      raise DifferingSizesException('Inputs have different sizes', a, b)
   result = []
   for i in range(alen):
      result.append(a[i] == b[i])
   return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM