np_X 是一个 (4,1) 数组:
In [114]: np_X
Out[114]:
array([[0],
[0],
[0],
[0]])
这是产生错误的代码。您应该已经显示了整个回溯。它可以帮助我们以及您确定问题所在。
In [115]: np_X.all(np_X)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-115-a3b876cb154f> in <module>
----> 1 np_X.all(np_X)
/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py in _all(a, axis, dtype, out, keepdims)
46
47 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
---> 48 return umr_all(a, axis, dtype, out, keepdims)
49
50 def _count_reduce_items(arr, axis):
TypeError: only integer scalar arrays can be converted to a scalar index
查看numpyall 的文档。它需要一个axis 参数,而不是一个数组!
有效的用途是:
In [116]: np_X.all(0)
Out[116]: array([False])
In [117]: np_X.all(1)
Out[117]: array([False, False, False, False])
你想做什么?比较p 和np_X?
In [119]: np_X != p
Out[119]:
array([[ True],
[False],
[ True],
[ True]])
将all 方法应用于该布尔数组:
In [120]: (np_X != p).all()
Out[120]: False
使您的数组 (4,1) 形状、列向量是不必要的复杂化。一个简单的 4 元素数组就足够了:
In [121]: np_X = np.zeros(4, int)
In [122]: np_X
Out[122]: array([0, 0, 0, 0])
p = [
int(input()),
int(input()),
int(input()),
int(input())
]
或者简单地说:
p = [int(input()) for _ in range(4)]