-1

2 つのクラスを含むスクリプトがあります。(私が扱っているエラーに関連しているとは思えない多くのものを明らかに削除しています。)最終的なタスクは、この質問で述べたように、決定木を作成することです。

残念ながら、無限ループが発生しており、その理由を特定するのが困難です。混乱しているコード行を特定しましたが、イテレータと追加先のリストは別のオブジェクトだと思っていたでしょう。私が気付いていないリストの .append 機能の副作用はありますか? それとも、私は他の盲目的に明らかな間違いを犯していますか?

class Dataset:
    individuals = [] #Becomes a list of dictionaries, in which each dictionary is a row from the CSV with the headers as keys
    def field_set(self): #Returns a list of the fields in individuals[] that can be used to split the data (i.e. have more than one value amongst the individuals
    def classified(self, predicted_value): #Returns True if all the individuals have the same value for predicted_value
    def fields_exhausted(self, predicted_value): #Returns True if all the individuals are identical except for predicted_value
    def lowest_entropy_value(self, predicted_value): #Returns the field that will reduce <a href="http://en.wikipedia.org/wiki/Entropy_%28information_theory%29">entropy</a> the most
    def __init__(self, individuals=[]):

class Node:
    ds = Dataset() #The data that is associated with this Node
    links = [] #List of Nodes, the offspring Nodes of this node
    level = 0 #Tree depth of this Node
    split_value = '' #Field used to split out this Node from the parent node
    node_value = '' #Value used to split out this Node from the parent Node

    def split_dataset(self, split_value): #Splits the dataset into a series of smaller datasets, each of which has a unique value for split_value.  Then creates subnodes to store these datasets.
        fields = [] #List of options for split_value amongst the individuals
        datasets = {} #Dictionary of Datasets, each one with a value from fields[] as its key
        for field in self.ds.field_set()[split_value]: #Populates the keys of fields[]
            fields.append(field)
            datasets[field] = Dataset()
        for i in self.ds.individuals: #Adds individuals to the datasets.dataset that matches their result for split_value
            datasets[i[split_value]].individuals.append(i) #<---Causes an infinite loop on the second hit
        for field in fields: #Creates subnodes from each of the datasets.Dataset options
            self.add_subnode(datasets[field],split_value,field)

    def add_subnode(self, dataset, split_value='', node_value=''):
    def __init__(self, level, dataset=Dataset()):

私の初期化コードは現在:

if __name__ == '__main__':
    filename = (sys.argv[1]) #Takes in a CSV file
    predicted_value = "# class" #Identifies the field from the CSV file that should be predicted
    base_dataset = parse_csv(filename) #Turns the CSV file into a list of lists
    parsed_dataset = individual_list(base_dataset) #Turns the list of lists into a list of dictionaries
    root = Node(0, Dataset(parsed_dataset)) #Creates a root node, passing it the full dataset
    root.split_dataset(root.ds.lowest_entropy_value(predicted_value)) #Performs the first split, creating multiple subnodes
    n = root.links[0] 
    n.split_dataset(n.ds.lowest_entropy_value(predicted_value)) #Attempts to split the first subnode.
4

2 に答える 2

4
class Dataset:
    individuals = []

疑わしい。のすべてのインスタンスで静的メンバー リストを共有したい場合を除き、Datasetそうすべきではありません。self.individuals= somethingで設定する場合は__init__、ここでも設定する必要はありませんindividuals

    def __init__(self, individuals=[]):

まだ疑わしい。individualsに引数を割り当てていますself.individualsか? その場合、individuals関数の定義時に作成された同じリストDatasetを、デフォルトの引数で作成されたすべてのリストに割り当てています。リストに項目を追加するDatasetと、明示的なindividuals引数なしで作成された他のすべての項目もその項目を取得します。

同様に:

class Node:
    def __init__(self, level, dataset=Dataset()):

Node明示的な引数なしで作成されたすべての は、まったく同じデフォルトインスタンスdatasetを受け取ります。Dataset

これは変更可能なデフォルト引数の問題であり、それが生成する破壊的な反復の種類は、無限ループを引き起こす可能性が非常に高いようです.

于 2010-05-13T23:55:52.197 に答える
4

イテレータが最後に到達する前に、繰り返し処理している同じリストに追加しているため、サイズが大きくなっていると思われます。代わりに、リストのコピーを反復処理してみてください。

for i in list(self.ds.individuals):
    datasets[i[split_value]].individuals.append(i) 
于 2010-05-13T23:34:12.793 に答える