mercredi 26 août 2020

I don't know how to make a conditional statement using 'while' , 'for' , 'if' etc

I'm an introductory member of Python. Currently, I feel difficulty for trying to code with Python in the conditional statement.

I am trying to extract a number of data values that satisfy special conditions.

Let me explain it to you.

'funcs' and 'coeff' defined numpy array.

In[192]: funcs
Out[192]: 
array([[ 1.06661229e-77,  2.79600427e-81, -3.18670959e-82,
     2.44699438e-81,  4.80121022e-82,  5.17937328e-82],
   [-6.85044722e-80,  3.31723764e-82, -1.90092928e-82,
     6.46861276e-82, -5.78786178e-82, -1.00729181e-82],
   [-1.63162271e-66,  3.38187298e-66,  7.57423529e-66,
    -4.09894792e-65,  3.78265103e-65,  1.82943877e-67],
   [-4.95693797e-09, -1.45229642e-08, -1.04330464e-08,
    -2.01224903e-09, -5.35270495e-09, -6.08458972e-09],
   [-2.12823433e-79,  5.82159029e-80, -8.99421065e-80,
    -1.41016928e-79, -1.67255631e-79, -1.86441120e-79],
   [ 3.15461229e-79, -2.36439314e-79, -4.90090515e-80,
     1.09641210e-80,  4.78412802e-80,  8.10728566e-80],
   [-7.73169440e-09, -6.63806299e-09, -3.42737105e-09,
    -1.10738572e-08, -2.55383682e-08, -1.41581744e-08],
   [ 3.22951472e-02, -6.99601691e-02,  2.53791702e-01,
     1.16700285e-01, -6.95036777e-01,  3.20910932e-01],
   [ 1.05815854e-08, -3.05259173e-09, -9.75855424e-09,
    -9.37246653e-09,  8.68981664e-10, -6.30852393e-09]])

In[193]: coeff
Out[193]: 
array([[ 2.26303373e-83, -2.03621368e-82, -6.42965381e-82,
     4.25254729e-81, -6.07915460e-81,  2.64394236e-81],
   [-8.46364599e-85, -1.16391626e-82,  1.66851234e-81,
    -5.41903835e-81,  6.46938773e-81, -2.59600686e-81],
   [ 5.69414622e-68, -3.52383975e-66,  3.28779598e-65,
    -9.51823310e-65,  1.08072939e-64, -4.21845745e-65],
   [ 1.23232073e-04,  4.05304829e-02, -1.17887992e-01,
     2.34849268e-01, -2.21198557e-01,  7.14445930e-02],
   [-7.72065565e-85,  1.50181866e-82, -1.70427344e-81,
     5.19665852e-81, -6.00699921e-81,  2.35776178e-81],
   [ 2.34175985e-84, -2.19865506e-82,  2.29092448e-81,
    -6.85846672e-81,  7.89183700e-81, -3.09769363e-81],
   [ 1.93436945e-04, -1.13538750e-02,  8.67116554e-02,
    -1.33231032e-01,  3.51553202e-02,  2.07234342e-02],
   [-1.55588574e-04,  8.31273435e-03, -7.60387134e-02,
     2.70458606e-01, -3.75730885e-01,  1.72915967e-01],
   [ 1.93436968e-04, -1.13538762e-02,  8.67116650e-02,
    -1.33231056e-01,  3.51553445e-02,  2.07234255e-02]])

Here i introduce the special condition that 'funcs' are smaller than '10^-9' and the 'coeff' is greater than '10^-4' as shown in the code below.

funcs_valid = np.all(np.abs(funcs) < 10**-9, axis=1)

coeff_valid = np.all(np.abs(coeff) > 1e-4, axis=1)

both_valid = coeff_valid & funcs_valid

However, My goal must be at least one true element of 'both_valid' array.

In [159]: both_valid
Out[159]: array([False, False, False, False, False, False, False, False, False])

The results of the above coding are as follows. So I changed criterion value of 'funcs' to 10^-7 instead of 10^-9. The results are as follows.

In [199]: both_valid
Out[199]: array([False, False, False,  True, False, False,  True, False,  True])

And my final goal is to get a 'coeff' when I meet this condition. The results are as follows.

In[203]: coeff[both_valid]
Out[203]: 
array([[ 1.23232073e-04,  4.05304829e-02, -1.17887992e-01,
     2.34849268e-01, -2.21198557e-01,  7.14445930e-02],
   [ 1.93436945e-04, -1.13538750e-02,  8.67116554e-02,
    -1.33231032e-01,  3.51553202e-02,  2.07234342e-02],
   [ 1.93436968e-04, -1.13538762e-02,  8.67116650e-02,
    -1.33231056e-01,  3.51553445e-02,  2.07234255e-02]])

Finally, What i want is to get a 'coeff' array when at least one of the 'both_valid' array elements satisfy 'true' when i start the change the criterion value of 'funcs' from 10^-10 to 10^-1.

I would appreciate it if Python experts could help me with coding that meets the above conditions.

Aucun commentaire:

Enregistrer un commentaire