简体   繁体   English

比较两个numpy数组的行方式ValueError

[英]Compare two numpy arrays row-wise ValueError

I want to compare two NumPy arrays row-wise and return the number of same rows. 我想逐行比较两个NumPy数组并返回相同行的数量。

If i use the code below: 如果我使用以下代码:

a=np.array([[1,2],[3,4]])
b=np.array([[1,4],[2,3]])
comp= np.logical_and(np.equal(a,b))
correct=numpy.sum(comp)

I get the following error: 我收到以下错误:

ValueError: invalid number of arguments

However, this works: 但是,这有效:

np.logical_and([True, False], [False, False])

This is probably very silly but I am new to NumPy . 这可能非常愚蠢,但我是NumPy新手。 Please help. 请帮忙。

I think that you want something akin to: 我想你想要的东西类似于:

np.sum(np.all(np.equal(a, b), axis=1))

which can shorthand to the following if you prefer: 如果您愿意,可以简写以下内容:

np.sum(np.all(a == b, axis=1))

This will return 1 for: 这将返回1

a = np.array([[1, 2], [3, 4]])
b = np.array([[1, 2], [5, 6]])

but 0 for: 0表示:

a = np.array([[1, 2], [3, 4]])
b = np.array([[1, 3], [5, 6]])

Just to extend the answer from @mgilson. 只是为了扩展@mgilson的答案。 You had the right idea, first you did this: 你有正确的想法,首先你做到了这一点:

a = np.array([[1,2],[3,4]])
b = np.array([[1,4],[2,3]])
np.equal(a, b)
>>>array([[ True, False],
   [False, False]], dtype=bool)

Now, you want to pass this to np.logical_and(), which if you look at the docs, it takes in two variables, x1 and x2 ( http://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_and.html ). 现在,您想将此传递给np.logical_and(),如果查看文档,则需要输入两个变量x1和x2( http://docs.scipy.org/doc/numpy/reference/generated/ numpy.logical_and.html )。

So if you pass in the above array, you get the following: 因此,如果您传入上面的数组,则会得到以下内容:

np.logical_and(np.array([[True, False], [False, False]]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid number of arguments

This is because np.array([[True, False], [False, True]]) is a single array, ie, you only gave an x1 value, and did not give an x2 value. 这是因为np.array([[True,False],[False,True]])是一个单独的数组,即,您只给出了一个x1值,并且没有给出x2值。 This is why the traceback tells you 'invalid number of arguments'. 这就是回溯告诉你'参数数量无效'的原因。 You need to give two values to this function. 您需要为此函数提供两个值。

@zero323 rightly gave you one solution, which is to just unpack the values into the function. @ zero323正确地给了你一个解决方案,就是将值解包到函数中。 More specifically, pass the first array value [True, False] into x1, and [False, False] into x2: 更具体地说,将第一个数组值[True,False]传递给x1,将[False,False]传递给x2:

>>> np.logical_and(*np.equal(a, b))
array([False, False], dtype=bool)

What about something like this: 这样的事情怎么样:

import numpy as np

a = np.array([['a', 'b'], ['c', 'd'],\
                  ['e', 't'], ['a', 'b'], ['a', 'b']])
[['a' 'b']
 ['c' 'd']
 ['e' 't']
 ['a' 'b']
 ['a' 'b']]

b = np.array([['a','b'],['e','t'],['r','t']])
[['a' 'b']
 ['e' 't']
 ['r' 't']]

shared_rows=0

for row in b:
    temp=a==row
    shared_rows+=sum(np.sum(temp, axis=1)==a.shape[1])

print(shared_rows)
4

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

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