2014-11-22 83 views
0

我試圖運行下面的代碼,其中Bwavelength,吞吐量和newflux是列表。努力理解錯誤消息

def ABconversion(Bwavelength, throughput): 
    ABconstant=[] 

    c=3e18 
    i=0 
    for i in range(0, len(Bwavelength)): 
     ABconstant.append(((3e18/((Bwavelength[i])**2))*throughput[i])) 
     i+=1 
     print len(Bwavelength), len(ABconstant), ABconstant 
     a=Bwavelength[0] 
     b=Bwavelength[-1] 
     h=((b-a)/len(Bwavelength)) 
     ABflux = numpy.trapz(Bwavelength, ABconstant, h) 
     return ABflux 

def ABmagnitude(newflux, ABflux): 
    ABmagarray=[] 
    for i in range(len(newflux)): 
     ABmag = -2.5*log10((newflux[i])/ABflux) - 48.6 
     ABmagarray.append(ABmag) 
    return ABmagarray 

ABflux1 = ABconversion(Bwavelength, throughput) 
print ABflux1 
ABmagarray = ABmagnitude(z, ABflux1) 
print epoch, ABmagarray 

z在文件的前面定義,也是一個列表。

然而,當我運行此我得到的消息:

Traceback (most recent call last): 
    File "Rewrite17.11.2014.py", line 196, in <module> 
    ABflux1 = ABconversion(Bwavelength, throughput) 
    File "Rewrite17.11.2014.py", line 186, in ABconversion 
    ABflux = numpy.trapz(Bwavelength, ABconstant, h) 
    File "C:\Python27\lib\site-packages\numpy\lib\function_base.py, line 3234, in trapz 
    ret = add.reduce(d * (y[slice1]+y[slice2]/2.0, axis) 
ValueError: Operands could not be broadcast together with shapes (0,) (444,) 

我不太理解的錯誤(我是相當新的節目),但我認爲,這意味着兩個「形狀」沒有相同的尺寸。我不確定這是爲什麼。

在此先感謝。

+0

什麼行'打印LEN(Bwavelength),LEN(ABconstant),ABconstant'打印? – ErikR 2014-11-22 15:27:17

+0

它打印445,1,[0.0] – NXW 2014-11-22 15:31:03

回答

1

根據this documentation的參數trapz(y, x, dx, axis)是:

  • y - 像陣列 - 輸入陣列集成。
  • x - 可選陣列 - 如果x爲無,則所有y元素之間的間距爲dx
  • dx - 可選標量 - 如果x爲無,則假定由dx給出的間距。默認值爲1.
  • axis - 可選Int - 指定座標軸。

所以,你不應該同時指定xdx - 他們中的一個應該是None。可能這是你想要的:trapz(Bwavelength, None, h)

有關錯誤消息和NumPy的「braodcasting規則」的更多詳細信息,請參閱this answer

0

替換:

numpy.trapz(Bwavelength, ABconstant, h) 

有:

numpy.trapz(np.array(Bwavelength)[:,np.newaxis], ABconstant, h)