python - numpy: Why is there a difference between (x,1) and (x, ) dimensionality -


i wondering why in numpy there 1 dimensional array of dimension (length, 1) , 1 dimensional array of dimension (length, ) w/o second value.

i running quite frequently, e.g. when using np.concatenate() requires reshape step beforehand (or directly use hstack/vstack).

i can't think of reason why behavior desirable. can explain?

edit:
suggested on of comments question possible duplicate. more interested in underlying workings of numpy , not there distinction between 1d , 2d arrays think point of mentioned thread.

the data of ndarray stored 1d buffer - block of memory. multidimensional nature of array produced shape , strides attributes, , code uses them.

the numpy developers chose allow arbitrary number of dimensions, shape , strides represented tuples of length, including 0 , 1.

in contrast matlab built around fortran programs developed matrix operations. in days in matlab 2d matrix. around 2000 (v3.5) generalized allow more 2d, never less. numpy np.matrix still follows old 2d matlab constraint.

if come matlab world used these 2 dimensions, , distinction between row vector , column vector. in math , physics isn't influenced matlab, vector 1d array. python lists inherently 1d, c arrays. 2d have have lists of lists or arrays of pointers arrays, x[1][2] style of indexing.

look @ shape , strides of array , variants:

in [48]: x=np.arange(10)  in [49]: x.shape out[49]: (10,)  in [50]: x.strides out[50]: (4,)  in [51]: x1=x.reshape(10,1)  in [52]: x1.shape out[52]: (10, 1)  in [53]: x1.strides out[53]: (4, 4)  in [54]: x2=np.concatenate((x1,x1),axis=1)  in [55]: x2.shape out[55]: (10, 2)  in [56]: x2.strides out[56]: (8, 4) 

matlab adds new dimensions @ end. orders values order='f' array, , can readily change (n,1) matrix (n,1,1,1). numpy default order='c', , readily expands array dimension @ start. understanding essential when taking advantage of broadcasting.

thus x1 + x (10,1)+(10,) => (10,1)+(1,10) => (10,10)

because of broadcasting (n,) array more (1,n) 1 (n,1) one. 1d array more row matrix column one.

in [64]: np.matrix(x) out[64]: matrix([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])  in [65]: _.shape out[65]: (1, 10) 

the point concatenate requires matching dimensions. not use broadcasting adjust dimensions. there bunch of stack functions ease constraint, adjusting dimensions before using concatenate. @ code (readable python).

so proficient numpy user needs comfortable generalized shape tuple, including empty () (0d array), (n,) 1d, , up. more advanced stuff understanding strides helps (look example @ strides , shape of transpose).


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -