我有三个2D数组-- SandArray、ClayArray和SiltArray。我还有一个描述这里的函数。下面是我的代码,当我运行脚本时,我会得到一个ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
def TextureClass(sand, clay, silt):
#if sand + clay > 100 or sand < 0 or clay < 0:
# raise Exception('Inputs adds over 100% or are negative')
if silt + 1.5*clay < 15:
textural_class = 'sand'
elif silt + 1.5*clay >= 15 and silt + 2*clay < 30:
textural_class = 'loamy sand'
elif (clay >= 7 and clay < 20 and sand > 52 and silt + 2*clay >= 30) or (clay < 7 and silt < 50 and silt + 2*clay >= 30):
textural_class = 'sandy loam'
elif clay >= 7 and clay < 27 and silt >= 28 and silt < 50 and sand <= 52:
textural_class = 'loam'
elif (silt >= 50 and clay >= 12 and clay < 27) or (silt >= 50 and silt < 80 and clay < 12):
textural_class = 'silt loam'
elif silt >= 80 and clay < 12:
textural_class = 'silt'
elif clay >= 20 and clay < 35 and silt < 28 and sand > 45:
textural_class = 'sandy clay loam'
elif clay >= 27 and clay < 40 and sand > 20 and sand <= 45:
textural_class = 'clay loam'
elif clay >= 27 and clay < 40 and sand <= 20:
textural_class = 'silty clay loam'
elif clay >= 35 and sand > 45:
textural_class = 'sandy clay'
elif clay >= 40 and silt >= 40:
textural_class = 'silty clay'
elif clay >= 40 and sand <= 45 and silt < 40:
textural_class = 'clay'
else:
textural_class = 'na'
return textural_class
Texture = TextureClass(SandArray,ClayArray,SiltArray)纹理应该是一个与SandArray、ClayArray和SiltArray具有相同形状的数组,但以textural_class str作为其值。
是否可能有一个具有条件的函数的文本输出数组,并使用数组作为它的输入参数,如果有,我遗漏了什么?
编辑:尝试了texture = np.array(list(map(TextureClass,SandArray,ClayArray,SiltArray)))之后,我仍然得到了相同的ValueError
发布于 2022-08-08 16:34:27
我对numpy不太了解,但是,从我在其他问题中搜索的内容来看,您可以使用矢量化来包装您的函数。
下面是一个例子:如何应用2d numpy数组/矩阵中每个元素的函数/映射值?
使用我之前使用的主要是内置python代码的方法:
您可以zip这三个数组(无论是在函数内部还是外部,但我会在外部执行),然后在压缩的数组上循环。
zip()函数返回一个zip对象,它是一个元组的迭代器,其中每个传递的迭代器中的第一个项是对的,然后每个传递的迭代器中的第二个项是配对的,等等。 如果传递的迭代器具有不同的长度,则具有最少项的迭代器将决定新迭代器的长度。
强调的是,假定这三个数组的长度相同。而且,在这种情况下,您需要双向压缩:
因此,与Texture = TextureClass(SandArray,ClayArray,SiltArray)不同,您可以使用:
soilCompositions = (zip(sands, clays, silts) for sands, clays, silts in zip(SandArray, ClayArray, SiltArray))
Textures = ((TextureClass(sand, clay, silt) for sand, clay, silt in soilCompositionRow) for soilCompositionRow in soilCompositions)请注意,我使用了生成器理解,但您也可以轻松地使用列表理解:
soilCompositions = (zip(sands, clays, silts) for sands, clays, silts in zip(SandArray, ClayArray, SiltArray))
Textures = [[TextureClass(sand, clay, silt) for sand, clay, silt in soilCompositionRow] for soilCompositionRow in soilCompositions]发布于 2022-08-08 17:49:37
将函数应用于矩阵的每个元素需要np.vectorize。文档是可用的这里。,类似问题的一个例子可以在这里找到:如何应用2d numpy数组/矩阵中每个元素的函数/映射值?。
我认为这个问题是独一无二的,因为它显示了np.vectorize工作的函数的范围。我最初的问题是,np.vectorize是否会像我问题中的条件函数那样工作。
def TextureClass(sand, clay, silt):
#if sand + clay > 100 or sand < 0 or clay < 0:
# raise Exception('Inputs adds over 100% or are negative')
if silt + 1.5*clay < 15:
textural_class = 'sand'
elif silt + 1.5*clay >= 15 and silt + 2*clay < 30:
textural_class = 'loamy sand'
elif (clay >= 7 and clay < 20 and sand > 52 and silt + 2*clay >= 30) or (clay < 7 and silt < 50 and silt + 2*clay >= 30):
textural_class = 'sandy loam'
elif clay >= 7 and clay < 27 and silt >= 28 and silt < 50 and sand <= 52:
textural_class = 'loam'
elif (silt >= 50 and clay >= 12 and clay < 27) or (silt >= 50 and silt < 80 and clay < 12):
textural_class = 'silt loam'
elif silt >= 80 and clay < 12:
textural_class = 'silt'
elif clay >= 20 and clay < 35 and silt < 28 and sand > 45:
textural_class = 'sandy clay loam'
elif clay >= 27 and clay < 40 and sand > 20 and sand <= 45:
textural_class = 'clay loam'
elif clay >= 27 and clay < 40 and sand <= 20:
textural_class = 'silty clay loam'
elif clay >= 35 and sand > 45:
textural_class = 'sandy clay'
elif clay >= 40 and silt >= 40:
textural_class = 'silty clay'
elif clay >= 40 and sand <= 45 and silt < 40:
textural_class = 'clay'
else:
textural_class = 'na'
return textural_class
vector_func = np.vectorize(TextureClass)
textures = vector_func(SandArray, ClayArray, SiltArray)https://stackoverflow.com/questions/73280852
复制相似问题