Best Python code snippet using toolium_python
train-l2.py
Source:train-l2.py
1import pandas as pd2import numpy as np3import sys4from util.meta import full_split, cv1_split, cv1_split_time, test_split_time5from util import gen_prediction_name, gen_submission, score_sorted6from util.sklearn_model import SklearnModel7from util.keras_model import KerasModel8from util.xgb_model import XgbModel9from sklearn.model_selection import GroupKFold10from sklearn.linear_model import LogisticRegression11from sklearn.metrics import log_loss12from scipy.special import logit13preds = [14 #'20170114-2122-ffm2-f1b-0.68827',15 #'20170114-0106-ffm2-f1b-0.68775',16 #'20170113-1506-ffm2-f1-0.68447',17 #'20170113-1213-ffm2-p1-0.68392',18 '20170110-0230-ffm2-f1-0.69220',19 '20170110-1055-ffm2-f1-2-0.69214',20 '20170110-0124-ffm2-f1-0.69175',21 '20170109-1354-ffm2-f1-0.69148',22 '20170108-2008-ffm2-f1-0.68984',23 '20170107-2248-ffm2-p1-0.68876',24 '20170108-0345-ffm2-p2-0.68762',25 '20170106-2000-ffm2-p1-0.68754',26 '20170106-2050-ffm2-p2-0.68656',27 '20170105-2113-ffm2-p1-0.68684',28 '20161230-1323-ffm-p1-0.68204',29 '20161230-1049-ffm-p2-0.68169',30 '20161231-0544-vw-p1-0.67309',31 '20161231-1927-vw-p2-0.66718',32 '20170106-1339-vw-p1-0.67829',33 '20170109-1239-vw-p2-0.67148',34]35models = {36 'lr': lambda: SklearnModel(LogisticRegression(C=0.01)),37 'nn': lambda: KerasModel(batch_size=128, layers=[40, 10], dropouts=[0.3, 0.1], n_epoch=1),38 'xgb': lambda: XgbModel(n_iter=1500, silent=1, objective='binary:logistic', eval_metric='logloss', seed=144, max_depth=4, colsample_bytree=0.5, subsample=0.25, tree_method='exact', eta=0.05)39}40model_name = sys.argv[1]41model_factory = models[model_name]42def y_hash(y):43 return hash(tuple(np.where(y[:200])[0]) + tuple(np.where(y[-200:])[0]))44def fit_present_model(events, train_X, train_y, train_event):45 print "Training present model..."46 train_is_present = train_event.isin(events[events['timestamp'] < cv1_split_time].index).values47 present_train_X = train_X[train_is_present].values48 present_train_y = train_y[train_is_present].values49 present_train_g = train_event[train_is_present].values50 folds = list(GroupKFold(3).split(present_train_X, present_train_y, present_train_g))51 ll_scores = []52 map_scores = []53 for k, (idx_train, idx_test) in enumerate(folds):54 fold_train_X = present_train_X[idx_train]55 fold_train_y = present_train_y[idx_train]56 fold_train_g = present_train_g[idx_train]57 fold_val_X = present_train_X[idx_test]58 fold_val_y = present_train_y[idx_test]59 fold_val_g = present_train_g[idx_test]60 model = model_factory()61 model.fit(fold_train_X, fold_train_y, fold_train_g, fold_val_X, fold_val_y, fold_val_g)62 pred = model.predict(fold_val_X)63 ll_scores.append(log_loss(fold_val_y, pred, eps=1e-7))64 map_scores.append(score_sorted(fold_val_y, pred, fold_val_g))65 print " Fold %d logloss: %.7f, map score: %.7f" % (k+1, ll_scores[-1], map_scores[-1])66 print " Present map score: %.7f +- %.7f" % (np.mean(map_scores), np.std(map_scores))67 return model_factory().fit(present_train_X, present_train_y, fold_train_g), np.mean(map_scores)68def fit_future_model(events, train_X, train_y, train_event):69 print "Training future model..."70 val2_split_time = 107866777971 train_is_future_all = train_event.isin(events[events['timestamp'] >= cv1_split_time].index.values)72 train_is_future_train = train_event.isin(events[(events['timestamp'] >= cv1_split_time) & (events['timestamp'] < val2_split_time)].index.values)73 train_is_future_val = train_event.isin(events[(events['timestamp'] >= val2_split_time) & (events['timestamp'] < test_split_time)].index.values)74 future_train_X = train_X[train_is_future_train].values75 future_train_y = train_y[train_is_future_train].values76 future_train_g = train_event[train_is_future_train].values77 future_val_X = train_X[train_is_future_val].values78 future_val_y = train_y[train_is_future_val].values79 future_val_g = train_event[train_is_future_val].values80 model = model_factory()81 model.fit(future_train_X, future_train_y, future_train_g, future_val_X, future_val_y, future_val_g)82 pred = model.predict(future_val_X)83 ll_score = log_loss(future_val_y, pred, eps=1e-7)84 map_score = score_sorted(future_val_y, pred, future_val_g)85 print " Future logloss: %.7f, map score: %.7f" % (ll_score, map_score)86 future_all_X = train_X[train_is_future_all].values87 future_all_y = train_y[train_is_future_all].values88 future_all_g = train_event[train_is_future_all].values89 return model_factory().fit(future_all_X, future_all_y, future_all_g), map_score90def load_x(ds):91 if ds == 'train':92 feature_ds = 'cv1_test'93 pred_ds = 'cv1'94 elif ds == 'test':95 feature_ds = 'full_test'96 pred_ds = 'test'97 else:98 raise ValueError()99 X = []100 X.append((pd.read_csv('cache/leak_%s.csv.gz' % feature_ds, dtype=np.uint8) > 0).astype(np.uint8))101 for pi, p in enumerate(preds):102 X.append(logit(pd.read_csv('preds/%s-%s.csv.gz' % (p, pred_ds), dtype=np.float32)[['pred']].rename(columns={'pred': 'p%d' % pi}).clip(lower=1e-7, upper=1-1e-7)))103 return pd.concat(X, axis=1)104def load_train_data():105 print "Loading train data..."106 d = pd.read_csv(cv1_split[1], dtype=np.uint32, usecols=['display_id', 'clicked'])107 return load_x('train'), d['clicked'], d['display_id']108## Main part109print "Loading events..."110events = pd.read_csv("../input/events.csv.gz", dtype=np.int32, index_col=0, usecols=[0, 3]) # Load events111## Training models112train_data = load_train_data()113present_model, present_score = fit_present_model(events, *train_data)114future_model, future_score = fit_future_model(events, *train_data)115score = present_score * 0.47671335657020786 + future_score * 0.5232866434297921116print "Estimated score: %.7f" % score117del train_data118## Predicting119print "Predicting on test..."120print " Loading data..."121test_X = load_x('test').values122test_p = pd.read_csv(full_split[1], dtype=np.uint32)123test_p['pred'] = np.nan124test_is_present = test_p['display_id'].isin(events[events['timestamp'] < test_split_time].index).values125test_is_future = test_p['display_id'].isin(events[events['timestamp'] >= test_split_time].index).values126del events127print " Predicting..."128name = gen_prediction_name('l2-%s' % model_name, score)129test_p.loc[test_is_present, 'pred'] = present_model.predict(test_X[test_is_present])130test_p.loc[test_is_future, 'pred'] = future_model.predict(test_X[test_is_future])131test_p[['pred']].to_csv('preds/%s-test.csv.gz' % name, index=False, compression='gzip')132del test_X, test_is_future, test_is_present133print " Generating submission..."134subm = gen_submission(test_p)135subm.to_csv('subm/%s.csv.gz' % name, index=False, compression='gzip')136print " File name: %s" % name...
tests.py
Source:tests.py
...38 self.assertEqual(account.makerCommission, 1)39 self.assertEquals(account.balances.all().filter(asset='BNB').get().free, 1000)40 self.assertEquals(account.balances.all().filter(asset='ETH').get().free, 105)41class MainIndexViewTests(TestCase):42 def test_is_present(self):43 """44 Something responds at the index url45 """46 response = self.client.get(reverse('main:index'))47 self.assertEqual(response.status_code, 200)48class GetAccountViewTests(TestCase):49 def test_is_present(self):50 """51 Something responds at the index url52 """53 response = self.client.get(reverse('main:get_account'))...
SampleTest2.py
Source:SampleTest2.py
1# -*- coding:utf-8 -*-2__author__ = "Zeal Zhang/zealzhangz@gmail.com"3__version__ = "0.0.1"4import unittest5from selenium.webdriver.common.by import By6from selenium.webdriver.support import expected_conditions as EC7from CommonUtil.WebdriverUtil import ChromeWebdriver8from CommonUtil.setttings import Settings9class SampleTestCase2(unittest.TestCase, ChromeWebdriver):10 def __init__(self, *args, **kwargs):11 super(SampleTestCase2, self).__init__(*args, **kwargs)12 ChromeWebdriver.__init__(self)13 def setUp(self):14 # login15 self.login()16 def test_html_exist_string2(self):17 self.wait.until(EC.element_to_be_clickable((By.XPATH, Settings.START_PROJECT_XPATH))).click()18 test_is_present = self.wait.until(19 EC.text_to_be_present_in_element((By.XPATH, Settings.SUBHEAD_HEADING), "Create a new repository"))20 self.assertTrue(test_is_present)21 pass22 def tearDown(self):23 self.driver.close()24if __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!!