我有一个包含元素的数组,我想总结一下准确性。我想对具有相同顺序的元素的数组求和。我宁愿不用zip
编写for循环遍历每个元素并将它们相加,是否有更简单的方法来执行此操作?
这两个数组如下,目前我的代码用于计算总和。
yp = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
y = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
sums = np.sum(yp == y)
我的准确度为零。
答案 0 :(得分:0)
使用您的示例:
yp = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
y = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
# First make the two arrays in question numpy arrays.
yp = np.array(yp)
y = np.array(y)
array_length = y.shape[1] # store length of sub arrays
equal_elements = np.array(yp) == np.array(y) # check all equal elements
sums = np.sum(equal_elements, 1) # sum the number of equal elements in each sub array, use axis 1 as each array/sample is axis 0
equal_arrays = np.where(sums==array_length)[0] # returns a tuple, so index first element immediately
number_equal_arrays = equal_arrays.shape[0] # What elements are equal
print('Number of equal arrays %d' % number_equal_arrays)
print('Accuracy %0.2f' % (number_equal_arrays/yp.shape[0]))
打印
相等数组的数量6
准确度1.00