python - how to exclude elements from numpy matrix -


suppose have matrix:

mat = np.random.randn(5,5)  array([[-1.3979852 , -0.37711369, -1.99509723, -0.6151796 , -0.78780951],        [ 0.12491113,  0.90526669, -0.18217331,  1.1252506 , -0.31782889],        [-3.5933008 , -0.17981343,  0.91469733, -0.59719805,  0.12728085],        [ 0.6906646 ,  0.2316733 , -0.2804641 ,  1.39864598, -0.09113139],        [-0.38012856, -1.7230821 , -0.5779237 ,  0.30610451, -1.30015299]]) 

suppose have index array:

idx = np.array([0,4,3,1,3]) 

while can extract elements matrix using following:

mat[idx, range(len(idx))] array([-1.3979852 , -1.7230821 , -0.2804641 ,  1.1252506 , -0.09113139]) 

what want know how can use index exclude elements matrix, i.e. how obtain following result:

array([[0.12491113 , -0.37711369, -1.99509723, -0.6151796 , -0.78780951],        [-3.5933008 ,  0.90526669, -0.18217331, -0.59719805, -0.31782889],        [0.6906646  , -0.17981343,  0.91469733,  1.39864598,  0.12728085],        [-0.38012856,  0.2316733 , -0.5779237 ,  0.30610451, -1.30015299]]) 

thought simple doing mat[-idx, range(len(idx))] doesn't work. i've tried np.delete() doesn't seem either. solutions out there don't require looping or list comprehensions? appreciate insight. thanks.

edit: data must in same columns post processing.

when 'delete' not work, mean? do? might diagnostic.

lets first @ selection work:

in [484]: mat=np.arange(25).reshape(5,5) # better random  in [485]: mat[idx,range(5)] out[485]: array([ 0, 21, 17,  8, 19]) 

this can used on flattened version of file:

in [486]: mat.flat[idx*5+np.arange(5)] out[486]: array([ 0, 21, 17,  8, 19]) 

now try same default flat delete:

in [487]: np.delete(mat,idx*5+np.arange(5)).reshape(5,4) out[487]:  array([[ 1,  2,  3,  4],        [ 5,  6,  7,  9],        [10, 11, 12, 13],        [14, 15, 16, 18],        [20, 22, 23, 24]]) 

delete isn't inplace operator; returns new matrix. , if specify axis, delete removes whole rows or columns, not selected items.

mat[-idx, range(len(idx))] isn't going work since negative indexes have meaning - count end.

this delete ends doing boolean indexing, thus:

in [498]: mat1=mat.ravel() in [499]: idx1=idx*5+np.arange(5) in [500]: ii=np.ones(mat1.shape, bool) in [501]: ii[idx1]=false in [502]: mat1[ii] out[502]:  array([ 1,  3,  4,  5,  6,  7,  8,  9, 10, 12, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24]) 

this sort of indexing/delete works if delete different number of items each row. of course in case couldn't count on reshaping matrix rectangular matrix.

in general when dealing different indexes different rows, operation ends acting on flat or raveled version of matrix. 'irregular' operations make more sense when dealing 1d arrays 2d.


looking more @ example, see when remove item, move other column values fill gap. in version, moved values along rows. let's try f ordered.

in [523]: mat2=mat.flatten('f') in [524]: np.delete(mat2,idx2).reshape(5,4).t out[524]:  array([[ 5,  1,  2,  3,  4],        [10,  6,  7, 13,  9],        [15, 11, 12, 18, 14],        [20, 16, 22, 23, 24]]) 

where removed value each column:

in [525]: mat2[idx2] out[525]: array([ 0, 21, 17,  8, 19]) 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -