2013-04-05 69 views
2
y1 = [] 
y2 = [] 
x = [] 
for i in range(40): 
    #fer dos llistes (error y epoch) y despres fer un plot 
    trainer.trainEpochs(1) 
    trnresult = percentError(trainer.testOnClassData(),trndata['class']) 
    tstresult = percentError(trainer.testOnClassData(dataset=tstdata),tstdata['class']) 

    print "epoch: %4d" % trainer.totalepochs, \ 
      " train error: %5.2f%%" % trnresult, \ 
      " test error: %5.2f%%" % tstresult 

    if i==1: 
     g=Gnuplot.Gnuplot() 
    else: 
     y1 = y1.append(float(trnresult)) 
     y2 = y2.append(float(tstresult)) 
     x = x.append(i) 
d1=Gnuplot.Data(x,y1,with_="line") 
d2=Gnuplot.Data(x,y2,with_="line") 
g.plot(d1,d2) 

大家好,我第一次在這裏發帖,但感謝您的工作。製作訓練數據集的圖形python

好吧,我正在使用神經網絡(多層preceptron)和我正在與UCI ML知識庫進行測試,我必須作出錯誤與時代數量的圖形顯示,但我不知道什麼i'm做錯了,這是我得到的錯誤:

y1 = y1.append(float(trnresult)) 
AttributeError: 'NoneType' object has no attribute 'append' 

從來就tryed與int和漂浮在y1.append(),但我得到了同樣的錯誤。 這是我得到的控制檯:

Number of training patterns: 233 

Input and output dimensions: 6 2 

First sample (input, target, class): 

[ 63.03 22.55 39.61 40.48 98.67 -0.25] [1 0] [ 0.] 

Total error: 0.110573541007 

epoch: 1 train error: 33.05% test error: 29.87% 

Total error: 0.0953749484982 

epoch: 2 train error: 32.19% test error: 35.06% 

Total error: 0.0977600868845 

epoch: 3 train error: 27.90% test error: 29.87% 

Traceback (most recent call last): 
    File "C:\Python\Practice\dataset.py", line 79, in <module> 
    y1 = y1.append(float(trnresult)) 
AttributeError: 'NoneType' object has no attribute 'append' 

感謝。

回答

1

列表中的append()函數沒有返回值。因此y1被替換爲None。您應該執行y1.append()y2.append(),而不將其分配回y1y2

更具體地說

>>> a = [] 
>>> b = a.append(1) 
>>> b is None 
True 
>>> a 
[1] 
>>> a.append(2) 
>>> a 
[1, 2] 

如果你願意,你可以使用上列出了+操作(注意[]周圍3):

>>> a = a + [3] 
>>> a 
[1, 2, 3] 
+0

好了,第二個解釋似乎完美地工作,謝謝。 – Faramir 2013-04-05 16:41:16

相關問題