dimanche 6 décembre 2020

Populate values in an array if conditions are true in another array in Python

I have created an array d:

d = [[[1.60269836 1.97347391 1.76414466 1.53102548 1.35352821]
  [1.0153325  1.53331695 1.36105004 1.76111151 1.62595392]
  [1.5156144  1.77076004 1.24249056 1.94406171 1.98917422]]

 [[1.44790465 1.46990159 1.48156613 1.92963951 1.11459211]
  [1.10674091 1.57711027 1.85275685 1.84640848 1.34216641]
  [1.63670185 1.69894884 1.45114395 1.09750849 1.09564564]]]

Which have a max, min and mean:

Max value of d is: 1.98917422158341

Min value of d is: 1.0153325043494292

The mean of d is: 1.5377490722289062

Also I created an empty array f:

f = np.empty((2,3,5))
f = [[[1.60269836 1.97347391 1.76414466 1.53102548 1.35352821]
  [1.0153325  1.53331695 1.36105004 1.76111151 1.62595392]
  [1.5156144  1.77076004 1.24249056 1.94406171 1.98917422]]

 [[1.44790465 1.46990159 1.48156613 1.92963951 1.11459211]
  [1.10674091 1.57711027 1.85275685 1.84640848 1.34216641]
  [1.63670185 1.69894884 1.45114395 1.09750849 1.09564564]]]

With all of this, I need to check for each value in d and:

  • If this d value it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.
  • If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.
  • If a value equals to d_mean, assign 50 to the corresponding value in f.
  • Assign 0 to the corresponding value(s) in f for d_min in d.
  • Assign 100 to the corresponding value(s) in f for d_max in d.

In the end, f should have only the following values: 0, 25, 50, 75, and 100.

Up to now, my best solution has been:

for value in d:
    if value.any() > d_min and value.any() < d_mean:
        value = 25
        value.append(f)

    if value.any() > d_mean and value.any() < d_max:
        value = 75
        value.append(f)

    if value.any() == d_mean:
        value = 50
        value.append(f)

    if value.any() == d_min:
        value = 0
        value.append(f)

    if value.any() == d_max:
        value = 100
        value.append(f)

I don't receive any error but neither the result that I need. I'm just starting to learn python and numpy so I'm sure it will exist a better way to do it but after reading the documentation and look for many examples, I don't get the solution.

Aucun commentaire:

Enregistrer un commentaire