How to use test_unique_list method in autotest

Best Python code snippet using autotest_python

BERT_CROSS_VALIDATION.py

Source:BERT_CROSS_VALIDATION.py Github

copy

Full Screen

1from simpletransformers.classification import ClassificationModel2from sklearn.model_selection import StratifiedKFold3from sklearn.metrics import accuracy_score4import pandas as pd5import re6import os 7import matplotlib.pyplot as plt8import seaborn as sns9import numpy as np10from transformers import AutoModel, AutoTokenizer,AutoModelForSequenceClassification,pipeline11from sklearn.metrics import accuracy_score, classification_report, confusion_matrix12def replace_with_index(x):13 index = unique_list.index(x)14 return index15def cleanText(input_sentence):16 17 tmp= [word.replace('A','a') for word in input_sentence.split(' ')]18 tmp= [word.lower() for word in tmp]19 tmp= [word.replace('i̇','i') for word in tmp]20 tmp = [re.sub('[^A-Za-z0-9ğüşıçöiâî]+', ' ', word) for word in tmp]21 tmp = [word.strip(' ') for word in tmp]22 tmp1 =' '.join(tmp)23 return tmp124def get_model(path,X_test,y_test):25 result = {}26 plt.rcParams["figure.figsize"] = (50,30)27 for epoch in path:28 tokenizer= AutoTokenizer.from_pretrained(path)29 # build and load model, it take time depending on your internet connection30 model= AutoModelForSequenceClassification.from_pretrained(path)31 # make pipeline32 nlp=pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)33 34 test = pd.concat([X_test,y_test],axis=1)35 #print(test)36 test_unique_list = test['label'].unique().tolist()37 test_unique_list.sort()38 #print(test_unique_list)39 pred_label_list, true_label_list = [],[]40 cou = 041 for t,l in zip(test["metin"], test["label"]):42 cou+=143 true_label_list.append(l)44 pred_label_list.append(int(nlp(t)[0]["label"].lstrip("LABEL_")))45 if cou %1000 == 0:46 print(path.index(epoch),cou)47 df = pd.DataFrame()48 df["true"] = true_label_list49 df["pred"] = pred_label_list50 print(df)51 for item in test_unique_list:52 true_df = df[df.true == item]53 class_acc = accuracy_score(true_df["true"], true_df["pred"])54 print('ID = {} ACC = {}'.format(item, class_acc),file=open("output.txt", "a"))55 56 acc = accuracy_score(df["true"], df["pred"])57 result[f"{path.index(epoch)+1}. epoch Accuracy"] = acc58 print('acc = {}'.format(acc),file=open("output.txt", "a"))59 print(classification_report(df["true"], df["pred"]),file=open("output.txt", "a"))60 """sns.heatmap(confusion_matrix(df["true"], df["pred"]), annot = True, fmt = "g")61 plt.show()"""62 return df63HUGGINGFACE_MODEL_PATH = "loodos/bert-base-turkish-uncased"64tokenizer = AutoTokenizer.from_pretrained(HUGGINGFACE_MODEL_PATH)65print('Zero_Shotsız_ve_Zero_Shotlı_BERT_Result',file=open("output.txt", "a"))66list_url = [['Single_BERT_DATASET_PATH','No_ZEROSHOT'],['FİLTERED_BY_ZEROSHOT_RESULT_DATASET_PATH','W_ZEROSHOT']]67 68for item in list_url:69 print(item[1],file=open("output.txt", "a"))70 data = pd.read_csv(item[0])71 unique_list = data['label'].unique().tolist()72 data['label'] = data['label'].apply(lambda x: replace_with_index(x))73 data = data.iloc[:,:2]74 for i in range(len(data['label'])):75 if isinstance(data['label'][i], np.generic):76 data['label'][i]= np.asscalar(data['label'][i])77 data.dropna(axis= 0, inplace=True)78 data = data.reset_index(drop = True)79 data["metin"] = data["metin"].apply(cleanText)80 print(len(data))81 data = data.drop_duplicates()82 print(len(data))83 data=data.sample(frac=1)84 data = data.reset_index(drop= True)85 # prepare cross validation86 n=587 kf = StratifiedKFold(n_splits=n, random_state=42, shuffle=True)88 no_labels = len(set(data["label"]))89 results = []90 for i,(train_index, val_index) in enumerate(kf.split(data["metin"],data["label"])):91 print('Kesit No: {}'.format(i),file=open("output.txt", "a"))92 # splitting Dataframe (dataset not included)93 X_train, X_test = data["metin"][train_index], data["metin"][val_index]94 y_train, y_test = data["label"][train_index], data["label"][val_index]95 96 train_df = pd.concat([X_train,y_train], axis = 1)97 val_df = pd.concat([X_test,y_test], axis = 1)98 # Defining Model99 MODEL_OUTPUT_DIR = 'BERT_{}_44_kategori_{}/'.format(item[1],i,)100 #model = AutoModel.from_pretrained(HUGGINGFACE_MODEL_PATH)101 model_args = {102 "use_early_stopping": True,103 "early_stopping_delta": 0.01,104 "early_stopping_metric": "mcc",105 "early_stopping_metric_minimize": False,106 "early_stopping_patience": 5,107 "evaluate_during_training_steps": 6000,108 "fp16": False,109 "num_train_epochs":3110 #"overwrite_output_dir": True111 }112 model = ClassificationModel(113 "bert", 114 HUGGINGFACE_MODEL_PATH,115 use_cuda=True, 116 args=model_args, 117 num_labels=no_labels118 )119 model.train_model(train_df, acc=accuracy_score, output_dir=MODEL_OUTPUT_DIR)120 EPOCH_PATH = [MODEL_OUTPUT_DIR +"/"+ t for t in os.listdir(MODEL_OUTPUT_DIR) if "epoch" in t and "-3"in t]121 EPOCH_PATH=EPOCH_PATH[0]122 data_test = pd.DataFrame()...

