Best Python code snippet using tempest_python
data_split.py
Source:data_split.py
1import os2import pandas as pd3import numpy as np4import argparse5import random67parser = argparse.ArgumentParser(description='data_split')8parser.add_argument('--data_path', default='./Amazon-office-raw/ratings.csv', type=str,9 help='datapath')10parser.add_argument('--save_path', default='./Amazon-office-raw', type=str,11 help='savepath')12parser.add_argument('--negative_sampling', default=True, type=bool,13 help='test negative sampling')14args = parser.parse_args()15random.seed(1)1617ratings=pd.read_csv(args.data_path)18type=['ratio-split','leave-one-out']19# indexing user20user_id=ratings[['userid']].drop_duplicates().reset_index(drop=True)21user_id['useridx']=pd.DataFrame(np.arange(len(user_id)))22userdict=dict(zip(user_id['userid'],user_id['useridx']))23ratings=ratings.replace({'userid':userdict})2425#indexing item26item_id=ratings[['itemid']].drop_duplicates().reset_index(drop=True)27item_id['itemidx']=pd.DataFrame(np.arange(len(item_id)))28itemdict=dict(zip(item_id['itemid'],item_id['itemidx']))29ratings=ratings.replace({'itemid':itemdict})3031# group items according to user index32items_set=set(item_id['itemidx'])33#ratings['rate'][ratings['rate']>0]=1.034rating1=ratings.groupby('userid')['itemid'].apply(list).reset_index()35rating2=ratings.groupby('userid')['timestamp'].apply(list).reset_index()3637# split38for split_type in type:39 ratings = pd.concat([rating1[['userid', 'itemid']], rating2['timestamp']], axis=1)40 if split_type=='leave-one-out':41 ratings['test_positive_index']=ratings['timestamp'].apply(lambda x: np.array(x).argmax())42 ratings['test_positive']=ratings.apply(lambda x: x['itemid'][x['test_positive_index']], axis=1)43 ratings['negative']=ratings['itemid'].apply(lambda x: list(items_set - set(x)))44 ratings['test_negative']=ratings['negative'].apply(lambda x: random.sample(x, 99))45 ratings['train_negative']=ratings.apply(lambda x: list(items_set - set(x['itemid']) - set(x['test_negative'])), axis=1)46 ratings.apply(lambda x: x['itemid'].remove(x['test_positive']), axis=1)474849 elif split_type=='ratio-split':50 ratings['test_positive']=ratings['itemid'].apply(lambda x: random.sample(x, round(len(x)*0.2)))51 ratings['itemid']=ratings.apply(lambda x: list(set(x['itemid'])-set(x['test_positive'])), axis=1)52 ratings['train_negative']=ratings.apply(lambda x: list(items_set - set(x['itemid'])),axis=1)53 if args.negative_sampling:54 ratings['test_negative']=ratings.apply(lambda x: random.sample(list(items_set - set(x['itemid'])- set(x['test_positive'])),len(x['test_positive']*10))55 if len(x['test_positive'])*10 <= len(list(items_set-set(x['itemid'])-set(x['test_positive']))) else list(items_set - set(x['itemid'])- set(x['test_positive'])), axis=1)5657 else :58 ratings['test_negative']=ratings.apply(lambda x: list(items_set - set(x['itemid'])- set(x['test_positive'])), axis=1)59 ratings['train_negative'] = ratings.apply(lambda x: list(set(x['train_negative'])-set(x['test_negative'])), axis=1)6061 ratings.rename(columns = {'itemid':'train_positive'}, inplace = True)62 ratings = ratings[['userid','train_positive','test_positive','train_negative', 'test_negative']].reset_index(drop=True)63 train_positive=ratings.join(ratings['train_positive'].apply(lambda x:pd.Series(x)).stack().reset_index(1,name='train_pos').drop('level_1',axis=1))64 test_positive=ratings.join(ratings['test_positive'].apply(lambda x:pd.Series(x)).stack().reset_index(1,name='test_pos').drop('level_1',axis=1))6566 # save67 if not os.path.exists(os.path.join(args.save_path, split_type)):68 os.makedirs(os.path.join(args.save_path, split_type))69 train_positive[['userid','train_pos']].reset_index(drop=True).to_feather(os.path.join(args.save_path,split_type, 'train_positive.ftr'))70 test_positive[['userid','test_pos']].reset_index(drop=True).to_feather(os.path.join(args.save_path,split_type, 'test_positive.ftr'))71 ratings[['userid','train_negative']].reset_index(drop=True).to_feather(os.path.join(args.save_path,split_type, 'train_negative.ftr'))72 ratings[['userid','test_negative']].reset_index(drop=True).to_feather(os.path.join(args.save_path,split_type, 'test_negative.ftr'))7374if not os.path.exists(os.path.join(args.save_path,'index-info')):75 os.makedirs(os.path.join(args.save_path,'index-info'))76user_id.to_csv(os.path.join(args.save_path,'index-info', 'user_index.csv'),index=False)
...
evaluation_random_classifier.py
Source:evaluation_random_classifier.py
1import json2import subprocess3import sys4import os5import time6from PIL import Image7from numpy import average, dot, linalg8import cv29import skimage.measure10import os11import subprocess12import random13import numpy14# give a list for psc2code video dealing time15# def getlist_psc2code():16# output = {}17# rightlist = []18# faultlist = []19# database = "data_copy/test"20# for category in os.listdir(database):21# # print(category) 22# if category == "non-screencast":23# for video in os.listdir(os.path.join(database,category)):24# rightlist.append(video)25# for img in os.listdir(os.path.join(database,category,video)):26# # if "frame" not in img:27# # print(img)28# output[video] = int(1.6*len(os.listdir(os.path.join(database,category,video))))29# else:30# for video in os.listdir(os.path.join(database,category)):31# faultlist.append(video)32# for img in os.listdir(os.path.join(database,category,video)):33# # if "frame" not in img:34# # print(img)35# output[video] = len(os.listdir(os.path.join(database,category,video)))36# return output,rightlist,faultlist37# psc2codelist,nonlist,yeslist = getlist_psc2code()38# count = 039# for video in psc2codelist.keys():40# psc2codelist[video] = int(psc2codelist[video]*1.88)41# count+= psc2codelist[video]42# print(len(nonlist))43# print(len(yeslist))44# # wrong one: MkJ1jY8ubKs45# # 2.5å46# def random1():47# count = 048# test_positive = random.sample(list(psc2codelist), k=13)49# test_negative = list(set(list(psc2codelist)).difference(set(test_positive)))50# # print(len(list(psc2codelist)))51# # print(len(test_positive))52# # print(len(test_negative))53# tp = 054# fp = 055# fn = 056# for item in test_positive:57# if item in yeslist:58# tp += 159# if item in nonlist:60# fp+=161# for item in test_negative:62# if item in yeslist:63# fn+=164# return tp,fp,fn65# allpre = []66# allrecall = []67# allfi = []68# for i in range(20):69# tp,fp,fn = random1()70# pre = float(tp)/(tp+fp)71# allpre.append(pre)72# recall = float(tp)/(tp+fn)73# allrecall.append(recall)74# allfi.append(2*recall*pre/(recall+pre))75# # print("%d,%d,%d"%(tp,fp,fn))76# print("the pre is %.3f, the recall is %.3f, the F-1 is %.3f"%(numpy.mean(allpre),numpy.mean(allrecall),numpy.mean(allfi))) 77def random1():78 count = 079 psc2codelist = [i for i in range(22)]80 print(psc2codelist)81 yeslist = [i for i in range(14)]82 nonlist = [i for i in range(14,22)]83 print(yeslist)84 print(nonlist)85 test_positive = random.sample(list(psc2codelist), k=11)86 test_negative = list(set(list(psc2codelist)).difference(set(test_positive)))87 # print(len(list(psc2codelist)))88 # print(len(test_positive))89 # print(len(test_negative))90 tp = 091 fp = 092 fn = 093 for item in test_positive:94 if item in yeslist:95 tp += 196 if item in nonlist:97 fp+=198 for item in test_negative:99 if item in yeslist:100 fn+=1101 return tp,fp,fn102allpre = []103allrecall = []104allfi = []105for i in range(20):106 tp,fp,fn = random1()107 pre = float(tp)/(tp+fp)108 allpre.append(pre)109 recall = float(tp)/(tp+fn)110 allrecall.append(recall)111 allfi.append(2*recall*pre/(recall+pre))112 # print("%d,%d,%d"%(tp,fp,fn))...
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!!