Python 根据在其他三维数组上运行的函数为数组指定新值
我有一个多波段光栅,我想把一个函数应用到每个像素在所有波段上的值上。根据结果,将指定一个新值,并根据这些新值生成一个新的单波段光栅。例如,如果某个像素的值在各个波段之间不断增加,则值“1”将指定给结果光栅中的该像素。我正在使用numpy对三维数组进行一些测试,但我无法解析最后一部分,其中分配了新值 应用于三维数组的函数是趋势(列表)。我一开始就定义了它。为了更容易地遍历z(或0)轴上的数组值,我使用了np.swapaxes(谢谢@mummercor)。现在,当将新值分配给新的_band[i,j]数组时,问题就出现了,以便列表上的趋势(列表)结果:Python 根据在其他三维数组上运行的函数为数组指定新值,python,arrays,numpy,Python,Arrays,Numpy,我有一个多波段光栅,我想把一个函数应用到每个像素在所有波段上的值上。根据结果,将指定一个新值,并根据这些新值生成一个新的单波段光栅。例如,如果某个像素的值在各个波段之间不断增加,则值“1”将指定给结果光栅中的该像素。我正在使用numpy对三维数组进行一些测试,但我无法解析最后一部分,其中分配了新值 应用于三维数组的函数是趋势(列表)。我一开始就定义了它。为了更容易地遍历z(或0)轴上的数组值,我使用了np.swapaxes(谢谢@mummercor)。现在,当将新值分配给新的_band[i,j]
[myArraySw[0,0]] will be assigned to new_band[0,0]
[myArraySw[0,1]] will be assigned to new_band[0,1]
[myArraySw[0,2]] will be assigned to new_band[0,2]
[myArraySw[0,3]] will be assigned to new_band[0,3]
................................................
[myArraySw[3,3]] will be assigned to new_band[3,3]
有些值已指定,但有些值未指定。例如,新的_波段[0,1]应为“2”,但为“0”。与新的_波段[3,0]、新的_波段[3,1]、新的_波段[3,2]、新的_波段[3,3]相同,它们应该是“5”,但它们是“0”。其他值看起来不错。问题出在哪里
多谢各位
代码如下:
import os
import numpy as np
def decrease(A):
return all(A[i] >= A[i+1]for i in range(len(A)-1))
def increase(A):
return all(A[i] <= A[i+1] for i in range(len(A)-1))
def Trend(List):
if all(List[i] == List[i+1] for i in range(len(List)-1))==1:
return "Trend: Stable"
else:
a=[value for value in List if value !=0]
MaxList = a.index(max(a)) #max of the list
MinList=a.index(min(a)) #min of the list
SliceInc=a[:MaxList] #slice until max
SliceDec=a[MaxList:] #slice after max value
SliceDec2=a[:MinList] #slice until min value
SliceInc2=a[MinList:] #slice after min value
if all(a[i] <= a[i+1] for i in range(len(a)-1))==1:
return "Trend: increasing"
elif all(a[i] >= a[i+1] for i in range(len(a)-1))==1:
print "Trend: decreasing"
elif increase(SliceInc)==1 and decrease(SliceDec)==1:
return "Trend: Increasing and then Decreasing"
elif decrease(SliceDec2)==1 and increase(SliceInc2)==1:
return "Trend: Decreasing and then Increasing"
else:
return "Trend: mixed"
myArray = np.zeros((4,4,4)) # generating an example array to try the above functions on
myArray[1,0,0] = 2
myArray[3,0,0] = 4
myArray[1,0,1] = 10
myArray[3,0,1] = 8
myArray[0,1,2] = 5
myArray[1,1,2] = 7
myArray[2,1,2] = 4
print "\n"
print "This is the original: "
print "\n"
print myArray
print "\n"
print "\n"
myArraySw = np.swapaxes(np.swapaxes(myArray,0,2),0,1) # swaping axes so that I can iterate through the lists
print "\n"
print "This is the swapped: "
print "\n"
print myArraySw
print "\n"
new_band = np.zeros_like(myArray[0]) # create a new array to store the results of the functions
for j in range(3):
for i in range(3):
if Trend(myArraySw[i,j]) == "Trend: increasing":
new_band[i,j] = 1
elif Trend(myArraySw[i,j]) == "Trend: decreasing":
new_band[i,j] = 2
elif Trend(myArraySw[i,j]) == "Trend: Increasing and then Decreasing":
new_band[i,j] = 3
elif Trend(myArraySw[i,j]) == "Trend: Decreasing and then Increasing":
new_band[i,j] = 4
elif Trend(myArraySw[i,j]) == "Trend: Stable":
new_band[i,j] = 5
elif Trend(myArraySw[i,j]) == "Trend: mixed":
new_band[i,j] = 6
print "\n"
print "The new array is: "
print "\n"
print new_band
导入操作系统
将numpy作为np导入
def减少(A):
返回范围(len(A)-1)内i的全部(A[i]>=A[i+1]
def增加(A):
返回全部(A[i]至少部分问题在于键入时:
elif all(a[i] >= a[i+1] for i in range(len(a)-1))==1:
print "Trend: decreasing"
您可能想键入以下内容:
elif all(a[i] >= a[i+1] for i in range(len(a)-1))==1:
return "Trend: decreasing"
^^^^^^
另外,如果你不介意一些不请自来的建议,你发布的代码有一种非常强烈的“代码气味”-你正在以不必要的复杂方式做很多事情。无论如何,这对你来说都是好事,但我认为如果你通过一些python教程习题集,阅读给定的解决方案,看看更有经验的程序员如何处理常见任务,你会发现这类事情会更容易。你会发现更容易实现的方法t本项目和未来项目的许多要素。完全同意您的观点,感谢您指出“打印”的错误陈述。虽然我不是程序员,但我更喜欢尽可能多地编写代码(即使它不是最有效的代码),而不是要求别人为我编写……)。