3

I have numpy array like this, where I have one column and one row of ZEROS :

 ([[0. , 2.8, 3.5, 0. , 2.5, 1. , 0.8],
   [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
   [3.5, 2.5, 0. , 0. , 2.8, 1.3, 1.1],
   [3.6, 3.8, 3.3, 0. , 2.5, 0.6, 0.4],
   [2.5, 1.5, 2.8, 0. , 0. , 3.1, 1.9],
   [1. , 0.8, 1.3, 0. , 3.1, 0. , 2.8],
   [0.8, 1.6, 1.1, 0. , 1.9, 2.8, 0. ]])

I want to shift the zero-row to the top and the zero-column either to the left or remove it :

 ([[0. , 0. , 0. , 0. , 0. , 0. ]
   [0. , 2.8, 3.5, 2.5, 1. , 0.8],
   [3.5, 2.5, 0. , 2.8, 1.3, 1.1],
   [3.6, 3.8, 3.3, 2.5, 0.6, 0.4],
   [2.5, 1.5, 2.8, 0. , 3.1, 1.9],
   [1. , 0.8, 1.3, 3.1, 0. , 2.8],
   [0.8, 1.6, 1.1, 1.9, 2.8, 0. ]])

any quick and easy way to do it ? BTW I know the col&row-number, so i doesnt have to search for it.

2
  • 1
    Do you want the zero row/column shifted to the sides or swapped with whatever is currently on the sides? Commented Jun 2, 2018 at 3:23
  • shifted to the left and the top. Commented Jun 2, 2018 at 4:29

2 Answers 2

2

Delete both the column and row and add back in a row of zeros.

This works for your example:

import numpy as np
a =  np.array([[0. , 2.8, 3.5, 0. , 2.5, 1. , 0.8],
   [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
   [3.5, 2.5, 0. , 0. , 2.8, 1.3, 1.1],
   [3.6, 3.8, 3.3, 0. , 2.5, 0.6, 0.4],
   [2.5, 1.5, 2.8, 0. , 0. , 3.1, 1.9],
   [1. , 0.8, 1.3, 0. , 3.1, 0. , 2.8],
   [0.8, 1.6, 1.1, 0. , 1.9, 2.8, 0. ]])

def remove_column_of_zeros_and_shift_row(a, row, col):
    without_row = np.delete(a, row, axis=0)
    without_row_and_col = np.delete(without_row, col, axis=1)
    z = np.zeros((1, len(without_row_and_col[0])))
    without_col_shifted_row = np.append(z, without_row_and_col, axis=0)
    return without_col_shifted_row

my_result = remove_column_of_zeros_and_shift_row(a, 1, 3)
Sign up to request clarification or add additional context in comments.

Comments

2

Use advanced indexing together with np.ix_:

>>> import numpy as np
>>> 
>>> X = np.array( ([[0. , 2.8, 3.5, 0. , 2.5, 1. , 0.8],
...    [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
...    [3.5, 2.5, 0. , 0. , 2.8, 1.3, 1.1],
...    [3.6, 3.8, 3.3, 0. , 2.5, 0.6, 0.4],
...    [2.5, 1.5, 2.8, 0. , 0. , 3.1, 1.9],
...    [1. , 0.8, 1.3, 0. , 3.1, 0. , 2.8],
...    [0.8, 1.6, 1.1, 0. , 1.9, 2.8, 0. ]]))
>>> 
>>> row = 1; col = 3
>>> h, w = X.shape
>>> i = np.r_[row, :row, row+1:h]
>>> j = np.r_[:col, col+1:w]
>>> X[np.ix_(i, j)]
array([[0. , 0. , 0. , 0. , 0. , 0. ],
       [0. , 2.8, 3.5, 2.5, 1. , 0.8],
       [3.5, 2.5, 0. , 2.8, 1.3, 1.1],
       [3.6, 3.8, 3.3, 2.5, 0.6, 0.4],
       [2.5, 1.5, 2.8, 0. , 3.1, 1.9],
       [1. , 0.8, 1.3, 3.1, 0. , 2.8],
       [0.8, 1.6, 1.1, 1.9, 2.8, 0. ]])

2 Comments

np.ix_ docs say "This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions." Can you clarify that at all? This answer is probably more clever but I'd need some comments to follow it if I ran into it in the wild.
@Zev what this does is setting the two 1D arrays up for broadcasting. Think of it as a more economical version of meshgrid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.