Full Screen

Full Screen

Single_BERT.py

Source:Single_BERT.py Github

copy

Full Screen

1from sklearn.model_selection import train_test_split2import sklearn3import os4from sklearn.metrics import accuracy_score, classification_report, confusion_matrix5import pandas as pd6import numpy as np7import matplotlib.pyplot as plt8import seaborn as sns9from transformers import AutoModel, AutoTokenizer,AutoModelForSequenceClassification,pipeline10import re11from simpletransformers.classification import ClassificationModel12data = pd.read_csv('data set path according to which method you will use ')13HUGGINGFACE_MODEL_PATH = "loodos/bert-base-turkish-uncased"14MODEL_OUTPUT_DIR = 'BERT4_no_zero_shot_44/'15#data = data[data.groupby('kategori')['kategori'].transform('size') > 200]16#data = data.reset_index(drop = True)17unique_list = data['label'].unique().tolist()18def replace_with_index(x):19 index = unique_list.index(x)20 return index21data['label'] = data['label'].apply(lambda x: replace_with_index(x))22data = data.iloc[:,:2]23for i in range(len(data['label'])):24 if isinstance(data['label'][i], np.generic):25 data['label'][i]= np.asscalar(data['label'][i])26data.dropna(axis= 0, inplace=True)27data = data.reset_index(drop = True)28def cleanText(input_sentence):29 30 tmp= [word.replace('A','a') for word in input_sentence.split(' ')]31 tmp= [word.lower() for word in tmp]32 tmp= [word.replace('i̇','i') for word in tmp]33 tmp = [re.sub('[^A-Za-z0-9ğüşıçöiâî]+', ' ', word) for word in tmp]34 tmp = [word.strip(' ') for word in tmp]35 tmp1 =' '.join(tmp)36 return tmp137data["metin"] = data["metin"].apply(cleanText)38print(len(data))39data = data.drop_duplicates()40print(len(data))41data=data.sample(frac=1)42data = data.reset_index(drop= True)43#print(data['label'].unique())44X_train, X_test, y_train, y_test = train_test_split(data["metin"],data["label"], test_size= .2, stratify=data['label'], random_state = 42)45train = pd.concat([X_train,y_train], axis = 1)46tokenizer = AutoTokenizer.from_pretrained(HUGGINGFACE_MODEL_PATH)47model = AutoModel.from_pretrained(HUGGINGFACE_MODEL_PATH)48no_labels = len(set(data["label"]))49model_args = {50 "use_early_stopping": True,51 "early_stopping_delta": 0.01,52 "early_stopping_metric": "mcc",53 "early_stopping_metric_minimize": False,54 "early_stopping_patience": 5,55 "evaluate_during_training_steps": 6000,56 "fp16": False,57 "num_train_epochs":358}59model = ClassificationModel(60 "bert", 61 HUGGINGFACE_MODEL_PATH,62 use_cuda=True, 63 args=model_args, 64 num_labels=no_labels65)66model.train_model(train, acc=sklearn.metrics.accuracy_score, output_dir=MODEL_OUTPUT_DIR)67EPOCH_PATH = [MODEL_OUTPUT_DIR +"/"+ i for i in os.listdir(MODEL_OUTPUT_DIR) if "epoch" in i and "-3"in i]68EPOCH_PATH=EPOCH_PATH[0]69def get_model(path):70 result = {}71 plt.rcParams["figure.figsize"] = (50,30)72 for epoch in path:73 tokenizer= AutoTokenizer.from_pretrained(EPOCH_PATH)74 # build and load model, it take time depending on your internet connection75 model= AutoModelForSequenceClassification.from_pretrained(EPOCH_PATH)76 # make pipeline77 nlp=pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)78 79 test = pd.concat([X_test,y_test],axis=1)80 print(test)81 #test_unique_list = test['label'].unique().tolist()82 #test_unique_list.sort()83 #print(test_unique_list)84 pred_label_list, true_label_list = [],[]85 cou = 086 for t,l in zip(test["metin"], test["label"]):87 cou+=188 true_label_list.append(l)89 pred_label_list.append(int(nlp(t)[0]["label"].lstrip("LABEL_")))90 if cou %1000 == 0:91 print(EPOCH_PATH.index(epoch),cou)92 df = pd.DataFrame()93 df["true"] = true_label_list94 df["pred"] = pred_label_list95 print(df)96 """for item in test_unique_list:97 true_df = df[df.true == item]98 class_acc = accuracy_score(true_df["true"], true_df["pred"])99 print('ID = {} ACC = {}'.format(item, class_acc))"""100 101 acc = accuracy_score(df["true"], df["pred"])102 result[f"{EPOCH_PATH.index(epoch)+1}. epoch Accuracy"] = acc103 print(acc)104 print(classification_report(df["true"], df["pred"]))105 sns.heatmap(confusion_matrix(df["true"], df["pred"]), annot = True, fmt = "g")106 plt.show()107 return df108data_test = pd.DataFrame()...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

1import unittest2import numpy as np3from radbm.utils import unique_list, Ramp4class Test_unique_list(unittest.TestCase):5 def test_unique_list(self):6 it = [3,4,4,2,1,1,0,3,2]7 expected_list = [3,4,2,1,0]8 result = unique_list(it)9 self.assertEqual(result, expected_list)10 11class TestRamp(unittest.TestCase):12 def test_ramp(self):13 #from x=3..5 we ramp from y=-1..714 ramp = Ramp(3, 5, -1, 7)15 xs = np.array([2, 3, 4, 5, 6])16 ys = np.array([-1, -1, 3, 7, 7])...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful