2016-01-20 7 views
7

का उपयोग करते समय np.delete का उपयोग करते समय इंडेक्स त्रुटि उत्पन्न होती है जब आउट-ऑफ-बाउंड इंडेक्स का उपयोग किया जाता है। जब एक आउट-ऑफ-बाउंड इंडेक्स एक np.array में उपयोग किया जाता है और सरणी को np.delete में तर्क के रूप में प्रयोग किया जाता है, तो यह क्यों नहीं करता है एक अनुक्रमणिका त्रुटि?क्यों python numpy.delete इंडेक्स नहीं बढ़ाता है जब आउट-ऑफ-बाउंड इंडेक्स एनपी सरणी

np.delete(np.array([0, 2, 4, 5, 6, 7, 8, 9]), 9) 
इस

, एक सूचकांक-त्रुटि देता है एकदम सही ढंग से (सूचकांक 9 सीमा से बाहर है)

जबकि

np.delete(np.arange(0,5), np.array([9])) 

और

np.delete(np.arange(0,5), (9,)) 

दे:

array([0, 1, 2, 3, 4]) 

उत्तर

6

यह एक ज्ञात "फीचर" है और बाद के संस्करणों में इसे हटा दिया जाएगा।

From the source of numpy:

# Test if there are out of bound indices, this is deprecated 
inside_bounds = (obj < N) & (obj >= -N) 
if not inside_bounds.all(): 
    # 2013-09-24, 1.9 
    warnings.warn(
     "in the future out of bounds indices will raise an error " 
     "instead of being ignored by `numpy.delete`.", 
     DeprecationWarning) 
    obj = obj[inside_bounds] 

अजगर में DeprecationWarning को सक्षम करने से वास्तव में इस चेतावनी को दर्शाता है। Ref

In [1]: import warnings 

In [2]: warnings.simplefilter('always', DeprecationWarning) 

In [3]: warnings.warn('test', DeprecationWarning) 
C:\Users\u31492\AppData\Local\Continuum\Anaconda\Scripts\ipython-script.py:1: De 
precationWarning: test 
    if __name__ == '__main__': 

In [4]: import numpy as np 

In [5]: np.delete(np.arange(0,5), np.array([9])) 
C:\Users\u31492\AppData\Local\Continuum\Anaconda\lib\site-packages\numpy\lib\fun 
ction_base.py:3869: DeprecationWarning: in the future out of bounds indices will 
raise an error instead of being ignored by `numpy.delete`. 
    DeprecationWarning) 
Out[5]: array([0, 1, 2, 3, 4]) 
संबंधित मुद्दे