简体   繁体   中英

How to subtract a list from a list of array?

I have the following variables:

  data = ['10 20 10 36 30 33', 
          '100 50 50 30 60 27 70 24', 
          '300 1000 80 21 90 18 100 15 110 12 120',
          '30 90 130 6 140 3']
  data = [e.split() for e in data]

list of arrays of int64

  time = [np.array((time[2::2]), dtype=int) for time in data]
  concentration = [(np.array((concentration[3::2]), dtype=int)) for concentration in data]

list of float64

  C_mean = [np.mean(np.log(x)) for x in concentration]
  T_mean = [np.mean(x) for x in time]

I want to do the following:

在此处输入图像描述

So calculated this:

   a = ((np.subtract(time, T_mean)),(np.subtract(concentration, C_mean)))/(np.subtract(concentration, C_mean))

I get the following output:

                                       Colum 1
   0 row [  -6.49123184   -7.47653761   -6.7979074    -3.07121711  670.34039133 -265.40965169]
   1 row [1. 1. 1. 1. 1. 1.]


                                        Colum 2
   0 row [-4.66057051   -4.80484032   -2.53370436   13.82370265 -198.83963652]
                                    
   1 row [1. 1. 1. 1. 1.]

                                    column 3
   0 row [  -3.2910719    -3.38052422   -3.42088285   -2.39247195    5.4158155 64.96324446
         -455.85973604]
   1 row [1. 1. 1. 1. 1. 1. 1.]

I do not know why I get the [1. 1. 1. 1. 1. 1. 1. 1.] and i would like them not to appear. Furthermore, I am afraid that I have calculated the a-value wrongly.

Firstly, there is no need to convert data to a list, numpy can do everything that you want it to do on multiple columns. Another note is that you do not take the square in the denumerator (in your provided example) and multiply the numerator.

When you are trying to solve problems, try and start with the simplest case, so we will start with a single data row:

data = '10 20 10 36 30 33'
data = np.array(list(map(int, data.split( ))))

time = data[2::2]
concentration = np.log(data[3::2])

Now we can separately calculate the numerator and denumerator. You can verify these answers by hand to check if the answers are correct.

numerator = np.sum((time - time.mean()) * (concentration - concentration.mean()))
denumerator = np.sum(np.square(time - time.mean()))
a = numerator / denumerator

# Output
-0.004350568849481484

Now you can put this code in a function and run the data over that function. It is much easier to try and debug a single entry than all entries at the same time.


Edit

The above solution was for a single run, to apply the same process on all runs we can use:

data = ['10 20 10 36 30 33', 
        '100 50 50 30 60 27 70 24', 
        '300 1000 80 21 90 18 100 15 110 12 120',
        '30 90 130 6 140 3']

Define a function from the above solution:

def processing(row):
    data = np.array(list(map(int, row.split( ))))

    time = data[2::2]
    concentration = np.log(data[3::2])

    # I had to add this line, since there is a length difference on your 3th input.
    if len(time) != len(concentration):
        return np.NaN

    numerator = np.sum((time - time.mean()) * (concentration - concentration.mean()))
    denumerator = np.sum(np.square(time - time.mean()))
    a = numerator / denumerator
    return a

Run a list comprehension over all data inputs:

result = [processing(row) for row in data]

Output:

[-0.004350568849481484, -0.011157177565710486, nan, -0.06931471805599453]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM