1

Original datas

 [[ 0.00000000e+00  1.00000000e+00 -6.76207728e+00 -1.63236398e+01]
 [ 0.00000000e+00  1.00000000e+00  2.51283367e+01  1.13952157e+02]
 [ 0.00000000e+00  1.00000000e+00  3.11402956e+00 -5.16009612e+02]
 [ 0.00000000e+00  1.00000000e+00  3.10969787e+01  1.82175649e+02]
 [ 1.00000000e+00 -2.31269114e+00 -4.13720127e+02  3.55395844e+03]
 [ 1.00000000e+00  4.54598490e+01  6.19694322e+02  2.61091335e+03]
 [ 1.00000000e+00  7.36925014e-01 -4.49386738e+02 -1.22392549e+03]
 [ 1.00000000e+00  3.29511609e+00 -4.43413555e+02 -4.12677155e+03]]

I tried to remove zeros with this ode below

def removeZeroPadding(X):
  res = []
  for poly in enumerate(X):
      tmp = poly[1]
      tmp = tmp[tmp != 0]
      res.append(tmp)
  return res

This transforms into

[array([  1.        ,  -6.76207728, -16.32363975]), array([  1.        ,  25.1283367 , 113.95215706]), array([   1.        ,    3.11402956, -516.0096117 ]), array([  1.        ,  31.09697873, 182.17564943]), array([ 1.00000000e+00, -2.31269114e+00, -4.13720127e+02,  3.55395844e+03]), array([1.00000000e+00, 4.54598490e+01, 6.19694322e+02, 2.61091335e+03]), array([ 1.00000000e+00,  7.36925014e-01, -4.49386738e+02, -1.22392549e+03]), array([ 1.00000000e+00,  3.29511609e+00, -4.43413555e+02, -4.12677155e+03])]

How can I keep the structures of the original data with no zeros? Thanks

edit: it should look like this

 [[ 1.00000000e+00 -6.76207728e+00 -1.63236398e+01]
 [ 1.00000000e+00  2.51283367e+01  1.13952157e+02]
 [ 1.00000000e+00  3.11402956e+00 -5.16009612e+02]
 [ 1.00000000e+00  3.10969787e+01  1.82175649e+02]
 [ 1.00000000e+00 -2.31269114e+00 -4.13720127e+02  3.55395844e+03]
 [ 1.00000000e+00  4.54598490e+01  6.19694322e+02  2.61091335e+03]
 [ 1.00000000e+00  7.36925014e-01 -4.49386738e+02 -1.22392549e+03]
 [ 1.00000000e+00  3.29511609e+00 -4.43413555e+02 -4.12677155e+03]]
12
  • Numpy provides a trim method Commented Jun 14, 2020 at 14:11
  • what do you want instead of the zeros? Commented Jun 14, 2020 at 14:11
  • try this, arr[arr != 0] Commented Jun 14, 2020 at 14:11
  • Do you want to look for columns consisting of all 0's and remove those? Commented Jun 14, 2020 at 14:11
  • 1
    With “keep the structures”, you mean you want to keep the initial dimensions of your array (i.e. 4 by 6 in your example)? If so, what values should take the place of your zeros? None? Commented Jun 14, 2020 at 14:12

3 Answers 3

1

It seems like you do not want a list of arrays as the output but instead want a 2-dimensional array (i.e. [[...]] instead of [array([...]), array([...])]).

However, this is not possible as the rows of your array end up with different sizes after you trim them by removing the zeros (i.e. some end up having 3 elements and some have 4). If you want it to be a single array, same as for matrices in mathematics, all columns (and all rows) will need to have the same number of elements.

As an alternative, you could assign the elements you want to trim a different value, e.g. None, or you can create an empty array and fill it.

Sign up to request clarification or add additional context in comments.

Comments

1

What you seek is fundamentally impossible. You can't both remove a variable number of entries from each row and at the same time be guaranted that each row has the same number of entries (which is required to get a ndarray of type [[...]]. hlzl's answer explains this in more detail.

This following function will remove 0-entries from the left and right side of each row, but what it returns will be of type [array([...]), array([...])]:

def trim_zeros_in_rows(ndarr):
    return np.array([np.trim_zeros(row) for row in ndarr])

print(trim_zeros_in_rows(np.array([[0., 0., 1., 0.],
                                   [2., 3., 4., 0.]])))

Prints:

[array([1.]) array([2., 3., 4.])]

Comments

0

Have a look at https://numpy.org/doc/stable/reference/generated/numpy.trim_zeros.htmltrim_zeros.

It will avoid the for loop

Comments

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.