def __init__(self): self.read_data = ReadData() self.naive = Naive() self.branch_and_bound = Branch_And_Bound() self.tests = Tests() self.data = [] self.choice = 0 self.starting_city = 0
def test_naive(filename, p): start = time.time() f = open(filename) t = f.read() alg1 = Naive(t) results = alg1.match(p) end = time.time() col_time = end - start return col_time, results
class Tests: def __init__(self): self.data = ReadData() self.naive = Naive() self.branch_and_bound = Branch_And_Bound() # testy dla przeglądu zupełnego def testing_naive(self): for size in range(5, 12): graphs = self.data.generate_random_data( 150, size) # generowanie losowych grafów help_time = 0 total_time = 0 for j in range(50): exec_time = time.time() self.naive.naive_algorithm(graphs[j], 0) exec_time = time.time() - exec_time help_time += exec_time if help_time >= 150.0: break for i in range(50, 150): exec_time = time.time() self.naive.naive_algorithm(graphs[j], 0) exec_time = time.time() - exec_time # czas pojedynczego testu total_time += exec_time # sumowanie czasu if total_time >= 300.0: print('Wykonano: ', i - 50, 'prób') break print('Rozmiar grafu: ', size) print('Średni czas: ', total_time / (i - j)) # testy dla metody podziału i ograniczen def testing_b_b(self): # rozmiary instancji poszczególnych testów test = [5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 21] for size in test: graphs = self.data.generate_random_data(150, size) help_time = 0 total_time = 0 for j in range(50): exec_time = time.time() self.branch_and_bound.find_solution(graphs[j], 0) exec_time = time.time() - exec_time help_time += exec_time if help_time >= 150.0: break for i in range(50, 150): exec_time = time.time() self.branch_and_bound.find_solution(graphs[i], 0) exec_time = time.time() - exec_time # czas pojedynczego testu total_time += exec_time # sumowanie czasu if total_time >= 300.0: print('Wykonano: ', i - 50, 'prób') break print('Rozmiar grafu: ', size) print('Średni czas: ', total_time / (i - j))
def crossValidationIris(k=5): testData1 = np.load("iris_data/competitionData.npy") testData2 = np.load("iris_data/evaluationData.npy") trainData = np.load("iris_data/trainingData.npy") wholeData = np.concatenate((trainData, testData1, testData2)).astype(float) folds = k attTypes = [1, 1, 1, 1] trainingSets = np.split(wholeData, [(i + 1) * round(len(wholeData) / folds) for i in range(folds - 1)]) for set in trainingSets: print('\n\n\n', set, '\n\n\n') for idx, testData in enumerate(trainingSets): print(np.r_[0:idx, idx + 1:len(trainingSets) + 1]) trainingData = np.concatenate( [trainingSets[i] for i in np.r_[0:idx, idx + 1:len(trainingSets)]]) print('testData: ', len(testData)) print('trainingData: ', len(trainingData)) print('K FOLD #' + str(idx)) start = time.time() model = Naive(trainingData, attTypes) timer = time.time() print('Time to train: ', timer - start) start = time.time() evalModel = Evaluation(model, testData, CLASS_AMM_IRIS) evalModel.normalPrint() print('Took ', time.time() - start, 's')
def BayesIris(): testData1 = np.load("iris_data/competitionData.npy") testData2 = np.load("iris_data/evaluationData.npy") trainData = np.load("iris_data/trainingData.npy").astype(float) testData = np.concatenate((testData1, testData2)).astype(float) model = Naive(trainData, [1, 1, 1, 1]) ev = Evaluation(model, testData, CLASS_AMM_IRIS) ev.normalPrint()
def NBCovType(NBSizes=[5, 50, 500]): attTypes = [1 for _ in range(10)] + [0 for _ in range(44)] for size in NBSizes: print('\n\nLoading data from path=' + "cov_data/" + str(size) + "k/trainingData.npy") trainingData = np.load("cov_data/" + str(size) + "k/trainingData.npy") print('- Dataset of ' + str(size) + 'k') start = time.time() print('\n\n-- Train for NAIVE BAYES') model = Naive(trainingData, attTypes) timer = time.time() print('Time to train: ', timer - start) evalModel = Evaluation(model, competitionDataCov, CLASS_AMM_COV) print('--- Evaluation for size=' + str(size)) evalModel.normalPrint() print('Took ', time.time() - start, 's')
def crossValidationCovType(k=5, size=500): attTypes = [1 for _ in range(10)] + [0 for _ in range(44)] wholeData = np.load("cov_data/" + str(size) + "k/trainingData.npy") folds = k trainingSets = np.split(wholeData, [(i + 1) * (len(wholeData) // folds) for i in range(folds - 1)]) print([len(i) for i in trainingSets]) for idx, testData in enumerate(trainingSets): print(np.r_[0:idx, idx + 1:len(trainingSets) + 1]) trainingData = np.concatenate( [trainingSets[i] for i in np.r_[0:idx, idx + 1:len(trainingSets)]]) print('K FOLD #' + str(idx)) start = time.time() model = Naive(trainingData, attTypes) timer = time.time() print('Time to train: ', timer - start) start = time.time() evalModel = Evaluation(model, competitionDataCov, CLASS_AMM_COV) evalModel.normalPrint() print('Took ', time.time() - start, 's')
print(f"CASE {case}:") start_time_n = time.time() print(f"NAIVE: {' '.join(map(str, naive.find_pattern()))}") end_time_n = count_time(start_time_n) start_time_rk = time.time() print(f"RABIN-KARP: {' '.join(map(str, rk.find_pattern()))}") end_time_rk = count_time(start_time_rk) start_time_kmp = time.time() print(f"KNUTH-MORRIS-PRATT: {' '.join(map(str, kmp.find_pattern()))}") end_time_kmp = count_time(start_time_kmp) print("--------------------------------------------------") print( f"NAIVE TIME: {end_time_n}\nRABIN-KARP TIME: {end_time_rk}\nKNUTH-MORRIS-PRATT TIME: {end_time_kmp}" ) print(naive.find_pattern() == rk.find_pattern() == kmp.find_pattern()) print("--------------------------------------------------") if __name__ == "__main__": files_name = ["pattern", "pattern1", "text", "text1"] converted_files = setup_array(files_name) n = Naive(converted_files[0], converted_files[2]) n1 = Naive(converted_files[1], converted_files[3]) r_k = RabinKarp(converted_files[0], converted_files[2], 128, 27077) r_k1 = RabinKarp(converted_files[1], converted_files[3], 128, 27077) k_m_p = KnuthMorrisPratt(converted_files[0], converted_files[2]) k_m_p1 = KnuthMorrisPratt(converted_files[1], converted_files[3]) test(1, n, r_k, k_m_p) test(2, n1, r_k1, k_m_p1) unittest.main()
class Menu: def __init__(self): self.read_data = ReadData() self.naive = Naive() self.branch_and_bound = Branch_And_Bound() self.tests = Tests() self.data = [] self.choice = 0 self.starting_city = 0 def main_menu(self): while self.choice != 6: print('_________MENU_________') print('1. Wybierz dane') print('2. Wyświetl dane') print('3. Brute force') print('4. Branch and bound') print('5. Testy') print('6. Wyjście') self.choice = input('Wybór: ') if self.choice == '1': self.data = self.read_data.get_data() elif self.choice == '2': if self.data == [] or self.data == None: print('Brak danych do wyświetlenie.') else: print('Dane: ') for graph in self.data: print('') for row in graph: print(row) elif self.choice == '3': for graph in self.data: start = datetime.datetime.now() solution, path = self.naive.naive_algorithm( graph, self.starting_city) duration = datetime.datetime.now() - start print('Duration = ', duration) print('Solution = ', solution) print('Path = ', path) elif self.choice == '4': for graph in self.data: start = datetime.datetime.now() solution, path = self.branch_and_bound.find_solution( graph, self.starting_city) duration = datetime.datetime.now() - start print('Duration = ', duration) print('Solution = ', solution) print('Path = ', path) elif self.choice == '5': print('1. Brute force: ') print('2. Branch and bound: ') choice = int(input('Wybór: ')) if choice == 1: self.tests.testing_naive() if choice == 2: self.tests.testing_b_b() elif self.choice == '6': exit() else: print("Wprowadz poprawną liczbę")
from preprocessing import Preprocessing from naive import Naive if __name__ == "__main__": prepross = Preprocessing() instances = prepross.get_instances_of_file() preclassified_instances = prepross.pre_classfiy(instances) #prepross.bag_of_words(preclassified_instances) nv = Naive() nv.naive_bayes(preclassified_instances)
def test_naive(self): naive_test = Naive(self.test_case[0], self.test_case[1]) naive_test1 = Naive(self.test_case1[0], self.test_case1[1]) self.assertEqual(naive_test.find_pattern(), [0, 9, 13]) self.assertEqual(naive_test1.find_pattern(), [])
from naive import Naive P = 'GCAGAGAG' T = 'GCATCGCAGAGAGTATACAGTACGGCAGAGAG' algorithm = Naive(T) print(algorithm.match(P))
def timeToClassify(count=360): attTypes = [1 for _ in range(10)] + [0 for _ in range(44)] print('===============================================================') print('======= Time to classify augmenting counts on Train Set =======') print('===============================================================') trainingData = np.load("cov_data/500k/trainingData.npy").astype(float) sizes = [i + 1 for i in range(count)] knnTimes = [] id3TrainTimes = [] id3Times = [] nbTrainTimes = [] nbTimes = [] tupla = competitionDataCov[2, :-1] for i in sizes: start = time.time() nb = Naive(trainingData[:i * 1000], attTypes) ti = time.time() - start nbTrainTimes.append(ti) print('#', i, ' NB Train - ', ti) minTime = takeMinTime(nb, tupla) nbTimes.append(minTime) print('#', i, ' NB Classify - ', minTime) del nb start = time.time() id3 = ID3(trainingData[:i * 1000], 54, True, 2, attTypes, 1) ti = time.time() - start id3TrainTimes.append(ti) print('#', i, ' ID3 Train - ', ti) minTime = takeMinTime(id3, tupla) id3Times.append(minTime) print('#', i, ' ID3 Classify - ', minTime) del id3 knn = Knn2(trainingData[:i * 1000], 7) minTime = takeMinTime(knn, tupla) knnTimes.append(minTime) print('#', i, ' K-NN Classify - ', minTime) del knn plt.figure(1) plt.subplot(211) plt.ylabel('Time') plt.title('Naive Bayes train time') plt.plot(sizes, nbTrainTimes) plt.subplot(212) plt.ylabel('Time') plt.title('ID3 train time') plt.plot(sizes, id3TrainTimes) plt.figure(2) plt.subplot(311) plt.ylabel('Time') plt.title('Naive Bayes classify time') plt.plot(sizes, nbTimes) plt.subplot(312) plt.ylabel('Time') plt.title('ID3 classify time') plt.plot(sizes, id3Times) plt.subplot(313) plt.ylabel('Time') plt.title('K-NN classify time') plt.plot(sizes, knnTimes) plt.show()
from file_manager import File_manager from naive import Naive from datamuse import Datamuse import asyncio TRAIN_FILENAME = "train_data.tsv" TEST_FILENAME = "data_estag_ds.tsv" if __name__ == '__main__': filemng = File_manager() train_instances = filemng.get_instances_of_file(TRAIN_FILENAME) test_instances = filemng.get_instances_of_file(TEST_FILENAME) nv = Naive() dic = filemng.read_dictionary() dtmuse = Datamuse() loop = asyncio.get_event_loop() category_synonymous = loop.run_until_complete( dtmuse.get_synonymous('smartphone')) loop.close() category_synonymous = eval(category_synonymous) nv.naive_bayes(train_instances, dic, test_instances, category_synonymous)
image_size, latent_size, n_context, test=True, seed=seed) test_data = DataGenerator("DAVIS_Challenge", batch_size, image_size, latent_size, n_context, test=True, seed=seed) # model = VAE() # model.load("model.h5") model = Naive() for name, data in zip(("train", "val", "test"), (train_data, val_data, test_data)): input_, (truth, ) = next(data) (_, contexts) = input_ prevs, nexts = contexts[:, :, :, :3], contexts[:, :, :, 3:] pred = model.model_test.predict_on_batch(input_) for label, batch in zip(("1", "2", "truth", "pred"), (prevs, nexts, truth, pred)): result = np.round(np.clip(255 * batch, 0, 255)).astype("uint8") for i in range(result.shape[0]): imageio.imwrite("%s_%s_%d_%s.png" % (prefix, name, i, label), result[i])
def __init__(self): self.data = ReadData() self.naive = Naive() self.branch_and_bound = Branch_And_Bound()
def naive_time(self, index, iter): NaiveAlgorithm = Naive(index, self.SymmetricalMatrix) start = timer() NaiveAlgorithm.travellingSalesmanProblem() end = timer() self.NaiveTime[iter] = end - start