2017-12-18 95 views
0

我不確定我做錯了什麼,因爲我幾乎可以肯定地確定我已經引用了變量,並且都是正確的。課程與功能 -

我對使用函數還不太熟悉,並且剛剛開始學習如何在一天前使用Python類。

所以,當我運行代碼,我得到這個錯誤信息:

line 37, in pathlist 
    while self.no_of_files > 0:    #self.number_of_files 
AttributeError: 'int' object has no attribute 'no_of_files' 

我猜它是與我的代碼順序步驟,或者是因爲我已經轉換輸入到代碼第20行中的int()的numfiles。

我附上我的代碼如下。請幫我在此先感謝:)

import csv 
import numpy as np 


''' DEFINING MAIN CONTROL''' 

def main(): 
    no_of_files # = number_of_files() 
    a = Calculate_RMSE_Assess_Models() 
    a.no_of_files() # = no_of_files 
    a.pathlist() 
    a.out_path() 
    a.open_read_write_files() 


''' DEFINING CLASS OF ALL ''' 
class Calculate_RMSE_Assess_Models: 

    def __init__(self, no_of_files): 
     self.no_of_files = no_of_files 

    def number_of_files(): 
     numfiles = input("Enter the number of files to iterate through: ") 
     numfilesnumber = int(numfiles) 
     return numfilesnumber 

    no_of_files = number_of_files() 


    def pathlist(self): 
     filepathlist = [] 
     while self.no_of_files > 0:    #self.number_of_files 
      path = input("Enter the filepath of the input file: ") 
      filepathlist.append(path) 
      no_of_files = no_of_files - 1 
     return filepathlist 

    list_filepath = pathlist(no_of_files) 

    def out_path(): 
     path = input("Enter the file path of output path: ") 
     return path 

    file_out_path = outpath() 

    def open_read_write_files(): 
     with open('{d[0]}'.format(d=list_filepath), 'r') as csvinput, open('{d[1]}'.format(d=list_filepath), 'r') as csvinput2, open('d{[2]}'.format(d=list_filepath), 'r') as csvinput3, open('{d}'.format(d=file_out_path), 'w') as csvoutput: 
      reader, reader2, reader3 = csv.reader(csvinput, csvinput2, csvinput3)            #1: Decision Forest, 2: Boosted Decision Tree, 3: ANN 
      writer = csv.DictWriter(csvoutput, lineterminator='\n', fieldnames = ['oldRMSE', 'Decision Forest Regression RMSE', 'Boosted Decision Tree Regression RMSE', 'Neural Network Regression RMSE', 'Old Accurate Predictions', 'Old Inaccurate Predictions', 'Decision Forest Accurate Predictions', 'Decision Forest Inaccurate Predictions', 'Boosted Decision Tree Accurate Predictions', 'Boosted Decision Tree Inaccurate Predictions', 'Neural Network Accurate Predictions', 'Neural Network Inaccurate Predictions']) 

      ####################################### 
      #For Decision Forest Predictions 
      headerline = next(reader) 
      emptyl=[] 
      for row in reader: 
       emptyl.append(row) 

      #Calculate RMSE 
      DecFSqResidSum = 0 
      for row in emptyl: 
       for cell in row: 
        if cell == row[-3]: 
         DecFSqResidSum = float(cell) + DecFSqResidSum 
      DecFSqResidAvg = DecFSqResidSum/len(emptyl) 
      DecForest_RMSE = np.sqrt(DecFSqResidAvg) 

      #Constructing No. of Correct/Incorrect Predictions 
      DecisionForest_Accurate = 0 
      DecisionForest_Inaccurate = 0 
      Old_Accurate = 0 
      Old_Inaccurate = 0 
      for row in emptyl: 
       for cell in row: 
        if cell == row[-2] and 'Accurate' in cell: 
         Old_Accurate += 1 
        else: 
         Old_Inaccurate += 1 
        if cell == row[-1] and 'Accurate' in cell: 
         DecisionForest_Accurate += 1 
        else: 
         DecisionForest_Inaccurate += 1 


      ###################################### 
      #For Boosted Decision Tree 
      headerline2 = next(reader2) 
      emptyl2=[]          #make new csv file(list) from csv reader 
      for row in reader2: 
       emptyl2.append(row) 

      #Calculate RMSE 
      OldSqResidSum = 0 
      BoostDTSqResidSum = 0 
      for row in emptyl2:        #make Sum of Squared Residuals 
       for cell in row: 
        if cell == row[-4]: 
         OldSqResidSum = float(cell) + OldSqResidSum 
        if cell == row[-3]: 
         BoostDTSqResidSum = float(cell) + BoostDTSqResidSum 
      OldSqResidAvg = OldSqResidSum/len(emptyl2) #divide by N to get average 
      BoostDTResidAvg = BoostDTSqResidSum/len(emptyl2) 
      oldRMSE = np.sqrt(OldSqResidAvg)    #calculate RMSE of ESTARRTIME & Boosted Decision Tree 
      BoostedDecTree_RMSE = np.sqrt(BoostDTResidAvg) 

      #Constructing Correct/Incorrect Predictions 
      BoostedDT_Accurate = 0 
      BoostedDT_Inaccurate = 0 
      for row in emptyl2: 
        if cell == row[-1] and 'Accurate' in cell: 
         BoostedDT_Accurate += 1 
        else: 
         BoostedDT_Inaccurate += 1 



      ###################################### 
      #For Artificial Neural Network (ANN) Predictions 
      headerline3 = next(reader3) 
      emptyl3=[] 
      for row in reader3: 
       emptyl3.append(row) 

      #Calculate RMSE 
      ANNSqResidSum = 0 
      for row in emptyl3: 
       for cell in row: 
        if cell == row[-3]: 
         ANNSqResidSum = float(cell) + ANNSqResidSum 
      ANNSqResidAvg = ANNSqResidSum/len(emptyl3) 
      ANN_RMSE = np.sqrt(ANNSqResidAvg) 

      #Constructing Correct/Incorrect Predictions 
      ANN_Accurate = 0 
      ANN_Inaccurate = 0 
      for row in emptyl3: 
       for cell in row: 
        if cell == row[-1] and 'Accurate' in cell: 
         ANN_Accurate += 1 
        else: 
         ANN_Inaccurate += 1 



      #Compile the Error Measures 
      finalcsv = [] 
      finalcsv.append(oldRMSE) 
      finalcsv.append(DecForest_RMSE) 
      finalcsv.append(BoostedDecTree_RMSE) 
      finalcsv.append(ANN_RMSE) 
      finalcsv.append(Old_Accurate) 
      finalcsv.append(Old_Inaccurate) 
      finalcsv.append(DecisionForest_Accurate) 
      finalcsv.append(DecisionForest_Inaccurate) 
      finalcsv.append(BoostedDT_Accurate) 
      finalcsv.append(BoostedDT_Inaccurate) 
      finalcsv.append(ANN_Accurate) 
      finalcsv.append(ANN_Inaccurate) 



      #Write the Final Comparison file 
      writer.writeheader() 
      writer.writerows({'oldRMSE': row[0], 'Decision Forest Regression RMSE': row[1], 'Boosted Decision Tree Regression RMSE': row[2], 'Neural Network Regression RMSE': row[3], 'Old Accurate Predictions': row[4], 'Old Inaccurate Predictions': row[5], 'Decision Forest Accurate Predictions': row[6], 'Decision Forest Inaccurate Predictions': row[7], 'Boosted Decision Tree Accurate Predictions': row[8], 'Boosted Decision Tree Inaccurate Predictions': row[9], 'Neural Network Accurate Predictions': row[10], 'Neural Network Inaccurate Predictions': row[11]} for row in np.nditer(finalcsv)) 


