how to make decimal number to integer dynamically

Question:

I have data like below:

a = np.array([[61.22, 92.92, -53.0],
              [-0.272, 0.2828, -0.737],
              [12.43, -0.732, 0.82],
              [52.1, -62.12, 37.78],
              [0.1, 0.67, -0.22],
              [-0.09, -0.28, 0.22],
              [82.1, -11.12, 45.78],
              [-52.1, 0.12, -37.78]])

if index 1-3 in array contains numbers == 0 all i want to change it to 0.

the expectation result such below:

 a = np.array([[61.22, 92.92, -53.0],
              [0.,0., 0.],
              [12.43, -0.732, 0.82],
              [52.1, -62.12, 37.78],
              [0., 0., 0.],
              [0., 0., 0.],
              [82.1, -11.12, 45.78],
              [-52.1, 0.12, -37.78]])

I’ve try use simple code but its doesn’t work:

for i in range(len(a)):
  if a[i].all()==0:
    a[i] = np.round(a[i],0)
Asked By: stack offer

||

Answers:

Effectively you want to check if numbers start with 0, i.e., if their absolute value is smaller than 1. In that case you could use:

for i in range(len(a)):
    if (np.abs(a[i]) < 1).all():
        a[i] = 0
Answered By: Thijs

Here you go:

a = np.array([[61.22,92.92,53.0],
             [-0.272,0.2828,-0.737],
             [12.43,-0.732,0.82],
             [52.1,62.12,37.78],
             [0.1,0.67,-0.22],
             [-.09,-0.28,0.22],
             [82.1,11.12,45.78],
             [52.1,0.12,37.78]])

a[np.all(np.abs(a) < 1, axis=1), :] = 0

# a is now:
#
# array([[61.22 , 92.92 , 53.   ],
#       [ 0.   ,  0.   ,  0.   ],
#       [12.43 , -0.732,  0.82 ],
#       [52.1  , 62.12 , 37.78 ],
#       [ 0.   ,  0.   ,  0.   ],
#       [ 0.   ,  0.   ,  0.   ],
#       [82.1  , 11.12 , 45.78 ],
#       [52.1  ,  0.12 , 37.78 ]])

Your issue was that you were checking if a[i].all()==0: but what you want is absolute value of all numbers in row smaller than 1.

Maybe do something like this:

import numpy as np

a = np.array([[61.22,92.92,53.0],
                 [-0.272,0.2828,-0.737],
                 [12.43,-0.732,0.82],
                 [52.1,62.12,37.78],
                 [0.1,0.67,-0.22],
                 [-.09,-0.28,0.22],
                 [82.1,11.12,45.78],
                 [52.1,0.12,37.78]])

for i in range(len(a)):
   all_zero: bool = True
   
   for j in range(len(a[i])):
        num_str: str = str(a[i][j])
        first_digit: int = int(num_str[1] if num_str[0] == '-' else num_str[0])
        if first_digit == 0:
            all_zero = True
        else:
            all_zero = False
            break
   
   if all_zero:
       for j in range(len(a[i])):
           a[i][j] = 0
    

print(a)

This checks each array, and each element of that. If the first actual digit of the each item in the array is zero, it sets each element of the array to zero.

Answered By: DJ Cook

you have to truncate else -0.737 rounds to 1.0

a[np.all( np.where(np.trunc(a)==0,True,False),axis=1)]=0
Answered By: balu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.