If statement 将if语句与ndarray一起使用
我试图在从文本文件中读取的569x462 ndarray中查找低于200的值。我想使用numpy.where查找值。。。我尝试了以下代码:If statement 将if语句与ndarray一起使用,if-statement,numpy,where,If Statement,Numpy,Where,我试图在从文本文件中读取的569x462 ndarray中查找低于200的值。我想使用numpy.where查找值。。。我尝试了以下代码: import numpy as np #Load in text file from Sebago sebago=np.loadtxt('sebagoWatershedElv.txt',skiprows=6) #Find where elevation is less than 200 lowElv=np.where(sebago[:]<200)
import numpy as np
#Load in text file from Sebago
sebago=np.loadtxt('sebagoWatershedElv.txt',skiprows=6)
#Find where elevation is less than 200
lowElv=np.where(sebago[:]<200)
将numpy导入为np
#从Sebago加载文本文件
sebago=np.loadtxt('sebagoWatershedElv.txt',skiprows=6)
#查找标高小于200的位置
lowElv=np。除1:1之外,不要复制(或查看)数组
import numpy as np
a = np.random.randint(10, size=(8, 8))
# array([[2, 6, 5, 1, 1, 8, 0, 3],
# [4, 7, 5, 4, 9, 6, 1, 8],
# [8, 3, 3, 4, 2, 3, 3, 0],
# [7, 3, 6, 3, 0, 0, 8, 6],
# [5, 7, 7, 0, 7, 4, 8, 6],
# [5, 9, 4, 8, 3, 2, 2, 4],
# [3, 4, 6, 6, 5, 2, 1, 0],
# [3, 7, 6, 4, 4, 4, 1, 3]])
值满足条件的索引
如果将布尔数组(例如,aMinor放在一边),不要复制(或查看)您的数组[:]
,除非您正在执行1:1以外的操作
import numpy as np
a = np.random.randint(10, size=(8, 8))
# array([[2, 6, 5, 1, 1, 8, 0, 3],
# [4, 7, 5, 4, 9, 6, 1, 8],
# [8, 3, 3, 4, 2, 3, 3, 0],
# [7, 3, 6, 3, 0, 0, 8, 6],
# [5, 7, 7, 0, 7, 4, 8, 6],
# [5, 9, 4, 8, 3, 2, 2, 4],
# [3, 4, 6, 6, 5, 2, 1, 0],
# [3, 7, 6, 4, 4, 4, 1, 3]])
值满足条件的索引
如果输入布尔数组(例如a),则会将小于200的所有值作为一维数组:
lowElv = sebago[sebago < 200]
lowElv=sebago[sebago<200]
这将提供所有小于200的值作为一维数组:
lowElv = sebago[sebago < 200]
lowElv=sebago[sebago<200]
你想要值小于200的索引还是值小于200的索引?我实际上想要两者。显示索引…明白了。你想要值小于200的索引还是值小于200的索引?我实际上想要两者。显示索引…明白了。sebago[:]
不执行复制,它只是返回另一个视图到sebago
@ali\m啊,你是对的。但在这种情况下,1:1视图似乎毫无意义。对于最后一个视图,我将使用np.count\u non-zero
。它可读性更强,速度更快(在我的机器上是x5)比使用np.sum
sebago[:]
不执行复制,它只是返回另一个视图到sebago
@ali\m啊,你是对的。但是在这种情况下,1:1视图似乎毫无意义。对于最后一个视图,我将使用np.count\u non zero
来代替。它可读性更强,速度更快(在我的机器上是x5)而不是使用np.sum
。我试图练习使用numpy.where,但我想我不懂语法。我使用了lowerv=np.where(sebagonumpy.where
返回索引。因此,这个sebago[np.where(sebago<200)]
给出了与我的答案相同的结果。我试图练习使用numpy.where,但我想我不理解语法。我使用了lowerv=np.where(sebagonumpy.where
返回索引。因此,这个sebago[np.where(sebago<200)]
给出了与我的答案相同的结果。
lowElv = sebago[sebago < 200]