1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
| import numpy as np
import copy
class decision_tree:
def __init__(self, features, output, dataset):
self.features = features
self.output = output
self.dataset = dataset
self.depth = 0
def log(self, x):
return np.log2(x)
def get_prob(self, array):
(unique, counts) = np.unique(array, return_counts=True, axis=0)
return counts/len(array)
def entropy(self, array):
p = self.get_prob(array)
return -np.sum(p*np.log2(p))
def output_entropy(self):
output_array = []
for data in self.dataset:
output_array.append(data.get(self.output))
return self.entropy(output_array)
def conditional_entropy(self, feature):
feature_array = []
for data in self.dataset:
feature_array.append(data.get(feature))
weight_array = self.get_prob(feature_array)
(unique, counts) = np.unique(feature_array, return_counts=True)
entropy_array = []
for u in unique:
list = []
for data in self.dataset:
if data.get(feature) == u:
list.append(data.get(self.output))
entropy_array.append(self.entropy(list))
result = np.sum(np.array(weight_array) * entropy_array)
return result
def feature_selection(self):
v = 0
selected = '我是字符串'
for f in self.features:
v1 = self.output_entropy()-self.conditional_entropy(f)
if v1 >= v:
selected = f
return selected
def tof(self,classify_dataset):
(unique, counts) = np.unique([d.get(self.output) for d in classify_dataset],return_counts=True)
if len(counts) == 2:
if counts[0] > counts[1]:
return unique[0]
else:
return unique[1]
else:
return unique[0]
def classify(self,selected_feature,u):
classify_dataset = []
for d in self.dataset:
if d.get(selected_feature) == u:
classify_dataset.append(d)
return classify_dataset
def train(self, data):
global decision_order,depth,max_depth,tree,branch
depth = depth + 1
if depth <= max_depth:
selected_feature = self.feature_selection()
if len(data) == 0:
depth = depth - 1
max_depth = depth
print("最大深度为-------------",max_depth)
return
else:
if max_depth == 100:
decision_order.append(selected_feature)
feature_array = []
for d in self.dataset:
feature_array.append(d.get(decision_order[depth-1]))
(unique, counts) = np.unique(feature_array, return_counts=True)
decision_feature=list(data)
decision_feature.remove(selected_feature)
for u in unique:
branch.append(u)
classify_dataset=self.classify(selected_feature,u)
if depth != max_depth:
predict_tree=decision_tree(decision_feature,self.output,classify_dataset)
predict_tree.train(decision_feature)
if depth == max_depth:
a = self.tof(classify_dataset)
tree[tuple(branch)]=a
branch.pop(-1)
depth = depth - 1
return
def predict(self, data):
data1 = copy.deepcopy(data)
if self.output in data1:
del data1[self.output]
if tree == {}:
self.train(data1)
if len(data1)==len(decision_order):
my_judge = tree[tuple([data1.get(d) for d in decision_order])]
return my_judge
return
decision_order = []
depth = 0
max_depth = 100
tree = {}
branch = []
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)
print(my_tree.predict({'age': 19, 'male': False, 'single': False}), "should be 0 or False")
def tree_example(data):
if data["single"]:
return True
else:
return False
true_classification = 0
for data in dataset:
if tree_example(data) == data["visit_library_in_Sunday"]:
true_classification += 1
print("One feature classification accuracy:", true_classification/len(dataset))
true_classification = 0
for data in dataset:
if my_tree.predict(data) == data["visit_library_in_Sunday"]:
true_classification += 1
print("Decision tree classification accuracy:", round(true_classification/len(dataset),4), "should be around 0.9412")
|