2015-11-05 34 views
1

我跑在以下問題:ndarray.any()錯誤:ValueError異常: '軸' 條目是出界的

運行這段代碼

a = np.array([1,2,3]) 
a.any(2) 

給我的錯誤:ValueError: 'axis' entry is out of bounds

看起來方法any()收到一個axis參數太大。當我試圖指定axis說法我收到:

a.any(2, axis=1) 
---> 
TypeError: _any() got multiple values for argument 'axis' 

像有axis參數設置兩次。

我使用Pyzo2014a ver.3.5使用Python 3.4.3和numpy的1.10.1

+0

它真的不清楚你想要做什麼這裏如果有任何元素通過傳遞參數來傳遞參數'true',這裏'any'就會返回'True',在這裏你會混淆'axis'和'out'的參數。你想要'(a == 2).any()'嗎? – EdChum

回答

1

a.any()測試沿給定軸的任何數組元素是否評估爲True。爲了測試是否2a,你可以使用

np.any(a==2) 

(a==2).any() 

或只是

2 in a 
0
a = np.array([1,2,3]) 
a.any(2) 
ValueError: 'axis' entry is out of bounds 

,因爲你只有一次axis這是0你不必與軸你得到這個錯誤值

第二誤差

a.any(2, axis=1) 
---> 
TypeError: _any() got multiple values for argument 'axis' 

因爲軸是所述方法any第一argument所以這裏你」再提供2axis,再次你分配到1參數axis

0

你傳遞一個錯誤的軸any。注意是的any拳頭參數是陣列的一個軸,因爲a是一維陣列可以只通過0,因爲它的軸:

>>> a.any(0) 
True 

numpy.any(a, axis=None, out=None, keepdims=False)[source]

如果您想要一個二維數組,你可以通過01

>>> a = np.array([[1,2,3],[0,0,0]]) 
>>> a.any(0) 
array([ True, True, True], dtype=bool) 
>>> a.any(1) 
array([ True, False], dtype=bool) 
0

我想你想:

(a == 2).any() 

的方法簽名對於any狀態:

a : array_like Input array or object that can be converted to an array.

axis : None or int or tuple of ints, optional Axis or axes along which a logical OR reduction is performed. The default (axis = None) is to perform a logical OR over all the dimensions of the input array. axis may be negative, in which case it counts from the last to the first axis. New in version 1.7.0. If this is a tuple of ints, a reduction is performed on multiple axes, instead of a single axis or all the axes as before.

out : ndarray, optional Alternate output array in which to place the result. It must have the same shape as the expected output and its type is preserved (e.g., if it is of type float, then it will remain so, returning 1.0 for True and 0.0 for False, regardless of the type of a). See doc.ufuncs (Section 「Output arguments」) for details.

keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.

所以你混淆了axis param其中,與你想測試,因此錯誤值:

In [208]: 
a = np.array([1,2,3]) 
(a==2).any() 

Out[208]: 
True