[英]Python: loop over a matrix to check for all values in a vector
I have this vector: possibleGrades=np.array([-3,0,2,4,7,10,12])
我有这个向量: possibleGrades=np.array([-3,0,2,4,7,10,12])
And I want the computer to tell me where in this matrix there are values which are not from the vector:我希望计算机告诉我这个矩阵中的哪些值不是来自向量:
[[ 7. 7. 4. ]
[12. 10. 10. ]
[-3. 7. 2. ]
[10. 8. 12. ]
[ nan 7. nan]
[ 7. 7. 10. ]
[ 4.5 nan 2. ]
[ 2. 12. 4. ]]
My idea:我的想法:
for i in range(matrixGr):
if (-3) in matrixGr:
pass
elif 0 in matrixGr:
pass
elif 2 in matrixGr:
pass
elif 4 in matrixGr:
pass
elif 7 in matrixGr:
pass
elif 10 in matrixGr:
pass
elif 12 in matrixGr:
pass
else:
print("The data set contains incorrect grades at {location?}!")
But this is not possible and how to do it is beyond what my mental capacity can conduct.但这是不可能的,如何做到这一点超出了我的心理承受能力。
What is a smart and possible way?什么是聪明可行的方法?
It would be very nice if it was possible to say like "row X contains invalid grade 8 (eg)", so something with string " row {:s} has the invalid grade {:s}" and.format("something smart")如果可以说“行 X 包含无效的 8 级(例如)”,那就太好了,所以带有字符串“行 {:s} 的东西有无效的等级 {:s}”和.format("something smart ")
Can anyone help with this?有人能帮忙吗?
You could try this:你可以试试这个:
import numpy as np
possibleGrades = np.array([-3, 0, 2, 4, 7, 10, 12])
matrixGr = np.array([[7, 7, 4],
[12, 10, 10],
[-3, 7, 2],
[10, 8, 12],
[np.nan, 7, np.nan],
[7, 7, 10],
[4.5, np.nan, 2],
[2, 12, 4]])
locations = [(i, j) for i in range(matrixGr.shape[0]) for j in range(matrixGr.shape[1]) if matrixGr[i, j] not in possibleGrades]
locations:
[(3, 1), (4, 0), (4, 2), (6, 0), (6, 1)]
Good luck !祝你好运 !
Maybe you can try this:也许你可以试试这个:
possibleGrades = np.array([-3,0,2,4,7,10,12])
matrix = np.array([
[ 7. , 7., 4. ],
[12., 10. ,10. ],
[-3., 7. , 2. ],
[10., 8. ,12. ],
[ np.nan, 7. , np.nan],
[ 7. , 7. ,10. ],
[ 4.5, np.nan , 2. ],
[ 2. , 12., 4. ]
])
line, col = np.where(np.isin(matrix, possibleGrades, invert=True))
print('locations:')
[print(f'Invalid value {matrix[line[i],col[i]]} at location ({line[i]},{col[i]})') for i in range(line.size)]
You should get the result:你应该得到结果:
locations:
Invalid value 8.0 at location (3,1)
Invalid value nan at location (4,0)
Invalid value nan at location (4,2)
Invalid value 4.5 at location (6,0)
Invalid value nan at location (6,1)
Here's a simple oneliner that returns the indices that are not in your list of "goodgrades" using list comprehension and the enumerate() function:这是一个简单的 oneliner,它使用列表理解和 enumerate() function 返回不在“goodgrades”列表中的索引:
goodgrades = [-3,0,2,4,7,10,12]
testgrades = [10,-3,11]
badgrades = [ind for ind, i in enumerate(testgrades) if i not in goodgrades]
testgrades = [7,10,7] returns nothing, while [7,-1,7] returns [1]. testgrades = [7,10,7] 不返回任何内容,而 [7,-1,7] 返回 [1]。
To expand this to a list of grade lists, you can use this as a function:要将其扩展为成绩列表列表,您可以将其用作 function:
def checkgrades(grades):
return [ind for ind, i in enumerate(grades) if i not in goodgrades]
now, you can use it in a loop and print test results row by row:现在,您可以循环使用它并逐行打印测试结果:
for row in range(len(grades)):
badgrades = checkgrades(grades[row])
if badgrades != []:
print('Bad grades found in row {} at indices {}'.format(row, badgrades))
Here is a one-liner that returns a boolean matrix the same size as matrixGr
, with True if the grade is in possibleGrades
and False otherwise:这是一个单行代码,它返回与 matrixGr 大小相同的matrixGr
矩阵,如果成绩在possibleGrades
中,则返回 True,否则返回 False:
>>> np.any(np.stack([grade==matrixGr for grade in possibleGrades]),axis=0)
array([[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, False, True],
[False, True, False],
[ True, True, True],
[False, False, True],
[ True, True, True]])
If you want the opposite, just add np.logical_not()
around it:如果你想要相反,只需在它周围添加np.logical_not()
:
>>> np.logical_not(np.any(np.stack([e==mat for e in possibleGrades]),axis=0))
array([[False, False, False],
[False, False, False],
[False, False, False],
[False, True, False],
[ True, False, True],
[False, False, False],
[ True, True, False],
[False, False, False]])
In brief, what it does is that it builds a boolean matrix for each grade in possibleGrades
, then aggregates all the result.简而言之,它的作用是为possibleGrades
中的每个等级构建一个 boolean 矩阵,然后聚合所有结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.