Home > Software engineering >  How can I divide each array by the first value in that array in an array of arrays
How can I divide each array by the first value in that array in an array of arrays

Time:04-05

I have 4 labels listed and a corresponding 4 arrays listed for four different conditions containing relevant data.

I'm trying to write a loop that will go through 4 arrays(the conditions) of 4 arrays(for the 4 labels)and divide each array by the first value of that array, subtract 1, and *100 (each item in that array)

#INPUT - for the 4 labels there is an array under each condition in order, each array of values contains 5 values.
drug_name = ["Astemizole","Nifedipine","Ibutilide","Piperacillin"]
ord_90 =np.array([[0.274874838,0.285399869,0.317728578,0.407954316,0.57446342],[0.267759632,0.251495008,0.217297331,0.197795259,0.189896571],[0.570722939,0.284657491,0.604528529,1,1],[0.406940575,0.330184737,0.249444664,0.195703438,0.18415127]])
ord_30 =np.array([[0.171064425,0.174987058,0.193670662,0.22728254,0.266821461],[0.166787292,0.151327656,0.116268265,0.083153659,0.070527687],[0.322497615,0.153017323,0.267804563,0.705918704,0.683116757],[0.191369803,0.129987371,0.06659752,0.0465941,0.042836445]])
paci_90 =np.array([[0.441088892,0.457035981,0.50722613,0.64209474,0.890497102],[0.411905114,0.331256048,0.220719443,0.160115913,0.146116825],[0.359142009,0.6907817009,1,0.8307300345,0.8588833043],[0.271391074,0.275477015,0.348068661,0.505349944,0.589615152]])
paci_30 =np.array([[0.220987732,0.236639735,0.270462255,0.366486834,0.471698079],[0.20193762,0.14111078,0.066240407,0.043473622,0.037192029],[0.2119885431,0.2723828983,0.2864669099,0.77502029,0.771138501],[0.168170895,0.158282051,0.171664219,0.194167938,0.193573238]])

In essence i need this for each array in each array of arrays giving 4 arrays of 4 arrays containing 5 values.

o9 = 1-(ord_90/[0])*100
o3 = 1-(ord_30/[0])*100
p9 = 1-(paci_90/[0])*100
p3 = 1-(paci_30/[0])*100

I've Tried

for i in range(len(drug_name)):
   o9 = 1-(ord_90/i[0])*100

but get:     

o9 = 1-(ord_90/i[0])*100
TypeError: 'int' object is not subscriptable

#ive also tried writing the below function to calculate this but get a similar error when i use it in the loop.

def per_herg_block(i,base,negative,x):
  return negative - (i/base) *x

for i in range(len(drug_name)):
  o9 = per_herg_block(ord_90, i[0], 1, 100)

CodePudding user response:

This should do I guess:

def per_herg_block(i,base,negative,x):
  return negative - (i/base) *x

for i in range(len(drug_name)):
  o9 = per_herg_block(ord_90, ord_90[i][0], 1, 100)
  print(o9)

CodePudding user response:

If I understood correctly, if you put the labels' name in a list named labels, you can refer to base (which is the first value) using NumPy indexing i.e. labels[i][0]:

labels = [ord_90, ord_30, paci_90, paci_30]


def per_herg_block(label):
    return 1 - (label/label[0]) * 100


result = []
for i in range(len(drug_name)):
    result.append(per_herg_block(labels[i]))

o9, o3, p9, p3 = result

print(o9)
# [[ -99.          -99.          -99.          -99.          -99.        ]
#  [ -96.41147424  -87.12022545  -67.39086757  -47.484659    -32.05633821]
#  [-206.63011382  -98.73988145 -189.26570817 -244.12548606 -173.0754877 ]
#  [-147.04577165 -114.69197216  -77.50872766  -46.97190036  -31.05622214]]

The results in each loop appended to a list named result (4 loops --> each of them correspond to one of the outputs i.e. o9, o3, p9, p3).
Hope it be the answer, if not I believe that your answer can be derived from this written code. If not, please point to problems to modify the code.

  • Related