吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1492|回复: 4
收起左侧

[求助] 大佬们,能帮我看看我这代码错误在哪吗

[复制链接]
笑傲江湖jd 发表于 2022-5-12 23:48
40吾爱币
这段代码是要实现一个决策树,在predict这个函数下面我想实现实现一个筛选,使得B中只剩下满足条件的项,结果这段代码单独拿出去可以用,放在里面就报错了。报错内容显示local variable 'D' referenced before assignment。没怎么学过python,代码写的很乱,求大佬们帮忙看一看吧。
[Python] 纯文本查看 复制代码
import numpy as np

class decision_tree:
    def __init__(self, features, output, dataset):
        self.features = features
        self.output = output
        self.dataset = dataset
   
    def log(self, x):
        return np.log2(x)
    
    # you can use this function to calculate the emprical probability of a random variable under a dataset
    def get_prob(self, array):
        (unique, counts) = np.unique(array, return_counts=True, axis=0)
        return counts/len(array)
        
    # you can use this function to calculate the emprical entropy of a random variable under a dataset
    def entropy(self, array):
        p = self.get_prob(array)
        return -np.sum(p*np.log2(p))
    
    def output_entropy(self):
        # calculate the emprical entropy of the output
        # you can use your code in the last assignment
        output_data=np.zeros((len(dataset),1))
        i=0
        while (i<len(dataset)):
            output_data[i]=dataset[i][self.output]
            i=i+1
        return self.entropy(output_data)
    
    def conditional_entropy(self, feature):
        # calculate the emprical conditional entropy of the output relative to the "feature"
        # you can use your code in the last assignment
        RE=np.zeros((len(dataset),1))
        j=0
        while (j<len(dataset)):
            RE[j]=dataset[j][feature]
            j=j+1
        GP=self.get_prob(RE)
        A=np.unique(RE)
        B=len(A)
        k=0
        C=[]
        D=[]
        while(k<B):
            C.append([])
            k=k+1
        y=0
        while(y<B):
            x=0
            while(x<len(dataset)):
                if (A[y]==dataset[x][feature]):
                    C[y].append(dataset[x])
                x=x+1
            y=y+1
        z=0
        while(z<B):
            d=np.zeros((len(C[z]),1))
            i=0
            while (i<len(C[z])):
                d[i]=C[z][i][self.output]
                i=i+1
            D.append(self.entropy(d))
            z=z+1
        JG=0
        e=0
        while(e<B):
            JG=JG+GP[e]*D[e]
            e=e+1
        return JG
            
    def feature_selection(self,feature):
        # select the feature has maximum mutual information
        # you can use your code in the last assignment
        i=0
        k=len(feature)
        E=[]
        while(i<k):
            f=self.output_entropy()-self.conditional_entropy(feature[i])
            E.append(f)
            i=i+1
        g=max(E)
        j=0
        while(j<k):
            if(g==E[j]):
                sel=feature[j]
            j=j+1
        return sel
    
    def predict(self, data):
        # make prediction for an arbitrary data input
        A=[]
        k=len(self.features)
        feature=self.features
        while(k>0):
            a=self.feature_selection(feature)
            A.append(a)
            feature.remove(a)
            k=k-1
        print(A)
        j=0
        B=dataset
        k=len(self.features)
        while(j<k):
            u=0
            D=[]
            D.clear()
            a=A[j]
            while(u<len(B)):
                if(data[a] == B[u][a]):
                    b=B[u]
                    D.append(b)
                u=u+1
            j=j+1
            B=D
            
        print(D)
        return 
                    

    
        
        
dataset = [
    {"age": 19, "male": False, "single": False, "visit_library_in_Sunday": False},
    {"age": 19, "male": False, "single": False, "visit_library_in_Sunday": False},
    {"age": 19, "male": True,  "single": False, "visit_library_in_Sunday": True},
    {"age": 19, "male": True,  "single": True,  "visit_library_in_Sunday": True},
    {"age": 19, "male": False, "single": False, "visit_library_in_Sunday": False},
    
    {"age": 20, "male": False, "single": False, "visit_library_in_Sunday": False},
    {"age": 20, "male": False, "single": False, "visit_library_in_Sunday": False},
    {"age": 20, "male": True,  "single": True,  "visit_library_in_Sunday": True},
    {"age": 20, "male": False, "single": True,  "visit_library_in_Sunday": True},
    {"age": 20, "male": False, "single": True,  "visit_library_in_Sunday": True},
    
    {"age": 21, "male": False, "single": True,  "visit_library_in_Sunday": True},
    {"age": 21, "male": False, "single": True,  "visit_library_in_Sunday": True},
    {"age": 21, "male": True,  "single": False, "visit_library_in_Sunday": True},
    {"age": 21, "male": True,  "single": False, "visit_library_in_Sunday": True},
    {"age": 21, "male": False, "single": False, "visit_library_in_Sunday": False},
    {"age": 21, "male": False, "single": False, "visit_library_in_Sunday": False},
    {"age": 21, "male": False, "single": False, "visit_library_in_Sunday": True}
]   
    
        
my_tree = decision_tree(\
    ["age", "male", "single"], "visit_library_in_Sunday", dataset)


# Test 1
print(my_tree.predict({"age": 19, "male": False, "single": False}), "should be 0 or False")

最佳答案

查看完整内容

钻研这么久 辛苦了可

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

一笑懸命桑 发表于 2022-5-12 23:48
钻研这么久 辛苦了可
 楼主| 笑傲江湖jd 发表于 2022-5-13 01:17
想明白了。。。我真够蠢得。话说怎么删掉这个帖子。
海是倒过来的天 发表于 2022-5-13 09:22
笑傲江湖jd 发表于 2022-5-13 01:17
想明白了。。。我真够蠢得。话说怎么删掉这个帖子。

重新编辑删除内容即可
疯桦绝代 发表于 2022-5-13 10:45
感谢楼主无私分享,万分感谢~~~
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 18:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表