python - Why does hstack() copy data but hsplit() create a view on it? -
in numpy, why hstack()
copy data arrays being stacked:
a, b = np.array([1,2]), np.array([3,4]) c = np.hstack((a,b)) a[0]=99
gives c
:
array([1, 2, 3, 4])
whereas hsplit()
creates view on data:
a = np.array(((1,2),(3,4))) b, c = np.hsplit(a,2) a[0][0]=99
gives b
:
array([[99], [ 3]])
i mean - reasoning behind implementation of behaviour (which find inconsistent , hard remember): accept happens because it's coded way...
basically underlying ndarray data structure has single pointer start of data's memory , stride information how move through each dimension. if concatenate 2 arrays, won't know how move 1 memory location other. on other hand, if split array 2 arrays, each can store pointer first element (which somewhere inside original array).
the basic c implementation here, , there discussion at:
http://scipy-lectures.github.io/advanced/advanced_numpy/index.html#life-of-ndarray
Comments
Post a Comment