Home > Mobile >  How do I translate this python code using numpy and without using loops?
How do I translate this python code using numpy and without using loops?

Time:10-02

I'm not sure how to approach this. Would love an explanation to this as well. How do I translate the code shown below using numpy and does not include loops in it?

import random

random.seed(2022)

simulated_data = [random.randint(1, 10) for i in range(20)]
print(simulated_data)

classified_data = []
for value in simulated_data:
  if value % 8 == 0:
    classified_data.append(2)
  elif value % 9 == 0:
    classified_data.append(1)
  elif value % 10 == 0:
    classified_data.append(-1)
  elif value % 11 == 0:
    classified_data.append(-2)
  else:
    classified_data.append(0)

classified_data

CodePudding user response:

One possibility would be to compute arrays of whether the data mod 8, 9, 10, 11 is 0, then you can just sum those values multiplied by the required value for each modulo (using numpy.dot) to get your desired output:

# some non-random data for testing
simulated_data = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

modulos = simulated_data % np.arange(8, 12)[:, None] == 0
weights = np.array([2, 1, -1, -2])
classified_data = np.dot(modulos.transpose(), weights)
classified_data

Output:

array([ 0,  0,  2,  1, -1, -2,  0,  0,  0,  0,  2,  0,  1,  0, -1,  0, -2,
        0,  2])

CodePudding user response:

I believe "map" is the function you need, first you create a function that gives you the output you want based on a given value in your input set

def classify(value):
  if value % 8 == 0:
    return 2
  elif value % 9 == 0:
    return 1
  elif value % 10 == 0:
    return -1
  elif value % 11 == 0:
    return -2
  else:
    return 0

then apply it to your input data with the map function, default return type is a map object, cast it as a list if you want it that way

classified_data = list(map(classify, simulated_data))
  • Related