Comparing NumPy Object Links

I want to understand the behavior of NumPy.

When I try to get a reference to the internal array of the NumPy array and then compare it with the object itself, I get as a return value False.

Here is an example:

In [198]: x = np.array([[1,2,3], [4,5,6]])
In [201]: x0 = x[0]
In [202]: x0 is x[0]
Out[202]: False

On the other hand, with native objects, Python returns True.

In [205]: c = [[1,2,3],[1]]    
In [206]: c0 = c[0]    
In [207]: c0 is c[0]
Out[207]: True

My question is, what is the intended behavior of NumPy? If so, what should I do if I want to create a reference to the internal objects of NumPy arrays.

+4
source share
3 answers

2d slicing

, 1d. OP 2d , x[0] "", .

In [81]: arr = np.array([[1,2,3], [4,5,6]])
In [82]: arr.__array_interface__['data']
Out[82]: (181595128, False)

In [83]: x0 = arr[0,:]
In [84]: x0.__array_interface__['data']
Out[84]: (181595128, False)        # same databuffer pointer
In [85]: id(x0)
Out[85]: 2886887088
In [86]: x1 = arr[0,:]             # another slice, different id
In [87]: x1.__array_interface__['data']
Out[87]: (181595128, False)
In [88]: id(x1)
Out[88]: 2886888888

, , - . , arr[0,0], , 1d-.

2d arr , 1d arr.ravel(); . view, copy item .

2d C . numpy strided shape strides . , shape strides, -.

1d

, , :

In [51]: arr = np.arange(4)

- , . ( C), Python. :

In [52]: np.info(arr)
class:  ndarray
shape:  (4,)
strides:  (4,)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  True
data pointer: 0xa84f8d8
byteorder:  little
byteswap:  False
type: int32

In [53]: arr.__array_interface__
Out[53]: 
{'data': (176486616, False),
 'descr': [('', '<i4')],
 'shape': (4,),
 'strides': None,
 'typestr': '<i4',
 'version': 3}

, . .

, :

In [54]: x1 = arr[1]
In [55]: type(x1)
Out[55]: numpy.int32
In [56]: x1.__array_interface__
Out[56]: 
{'__ref': array(1),
 'data': (181158400, False),
....}
In [57]: id(x1)
Out[57]: 2946170352

, . , . , "data" .

- :

In [58]: x2 = arr[1]
In [59]: id(x2)
Out[59]: 2946170336
In [60]: x2.__array_interface__['data']
Out[60]: (181143288, False)

, , :

In [61]: arr[1] = 10
In [62]: arr
Out[62]: array([ 0, 10,  2,  3])
In [63]: x1
Out[63]: 1

x1 x2 id , , is, arr. , arr.

slicing view ,

In [64]: y = arr[1:2]
In [65]: y.__array_interface__
Out[65]: 
{'data': (176486620, False),
 'descr': [('', '<i4')],
 'shape': (1,),
 ....}
In [66]: y
Out[66]: array([10])
In [67]: y[0]=4
In [68]: arr
Out[68]: array([0, 4, 2, 3])
In [69]: x1
Out[69]: 1

4 , arr - , . y arr ( x1).

0d

In [71]: z = y.reshape(())
In [72]: z
Out[72]: array(4)
In [73]: z[...]=0
In [74]: arr
Out[74]: array([0, 0, 2, 3])

Python . c-api cython, . nditer - , 0d ( Python, c-api). cython typed memoryviews .

http://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html

https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html

https://docs.scipy.org/doc/numpy/reference/c-api.iterator.html#c.NpyIter

elementwise ==

NumPy

np.array([1]) == np.array([2]) ([False], dtype = bool)

== . .

(, if), , np.all np.any.

is ( numpy). . , is None, None - .

+2

, Numpy. , Numpy ( Python) , , .

Numpy, , . , Numpy C python, . Python , , split(), __getitem__, take() .., , -, , python - Numpy.

, :

In [7]: x
Out[7]: 
array([[1, 2, 3],
       [4, 5, 6]])

In [8]: x[0] is x[0]
Out[8]: False

, , , python, , , Numpy.

, @Imanol, Numpy, , (-). view :

a.view(some_dtype) a.view(dtype=some_dtype) . .

a.view(ndarray_subclass) a.view(type=ndarray_subclass) ndarray_subclass, ( , dtype ..). .

a.view(some_dtype), some_dtype , dtype (, ), a ( print(a)). , . , a C- for-tran-, .., .

+1

, , numpy.ndarray.ctypes , : https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.ctypes.html

- ( dtype, meh):

def is_same_array(a, b):
    return (a.shape == b.shape) and (a == b).all() and a.ctypes.data == b.ctypes.data

here: https://github.com/EricCousineau-TRI/repro/blob/a60daf899e9726daf2ca1259bb80ad2c7c9b3e3f/python/namedlist_alt.py#L111

0
source

Source: https://habr.com/ru/post/1676801/


All Articles