Python:在矩阵上循环以检查向量中的所有值
我有一个向量:Python:在矩阵上循环以检查向量中的所有值,python,arrays,pandas,numpy,vector,Python,Arrays,Pandas,Numpy,Vector,我有一个向量:possibleGrades=np.array([-3,0,2,4,7,10,12]) 我想让计算机告诉我,在这个矩阵中,哪些值不是来自向量: [[ 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. ]] 我的想法是: for i in range
possibleGrades=np.array([-3,0,2,4,7,10,12])
我想让计算机告诉我,在这个矩阵中,哪些值不是来自向量:
[[ 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. ]]
我的想法是:
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?}!")
但这是不可能的,如何做到这一点超出了我的心智能力
什么是明智和可行的方法
如果可以说“X行包含无效的等级8(例如)”,那么字符串为“row{:s}的内容具有无效的等级{:s}”和.format(“智能内容”)的话,那就太好了
有人能帮忙吗?这里有一个简单的单行程序,它使用列表理解和枚举()函数返回不在“良好等级”列表中的索引:
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]
def checkgrades(grades):
return [ind for ind, i in enumerate(grades) if i not in goodgrades]
testgrades=[7,10,7]不返回任何内容,而[7,-1,7]返回[1]
要将其展开为等级列表列表,可以将其用作函数:
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]
def checkgrades(grades):
return [ind for ind, i in enumerate(grades) if i not in goodgrades]
现在,您可以在循环中使用它并逐行打印测试结果:
for row in range(len(grades)):
badgrades = checkgrades(grades[row])
if badgrades != []:
print('Bad grades found in row {} at indices {}'.format(row, badgrades))
下面是一个简单的单行程序,它使用列表理解和enumerate()函数返回不在“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]
def checkgrades(grades):
return [ind for ind, i in enumerate(grades) if i not in goodgrades]
testgrades=[7,10,7]不返回任何内容,而[7,-1,7]返回[1]
要将其展开为等级列表列表,可以将其用作函数:
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]
def checkgrades(grades):
return [ind for ind, i in enumerate(grades) if i not in goodgrades]
现在,您可以在循环中使用它并逐行打印测试结果:
for row in range(len(grades)):
badgrades = checkgrades(grades[row])
if badgrades != []:
print('Bad grades found in row {} at indices {}'.format(row, badgrades))
你可以试试这个:
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]
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)]
祝你好运 您可以尝试以下方法:
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]
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)]
祝你好运 也许你可以试试这个:
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]
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)]
您应该得到以下结果:
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)
- :查找向量中的元素,但选项invert=True可用于反转结果
- :查找与True元素对应的行和列
- 也许你可以试试这个:
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]
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)]
您应该得到以下结果:
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)
- :查找向量中的元素,但选项invert=True可用于反转结果
- :查找与True元素对应的行和列
矩阵GR
大小相同的布尔矩阵,如果等级在可能等级中,则返回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]])
如果您想要相反的结果,只需在其周围添加np.logical\u 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]])
简而言之,它所做的是为possibleGrades
中的每个等级构建一个布尔矩阵,然后聚合所有结果。这里有一个一行程序,返回与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]])
如果您想要相反的结果,只需在其周围添加np.logical\u 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]])
简而言之,它的作用是为可能等级中的每个等级构建一个布尔矩阵,然后聚合所有结果。注意:虽然这种方法比Robustus答案更详细,但它适用于每个等级子列表具有不同等级数目的列表。这两种方法都很有价值,取决于您需要什么!注意:虽然这种方法比Robustus答案更详细,但它适用于每个等级子列表具有不同等级数的列表。这两种方法都很有价值,取决于您需要什么!非常感谢。这很有帮助!现在我有一个后续问题:`line,col=np.where(np.isin(matrixGrades,possibleGrades,invert=True))print('displating error or missing grades:\n')[print(f'error missing or missing grades in row{line[I]}行{line[I]}列}!displating error grade:{INSERT VALUE}'),对于范围(line.size)内的I,我该怎么做?所以它显示了不正确的行中的值?我尝试了matrix[I]
,但没有成功。我还想知道,我是否可以进行编辑,使其不以第1行为0开头,但实际上第1行和第1列为1,而不是第0列?我编辑了答案。要从值1开始,您可以将一个添加到行和列数组中,以便(1,1)对应于第一个元素。谢谢!这很有帮助!现在我有一个后续问题:`line,col=np.where(np.isin(matrixGrades,possibleGrades,invert=True))print('displating error or missing grades:\n')[print(f'error missing or missing grades in row{line[I]}行{line[I]}列}!displating error grade:{INSERT VALUE}'),对于范围(line.size)内的I,我该怎么做?所以它显示了不正确的行中的值?我尝试了matrix[I]
,但没有成功。我还想知道,我是否可以进行编辑,使其不以第1行为0开头,但实际上第1行和第1列为1,而不是第0列?我编辑了答案。要从值1开始,可以将一个值添加到行和列数组中,以便(1,1)对应于第一个元素。