简体   繁体   English

在python 2.7中查找矩阵中每一列等于或大于1时的行号

[英]Find row number when each column in a matrix is equal to or greater than 1 in python 2.7

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.对于此代码,我必须使用 python 2.7。 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.以前我使用 numpy 解决了它(见下面的解释),但我必须实现它的软件不支持 numpy 或 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:这个列表应该被分成类似于矩阵的东西,我用下面的 function 做这个,并将它分成长度为 6 的另一个列表中的列表,如下所示:

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: 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.这就是我卡住的地方,data_split 显然不是一个真正的矩阵,我无法弄清楚如何进行我想要的操作,以便我可以在每列中找到数字达到 1 或大于 1 的第一个条目。

The result of this should be:结果应该是:

[0 0 0 3 2 1]

Explanation of the output: 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.第 0、1 和 2 列永远不会达到数字 1,因此结果为 0。第 3 列在第 3 行达到 1,第 4 列在第 2 行达到 1,最后一列在第 1 行达到 1。

In Python 3在 Python 3

Previously I used numpy to solve this problem but the program which I'm writing the code for does not support it.以前我使用 numpy 来解决这个问题,但我正在为其编写代码的程序不支持它。 The working Python 3 code with numpy is as follows: numpy 的工作 Python 3 代码如下:

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: 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: 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:最后,我发现每列中第一次出现 true :

# row number where 1 was reached
index = np.argmax(over1, axis=0)
print(index)

Output: 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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python通过大于或等于每个子组中位数的列值找出数据帧中的记录 - Python find out records in dataframe by column values greater than or equal to their median in each subgroup 给定大小为3 x 3的矩阵垫,请找到其最终累积总和大于或等于tO 150的每一行中的所有偶数 - Given a matrix mat of size 3 x 3. Find all the even numbers situated in each of the row(s) whose end cumulative sum is greater than or equal tO 150 在较大的列中找到大于或等于较短的搜索列中的每个值的第一个值 - Find the first value in larger column greater than or equal to each value in shorter search column 在 Python Selenium 中等待某个值大于等于某个数 - Waiting for a value to be greater than or equal to a certain number in Python Selenium 在 Python 中找到大于或等于 n 的最小 2 次方 - Find the smallest power of 2 greater than or equal to n in Python 当列索引大于每行唯一的某个截止值时,屏蔽数组条目 - Mask array entries when column index is greater than a certain cutoff that is unique to each row 使用python打印多个数字格式,使列数大于行数 - print multiple number format with python that make column counts greater than row python 中的“大于”或“等于”与“等于”或“大于” - “Greater than” or “equal” vs “equal” or “greater than” in python 根据多种条件查找行(列值大于) - Find row based on multiple conditions (column values greater than) 如何将Python中第二个矩阵中每列的矩阵中的每一行乘以? - How to multiply each row in a matrix by each column in the second matrix in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM