Best Python code snippet using assertpy_python
linreg.py
Source:linreg.py
1import csv2import numpy as np3import matplotlib.pyplot as plt4def read_data(filename):5 data = []6 with open(filename) as csvfile:7 reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)8 for row in reader:9 data.append(row)10 return np.array(data)11def mse(l, train, test, train_regs, test_regs):12 tp = np.matrix.transpose(train)13 i = np.identity(len(train[0]))14 train_t = np.matrix.transpose(train_regs)[0]15 test_t = np.matrix.transpose(test_regs)[0]16 w = np.dot(np.matmul(np.linalg.inv((l * i) + np.matmul(tp, train)), tp), train_t)17 return np.mean(np.square(np.matmul(test, w) - test_t)) 18def task1(train_sets, train_regs, test_sets, test_regs):19 sets = ["100-10", "100-100", "1000-100", "forest_fire", "real_estate", "50(1000)-100", "100(1000)-100", "150(1000)-100"]20 print("Lambda\tMSE")21 for i in range(len(train_sets)):22 train_mse = []23 test_mse = []24 for l in range(0, 151):25 train_mse.append(mse(l, train_sets[i], train_sets[i], train_regs[i], train_regs[i]))26 test_mse.append(mse(l, train_sets[i], test_sets[i], train_regs[i], test_regs[i]))27 plt.plot(range(0, 151), train_mse, label = "Train")28 plt.plot(range(0, 151), test_mse, label = "Test")29 plt.legend()30 plt.ylabel("MSE")31 plt.xlabel("Lambda")32 plt.title(sets[i])33 plt.show()34 print(str(np.argmin(test_mse)) + "\t" + str(min(test_mse)))35def task2(train_sets, train_regs, test_sets, test_regs):36 print("Lambda\tMSE")37 for i in range(len(train_sets)):38 mse_folds = []39 for j in range (0, 10):40 test_nums = range(0, len(train_sets[i]))[j::10]41 train_nums = np.setdiff1d(range(0, len(train_sets[i])), test_nums)42 train = np.take(train_sets[i], train_nums, axis = 0)43 test = np.take(train_sets[i], test_nums, axis = 0)44 train_reg = np.take(train_regs[i], train_nums)45 test_reg = np.take(train_regs[i], test_nums)46 47 mses = []48 for l in range(0, 151):49 mses.append(mse(l, train, test, train_reg, test_reg))50 mse_folds.append(mses)51 opt_l = np.argmin(np.sum(mse_folds, axis = 0))52 test_mse = mse(l, train_sets[i], test_sets[i], train_regs[i], test_regs[i])53 print(str(opt_l) + "\t" + str(test_mse))54 return55def task3(train_sets, train_regs, test_sets, test_regs):56 print("Alpha\tBeta\tMSE")57 for i in range(0, 8):58 train = train_sets[i]59 tp = np.matrix.transpose(train)60 r = train_regs[i]61 l = np.linalg.eigvals(np.matmul(tp, train))62 m = 0 63 a = 164 b = 165 diff = 166 while (diff > .0000000001):67 s = np.linalg.inv(a * np.identity(len(train[0])) + b * np.matmul(tp, train))68 m = np.matrix.transpose(b * np.dot(np.matmul(s, tp), r))[0]69 c = np.sum(np.divide(b * l, (a + b * l)))70 newa = c / np.matmul(np.matrix.transpose(m), m)71 newb = 1.0/((1.0/(len(train) - c)) * np.sum(np.square(r - np.dot(m, tp))))72 diff = min(abs(a - newa), abs(b - newb))73 a = newa74 b = newb75 test_mse = np.mean(np.square(np.matmul(test_sets[i], m) - test_regs[i]))76 print(str(np.real(a)) + "\t" + str(np.real(b)) + "\t" + str(np.real(test_mse)))77 return78 79if __name__ == "__main__":80 train_sets = []81 train_sets.append(read_data("train-100-10.csv"))82 train_sets.append(read_data("train-100-100.csv")) 83 train_sets.append(read_data("train-1000-100.csv"))84 train_sets.append(read_data("train-forestfire.csv"))85 train_sets.append(read_data("train-realestate.csv"))86 train_sets.append(train_sets[2][:50])87 train_sets.append(train_sets[2][:100])88 train_sets.append(train_sets[2][:150])89 train_regs = []90 train_regs.append(read_data("trainR-100-10.csv"))91 train_regs.append(read_data("trainR-100-100.csv"))92 train_regs.append(read_data("trainR-1000-100.csv"))93 train_regs.append(read_data("trainR-forestfire.csv"))94 train_regs.append(read_data("trainR-realestate.csv"))95 train_regs.append(train_regs[2][:50])96 train_regs.append(train_regs[2][:100])97 train_regs.append(train_regs[2][:150])98 99 test_sets = []100 test_sets.append(read_data("test-100-10.csv"))101 test_sets.append(read_data("test-100-100.csv"))102 test_sets.append(read_data("test-1000-100.csv"))103 test_sets.append(read_data("test-forestfire.csv"))104 test_sets.append(read_data("test-realestate.csv"))105 test_sets.append(test_sets[2])106 test_sets.append(test_sets[2])107 test_sets.append(test_sets[2])108 test_regs = []109 test_regs.append(read_data("testR-100-10.csv"))110 test_regs.append(read_data("testR-100-100.csv"))111 test_regs.append(read_data("testR-1000-100.csv"))112 test_regs.append(read_data("testR-forestfire.csv"))113 test_regs.append(read_data("testR-realestate.csv"))114 test_regs.append(test_regs[2])115 test_regs.append(test_regs[2])116 test_regs.append(test_regs[2])117 task1(train_sets, train_regs, test_sets, test_regs)118 task2(train_sets, train_regs, test_sets, test_regs)...
constants.py
Source:constants.py
1from pathlib import Path2# Paths3PACKAGE_DIR = Path(__file__).resolve().parent.parent4RESOURCES_DIR = PACKAGE_DIR / "resources"5TOOLS_DIR = RESOURCES_DIR / "tools"6DATA_DIR = RESOURCES_DIR / "data"7STANFORD_CORENLP_DIR = TOOLS_DIR / "stanford-corenlp-full-2018-10-05"8UCCA_DIR = TOOLS_DIR / "ucca-bilstm-1.3.10"9UCCA_PARSER_PATH = UCCA_DIR / "models/ucca-bilstm"10TEST_SETS_PATHS = {11 ('asset_test', 'orig'): DATA_DIR / f'test_sets/asset/asset.test.orig',12 ('asset_test', 'refs'): [DATA_DIR / f'test_sets/asset/asset.test.simp.{i}' for i in range(10)],13 ('asset_valid', 'orig'): DATA_DIR / f'test_sets/asset/asset.valid.orig',14 ('asset_valid', 'refs'): [DATA_DIR / f'test_sets/asset/asset.valid.simp.{i}' for i in range(10)],15 ('turkcorpus_test', 'orig'): DATA_DIR / f'test_sets/turkcorpus/test.truecase.detok.orig',16 ('turkcorpus_test', 'refs'): [DATA_DIR / f'test_sets/turkcorpus/test.truecase.detok.simp.{i}' for i in range(8)],17 ('turkcorpus_valid', 'orig'): DATA_DIR / f'test_sets/turkcorpus/tune.truecase.detok.orig',18 ('turkcorpus_valid', 'refs'): [DATA_DIR / f'test_sets/turkcorpus/tune.truecase.detok.simp.{i}' for i in range(8)],19 ('turkcorpus_test_legacy', 'orig'): DATA_DIR / f'test_sets/turkcorpus/legacy/test.8turkers.tok.norm',20 ('turkcorpus_test_legacy', 'refs'): [21 DATA_DIR / f'test_sets/turkcorpus/legacy/test.8turkers.tok.turk.{i}' for i in range(8)22 ],23 ('turkcorpus_valid_legacy', 'orig'): DATA_DIR / f'test_sets/turkcorpus/legacy/tune.8turkers.tok.norm',24 ('turkcorpus_valid_legacy', 'refs'): [25 DATA_DIR / f'test_sets/turkcorpus/legacy/tune.8turkers.tok.turk.{i}' for i in range(8)26 ],27 ('pwkp_test', 'orig'): DATA_DIR / f'test_sets/pwkp/pwkp.test.orig',28 ('pwkp_test', 'refs'): [DATA_DIR / f'test_sets/pwkp/pwkp.test.simp'],29 ('pwkp_valid', 'orig'): DATA_DIR / f'test_sets/pwkp/pwkp.valid.orig',30 ('pwkp_valid', 'refs'): [DATA_DIR / f'test_sets/pwkp/pwkp.valid.simp'],31 ('hsplit_test', 'orig'): DATA_DIR / f'test_sets/hsplit/hsplit.tok.src',32 ('hsplit_test', 'refs'): [DATA_DIR / f'test_sets/hsplit/hsplit.tok.{i+1}' for i in range(4)],33 ('wikisplit_test', 'orig'): DATA_DIR / f'test_sets/wikisplit/wikisplit.test.untok.orig',34 ('wikisplit_test', 'refs'): [DATA_DIR / f'test_sets/wikisplit/wikisplit.test.untok.split'],35 ('wikisplit_valid', 'orig'): DATA_DIR / f'test_sets/wikisplit/wikisplit.valid.untok.orig',36 ('wikisplit_valid', 'refs'): [DATA_DIR / f'test_sets/wikisplit/wikisplit.valid.untok.split'],37 ('googlecomp_test', 'orig'): DATA_DIR / f'test_sets/googlecomp/googlecomp.test.orig',38 ('googlecomp_test', 'refs'): [DATA_DIR / f'test_sets/googlecomp/googlecomp.test.comp'],39 ('googlecomp_valid', 'orig'): DATA_DIR / f'test_sets/googlecomp/googlecomp.valid.orig',40 ('googlecomp_valid', 'refs'): [DATA_DIR / f'test_sets/googlecomp/googlecomp.valid.comp'],41 ('qats_test', 'orig'): DATA_DIR / f'test_sets/qats/qats.test.orig',42 ('qats_test', 'refs'): [DATA_DIR / f'test_sets/qats/qats.test.simp'],43}44SYSTEM_OUTPUTS_DIR = DATA_DIR / "system_outputs"45SYSTEM_OUTPUTS_DIRS_MAP = {46 "turkcorpus_test": SYSTEM_OUTPUTS_DIR / "turkcorpus/test",47 "turkcorpus_valid": SYSTEM_OUTPUTS_DIR / "turkcorpus/valid",48 "pwkp_test": SYSTEM_OUTPUTS_DIR / "pwkp/test",49}50# Constants51VALID_TEST_SETS = list(set([test_set for test_set, language in TEST_SETS_PATHS.keys()])) + ['custom']52VALID_METRICS = [53 'bleu',54 'sari',55 'samsa',56 'fkgl',57 'sent_bleu',58 'f1_token',59 'sari_legacy',60 'sari_by_operation',61 'bertscore',62]...
controversy.py
Source:controversy.py
1from cpath import data_path2from data_generator.tokenizer_b import FullTokenizerWarpper3from evaluation import *4from models.cnn_predictor import CNNPredictor5from models.controversy import *6def eval_all_contrv():7 ams_X, ams_Y = amsterdam.get_dev_data(False)8 clue_X, clue_Y = controversy.load_clueweb_testset()9 guardian_X, guardian_Y = controversy.load_guardian()10 models = []11 #models.append(("CNN/Wiki", CNNPredictor("WikiContrvCNN")))12 models.append(("CNN/Wiki", CNNPredictor("WikiContrvCNN_sigmoid", "WikiContrvCNN")))13 #models.append(("tlm/wiki", get_wiki_doc_lm()))14 #models.append(("Bert/Wiki", BertPredictor("WikiContrv2009")))15 #models.append(("Bert/Wiki", BertPredictor("WikiContrv2009_only_wiki")))16 #models.append(("tlm/dbpedia", get_dbpedia_contrv_lm()))17 #models.append(("tlm/Guardian", get_guardian16_lm()))18 #models.append(("yw_may", get_yw_may()))19 #models.append(("Guardian2", get_guardian_selective_lm()))20 test_sets = []21 #test_sets.append(("Ams18", [ams_X, ams_Y]))22 test_sets.append(("Clueweb" ,[clue_X, clue_Y]))23 #test_sets.append(("Guardian", [guardian_X, guardian_Y]))24 for set_name, test_set in test_sets:25 dev_X, dev_Y = test_set26 print(set_name)27 for name, model in models:28 scores = model.score(dev_X)29 auc = compute_pr_auc(scores, dev_Y)30 #auc = compute_auc(scores, dev_Y)31 acc = compute_opt_acc(scores, dev_Y)32 prec = compute_opt_prec(scores, dev_Y)33 recall = compute_opt_recall(scores, dev_Y)34 f1 = compute_opt_f1(scores, dev_Y)35 print("{0}\t{1:.03f}\t{2:.03f}\t{3:.03f}\t{4:.03f}\t{5:.03f}".format(name, auc, prec, recall, f1, acc))36def dataset_stat():37 ams_X, ams_Y = amsterdam.get_dev_data(False)38 clue_X, clue_Y = controversy.load_clueweb_testset()39 guardian_X, guardian_Y = controversy.load_guardian()40 vocab_size = 3052241 vocab_filename = "bert_voca.txt"42 voca_path = os.path.join(data_path, vocab_filename)43 encoder = FullTokenizerWarpper(voca_path)44 test_sets = []45 test_sets.append(("Ams18", [ams_X, ams_Y]))46 test_sets.append(("Clueweb" ,[clue_X, clue_Y]))47 test_sets.append(("Guardian", [guardian_X, guardian_Y]))48 for set_name, test_set in test_sets:49 dev_X, dev_Y = test_set50 num_over_size = 051 length_list = []52 for doc in dev_X:53 tokens = encoder.encode(doc)54 if len(tokens) > 200:55 num_over_size += 156 length_list.append(len(tokens))57 print("{0} {1:.03f} {2:.03f}".format(set_name, num_over_size / len(dev_X), average(length_list)))58if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!