from decision_tree_learning import build_tree, entropy, classify if __name__ == '__main__': x = input().split(', ') test_case = list(map(float, x[:-1])) + [int(x[-1])] test1 = dataset[:int(len(dataset) * 0.3)] test2 = dataset[int(len(dataset) * 0.3):int(len(dataset) * 0.6)] test3 = dataset[int(len(dataset) * 0.6):] drvo1 = build_tree(test1, entropy) drvo2 = build_tree(test2, entropy) drvo3 = build_tree(test3, entropy) glasovi = {0: 0, 1: 0, 2: 0} klasa1 = max(classify(test_case, drvo1).items(), key=lambda x: x[1])[0] klasa1 = '' max = 0 for x in classify(test_case, drvo1).items(): if x[1] > max: max = x[1] klasa1 = x[0] klasa2 = '' max = 0 for x in classify(test_case, drvo2).items(): if x[1] > max: max = x[1] klasa2 = x[0]
[600.0, 29.4, 32.0, 37.2, 41.5, 15.0, 'Bream'], [145.0, 22.0, 24.0, 25.5, 25.0, 15.0, 'Perch'], [1100.0, 40.1, 43.0, 45.5, 27.5, 16.3, 'Perch']] from math import log10 log2 = lambda x: log10(x) / log10(2) if __name__ == "__main__": test_case = input() test_case = [float(x) for x in test_case.split(', ')[:-1]] + [test_case.split(', ')[-1]] from decision_tree_learning import build_tree, classify, entropy lista = [] for x in data: if x[-1] == 'Roach': lista.append(x) lista2 = [] for x in data: if x[-1] == 'Pike': lista2.append(x) train_data = lista[0:40] + lista2[0:40] tree = build_tree(train_data, entropy) print(max(classify(test_case, tree).items(), key=lambda x: x[1])[0])
['google', 'UK', 'no', 21, 'Premium'], ['(direct)', 'New Zealand', 'no', 12, 'None'], ['(direct)', 'UK', 'no', 21, 'Basic'], ['google', 'USA', 'no', 24, 'Premium'], ['slashdot', 'France', 'yes', 19, 'None'], ['digg', 'USA', 'no', 18, 'None'], ['google', 'UK', 'no', 18, 'None'], ['kiwitobes', 'UK', 'no', 19, 'None'], ['digg', 'New Zealand', 'yes', 12, 'Basic'], ['slashdot', 'UK', 'no', 21, 'None'], ['google', 'UK', 'yes', 18, 'Basic'], ['kiwitobes', 'France', 'yes', 19, 'Basic']] test_cases = [['google', 'MK', 'no', 24, 'Unknown'], ['google', 'MK', 'no', 15, 'Unknown'], ['digg', 'UK', 'yes', 21, 'Unknown'], ['digg', 'UK', 'no', 25, 'Unknown']] if __name__ == '__main__': tree = build_tree(my_data, entropy) print(tree) for row in test_cases: result = classify(row, tree) # Opcija 1 import operator class_name = max(result.items(), key=operator.itemgetter(1))[0] # Opcija 2 class_name = max(result.items(), key=lambda x: x[1])[0] print(row) print(class_name)
if __name__ == '__main__': x = input().split(', ') test_case = list(map(float, x[:-1])) + [int(x[-1])] mnoz1 = dataset[:int(len(dataset) * 0.3)] mnoz2 = dataset[int(len(dataset) * 0.3):int(len(dataset) * 0.6)] mnoz3 = dataset[int(len(dataset) * 0.6):] drvo1 = build_tree(mnoz1, entropy) drvo2 = build_tree(mnoz2, entropy) drvo3 = build_tree(mnoz3, entropy) glasovi = {0: 0, 1: 0, 2: 0} klasa1 = max(classify(test_case, drvo1).items(), key=lambda x: x[1])[0] klasa2 = max(classify(test_case, drvo2).items(), key=lambda x: x[1])[0] klasa3 = max(classify(test_case, drvo3).items(), key=lambda x: x[1])[0] glasovi[klasa1] += 1 glasovi[klasa2] += 1 glasovi[klasa3] += 1 sorted_klasi = list( sorted(glasovi.items(), key=lambda x: x[1], reverse=True)) klasa = sorted_klasi[0][0] if sorted_klasi[0][1] == sorted_klasi[1][1]: print('unknown') else:
att3 = float(input()) att4 = float(input()) planttype = input() testCase = [att1, att2, att3, att4, planttype] from decision_tree_learning import build_tree, classify, entropy golemina = len(trainingData) // 2 set1 = trainingData[:golemina] set2 = trainingData[golemina:] drvo1 = build_tree(set1, entropy) drvo2 = build_tree(set2, entropy) prediction1 = classify(testCase, drvo1) prediction2 = classify(testCase, drvo2) class1 = '' max_value = 0 # Alternativno resenie e upotreba na lista i sort na listata za da se sortiraat klasite # possible_values = [] for x in prediction1.items(): if x[1] > max_value: max_value = x[1] class1 = x[0] # possible_values = [x[0]] elif x[1] == max_value: # possible_values.append(x[0]) if class1 > x[0]: class1 = x[0]
column_ind = int(input()) train_new = [] for x in train: row_new = x[:] row_new.pop(column_ind) train_new.append(row_new) test_new = [] for x in test: row_new = x[:] row_new.pop(column_ind) test_new.append(row_new) drvo1 = build_tree(train, entropy) drvo2 = build_tree(train_new, entropy) brojach = 0 for x in test: klasa = max(classify(x, drvo1).items(), key=lambda x: x[1])[0] if klasa == x[-1]: brojach += 1 print(brojach / len(test)) brojach2 = 0 for x in test_new: klasa = max(classify(x, drvo2).items(), key=lambda x: x[1])[0] if klasa == x[-1]: brojach2 += 1 print(brojach2 / len(test_new))
train_data = dataset[:divide] test_data = dataset[divide:] copy_train_data = copy.deepcopy(train_data) copy_test_data = copy.deepcopy(train_data) t1 = build_tree(train_data) t2 = build_tree(del_column(copy_train_data, column_ind)) total = len(test_data) hits_t1 = 0 for item in test_data: prediction1 = get_max_prediction_custom(classify(item, t1)) if prediction1 == item[-1]: hits_t1 += 1 del_column(copy_test_data, column_ind) hits_t2 = 0 for item in copy_test_data: prediction2 = get_max_prediction_custom(classify(item, t2)) if prediction2 == item[-1]: hits_t2 += 1
sample = input() from document_classification import NaiveBayes, get_words classifier = NaiveBayes(get_words) for train_sample in train_data: classifier.train(train_sample[0], train_sample[1]) class1 = classifier.classify_document(sample) print(f'Klasa so Naiven baesov klasifikator: {class1}') from decision_tree_learning import build_tree, classify,\ print_tree, entropy from tf_idf import create_dataset, process_document documents = [t[0] for t in train_data] labels = [t[1] for t in train_data] dataset, df, N, vocab = create_dataset(documents, labels) tree = build_tree(dataset, entropy) # print_tree(tree) sample_vector = process_document(sample, df, N, vocab) class2 = sorted(classify(sample_vector, tree).items(), key=lambda x: x[1])[0][0] print(f'Klasa dobiena so drva na odluka: {class2}') if class1 == class2: print('Dvata klasifikatori predviduvaat ista klasa') else: print('Dvata klasifikatori ne predviduvaat ista klasa')