I am trying to write some code that will tell me the row number when each column in a "matrix" is equal to or greater than one. I have to use python 2.7 for this code. Previously I solved it using numpy (see explanation below) but the software where I have to implement it does not support numpy or python 3.
I have a list like the following:
data = [0.9338022866253757, 0.9438022866253757, 0.9538022866253757, 0.9638022866253757, 0.9738022866253757, 0.9838022866253757,
0.9526111539604993, 0.9626111539604993, 0.9726111539604994, 0.9826111539604994, 0.9926111539604994, 1.0026111539604994,
0.9638333594112685, 0.9738333594112685, 0.9838333594112684, 0.9938333594112684, 1.0038333594112685, 1.0138333594112683,
0.9718956496941793, 0.9818956496941793, 0.9918956496941793, 1.0018956496941793, 1.0118956496941793, 1.0218956496941793,
0.9782070486192962, 0.9882070486192962, 0.9982070486192962, 1.0082070486192962, 1.0182070486192962, 1.0282070486192962]
This list should be divided into something resembling a matrix, I do this with the following function and split it into lists of length 6 within another list like this:
def split(arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return arrs
data_split = split(data,6)
print(data_split)
Output:
data_split = [[0.9338022866253757, 0.9438022866253757, 0.9538022866253757, 0.9638022866253757, 0.9738022866253757, 0.9838022866253757],
[0.9526111539604993, 0.9626111539604993, 0.9726111539604994, 0.9826111539604994, 0.9926111539604994, 1.0026111539604994],
[0.9638333594112685, 0.9738333594112685, 0.9838333594112684, 0.9938333594112684, 1.0038333594112685, 1.0138333594112683],
[0.9718956496941793, 0.9818956496941793, 0.9918956496941793, 1.0018956496941793, 1.0118956496941793, 1.0218956496941793],
[0.9782070486192962, 0.9882070486192962, 0.9982070486192962, 1.0082070486192962, 1.0182070486192962, 1.0282070486192962]]
This is where I am stuck, the data_split is obviously not a real matrix and I cant figure out how to do the operations I want such that I can find the the first entry in each column where the number reaches 1 or greater than 1.
The result of this should be:
[0 0 0 3 2 1]
Explanation of the output:
Column 0, 1 and 2 never reaches the number 1 so the result is 0. Column 3 reaches 1 at row number 3, column 4 reaches 1 at the row number 2 and last column reaches 1 at row number 1.
In Python 3
Previously I used numpy to solve this problem but the program which I'm writing the code for does not support it. The working Python 3 code with numpy is as follows:
import numpy as np
data = np.array([0.93380229, 0.94380229, 0.95380229, 0.96380229, 0.97380229, 0.98380229,
0.95261115, 0.96261115, 0.97261115, 0.98261115, 0.99261115, 1.00261115,
0.96383336, 0.97383336, 0.98383336, 0.99383336, 1.00383336, 1.01383336,
0.97189565, 0.98189565, 0.99189565, 1.00189565, 1.01189565, 1.02189565,
0.97820705, 0.98820705, 0.99820705, 1.00820705, 1.01820705, 1.02820705])
# data is rewritten into matrix vector form using split and stack
data_split = np.split(data,5)
data_stack = np.stack(data_split)
print(data_stack)
Output:
data_stack = [[0.93380229 0.94380229 0.95380229 0.96380229 0.97380229 0.98380229]
[0.95261115 0.96261115 0.97261115 0.98261115 0.99261115 1.00261115]
[0.96383336 0.97383336 0.98383336 0.99383336 1.00383336 1.01383336]
[0.97189565 0.98189565 0.99189565 1.00189565 1.01189565 1.02189565]
[0.97820705 0.98820705 0.99820705 1.00820705 1.01820705 1.02820705]]
Then I turn the matrix into a true false matrix:
over1 = data_stack >= 1
Output:
over1 = [[False False False False False False]
[False False False False False True]
[False False False False True True]
[False False False True True True]
[False False False True True True]]
Finally I find first occurence of true in each column:
# row number where 1 was reached
index = np.argmax(over1, axis=0)
print(index)
Output:
index = [0 0 0 3 2 1]
You can try this:
data = [0.9338022866253757, 0.9438022866253757, 0.9538022866253757, 0.9638022866253757, 0.9738022866253757, 0.9838022866253757,
0.9526111539604993, 0.9626111539604993, 0.9726111539604994, 0.9826111539604994, 0.9926111539604994, 1.0026111539604994,
0.9638333594112685, 0.9738333594112685, 0.9838333594112684, 0.9938333594112684, 1.0038333594112685, 1.0138333594112683,
0.9718956496941793, 0.9818956496941793, 0.9918956496941793, 1.0018956496941793, 1.0118956496941793, 1.0218956496941793,
0.9782070486192962, 0.9882070486192962, 0.9982070486192962, 1.0082070486192962, 1.0182070486192962, 1.0282070486192962]
cols = 6
lst = [[]]*cols
for i, e in enumerate(data):
n = i%6
lst[n] = lst[n] + [e]
def get_first_above_one(l):
for i,e in enumerate(l):
if e > 1:
return i
return 0
[get_first_above_one(l) for l in lst]
#Out[61]: [0, 0, 0, 3, 2, 1]
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.