大佬们,能帮我看看我这代码错误在哪吗
这段代码是要实现一个决策树,在predict这个函数下面我想实现实现一个筛选,使得B中只剩下满足条件的项,结果这段代码单独拿出去可以用,放在里面就报错了。报错内容显示local variable 'D' referenced before assignment。没怎么学过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=dataset
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=dataset
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==dataset):
C.append(dataset)
x=x+1
y=y+1
z=0
while(z<B):
d=np.zeros((len(C),1))
i=0
while (i<len(C)):
d=C
i=i+1
D.append(self.entropy(d))
z=z+1
JG=0
e=0
while(e<B):
JG=JG+GP*D
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)
E.append(f)
i=i+1
g=max(E)
j=0
while(j<k):
if(g==E):
sel=feature
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
while(u<len(B)):
if(data == B):
b=B
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")
钻研这么久 辛苦了可 想明白了。。。我真够蠢得。话说怎么删掉这个帖子。
笑傲江湖jd 发表于 2022-5-13 01:17
想明白了。。。我真够蠢得。话说怎么删掉这个帖子。
重新编辑删除内容即可 感谢楼主无私分享,万分感谢~~~
页:
[1]