main() 

回答

0

你應該給一個no_of_files PARAMS通過調用高清初始化(個體經營,no_of_files)創建一個 實例Calculate_RMSE_Assess_Models時。

0

您需要添加selfnumber_of_files()out_path()簽名,並open_read_write_file()

class Calculate_RMSE_Assess_Models: 

    def __init__(self, no_of_files): 
     self.no_of_files = no_of_files 

    def number_of_files(): 
     numfiles = input("Enter the number of files to iterate through: ") 
     numfilesnumber = int(numfiles) 
     return numfilesnumber 

    def pathlist(self): 
     filepathlist = [] 
     while self.no_of_files > 0:    #self.number_of_files 
      path = input("Enter the filepath of the input file: ") 
      filepathlist.append(path) 
      no_of_files = no_of_files - 1 
     return filepathlist 

    def out_path(self): 
     path = input("Enter the file path of output path: ") 
     return path 


    def open_read_write_files(self): 
     pass 

但是,如果您希望保留類中的一個函數的性質,可以使用classmethod裝飾:

class Calculate_RMSE_Assess_Models: 

    def __init__(self, no_of_files): 
     self.no_of_files = no_of_files 
    @classmethod 
    def number_of_files(cls): 
     numfiles = input("Enter the number of files to iterate through: ") 
     numfilesnumber = int(numfiles) 
     return numfilesnumber 

    def pathlist(self): 
     filepathlist = [] 
     while self.no_of_files > 0:    #self.number_of_files 
      path = input("Enter the filepath of the input file: ") 
      filepathlist.append(path) 
      no_of_files = no_of_files - 1 
     return filepathlist 
    @classmethod 
    def out_path(cls): 
     path = input("Enter the file path of output path: ") 
     return path 

    @classmethod 
    def open_read_write_files(cls): 
     pass 
0

在你的班級定義中,你有list_filepath = pathlist(no_of_files)。這稱爲pathlistno_of_filesselfno_of_filesint,因此while self.no_of_files > 0:正試圖訪問intno_of_files屬性。

完整的回溯顯示了這一點。在查看這樣的問題時發佈完整的Traceback是有幫助的。

Traceback (most recent call last): 
    File "redacted", line 17, in <module> 
    class Calculate_RMSE_Assess_Models: 
    File "redacted", line 38, in Calculate_RMSE_Assess_Models 
    list_filepath = pathlist(no_of_files) 
    File "redacted", line 32, in pathlist 
    while self.no_of_files > 0:    #self.number_of_files 
AttributeError: 'int' object has no attribute 'no_of_files' 
+0

'回溯(最近最後調用): 文件 「」,第22行,在 類Calculate_RMSE_Assess_Models: 文件 「」,43行,在Calculate_RMSE_Assess_Models list_filepath = pathlist(no_of_files) 文件 「」 ,第37行,在路徑列表 while self.no_of_files> 0: AttributeError:'int'object has no attribute'no_of_files'' – Christoph

+0

對不起,我沒有在OG文章中包含完整的回溯,認爲它並不重要就像我不得不手動刪除文件目錄一樣,因爲它包含一些機密的東西。 – Christoph

+0

我的答案解釋了爲什麼你會得到這個特定的錯誤。在看回溯時,試着在腦海中走過它。 – Galen