How to use test_z1 method in hypothesis

Best Python code snippet using hypothesis

experiment.py

Source:experiment.py Github

copy

Full Screen

1import os2import pickle5 as pickle3import pandas as pd4import numpy as np5from sklearn.linear_model import LogisticRegression6from sklearn.ensemble import RandomForestClassifier7def preprocess(tp, dir_path):8 """9 :param dir_path: ~/VFAE10 :param tp: 0 -> german, 1 -> adult, 2 -> health11 :return: scores of logistic, random forest, random choice & accuracy on y12 """13 if tp == 0:14 data = 'german'15 sensitive = 'sex'16 target_dir = dir_path + '/dataset/german/'17 with open(os.path.join(target_dir, 'german_train.pkl'), 'rb') as f:18 train = pickle.load(f)19 with open(os.path.join(target_dir, 'german_test.pkl'), 'rb') as f:20 test = pickle.load(f)21 elif tp == 1:22 data = 'adult'23 sensitive = 'sex'24 target_dir = dir_path + '/dataset/adult/'25 with open(os.path.join(target_dir, 'adult_train.pkl'), 'rb') as f:26 train = pickle.load(f)27 with open(os.path.join(target_dir, 'adult_test.pkl'), 'rb') as f:28 test = pickle.load(f)29 else:30 data = 'health'31 target_dir = dir_path + '/dataset/health/'32 sensitive = 'age'33 with open(os.path.join(target_dir, 'health_train.pkl'), 'rb') as f:34 train = pickle.load(f)35 with open(os.path.join(target_dir, 'health_test.pkl'), 'rb') as f:36 test = pickle.load(f)37 train_z1_vae = np.load(os.path.join(dir_path + '/exp_%s/' % data, "%s_train_z1_vae.npy" % data))38 test_z1_vae = np.load(os.path.join(dir_path + '/exp_%s/' % data, "%s_test_z1_vae.npy" % data))39 train_z1_vfae = np.load(os.path.join(dir_path + '/exp_%s/' % data, "%s_train_z1_vfae.npy" % data))40 test_z1_vfae = np.load(os.path.join(dir_path + '/exp_%s/' % data, "%s_test_z1_vfae.npy" % data))41 train_s = train.pop(sensitive).astype('category')42 train_y = train.pop('label').astype('category')43 train_x = train.astype('category')44 test_s = test.pop(sensitive).astype('category')45 test_y = test.pop('label').astype('category')46 test_x = test.astype('category')47 train_z1 = [train_x, train_z1_vae, train_z1_vfae]48 test_z1 = [test_x, test_z1_vae, test_z1_vfae]49 return [train_z1, test_z1], [train_s, test_s], [train_y, test_y]50def calculate(z, s, y):51 [train_z1, test_z1] = z52 [train_s, test_s] = s53 [train_y, test_y] = y54 log_scores = []55 rf_scores = []56 log_y_scores = []57 disc_scores = []58 disc_prob_scores = []59 for i in range(3):60 # logistic regression & random forest61 log = LogisticRegression(random_state=1).fit(train_z1[i], train_s)62 log_scores.append(log.score(test_z1[i], test_s))63 rf = RandomForestClassifier(random_state=1).fit(train_z1[i], train_s)64 rf_scores.append(rf.score(test_z1[i], test_s))65 log_y = LogisticRegression(random_state=1).fit(train_z1[i], train_y)66 log_y_scores.append(log_y.score(test_z1[i], test_y))67 # discrimination on s68 pred = log_y.predict(test_z1[i])69 s_zero = test_s == 070 s_one = test_s == 171 disc_scores.append(np.abs(((pred[s_zero] == test_y[s_zero]).sum()) / s_zero.sum() -72 ((pred[s_one] == test_y[s_one]).sum()) / s_one.sum()))73 # discrimination prob on s74 pred_probs = log_y.predict_proba(test_z1[i])75 pred_prob = pd.Series(np.apply_along_axis(lambda x: x[0] if x[0] > x[1] else x[1], 1, pred_probs), dtype=float)76 disc_prob_scores.append(np.abs((pred_prob[s_zero].sum()) / s_zero.sum() -77 (pred_prob[s_one].sum()) / s_one.sum()))...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1#!/usr/bin/env python22# -*- coding: utf-8 -*-3"""4Created on Sun Apr 8 21:49:30 20185@author: abinaya6"""7import numpy as np8import matplotlib.pyplot as plt9import math10from scipy.stats import norm, uniform11def function_f(x):12 z = (3 - 0.8) * (1/ (1 + (np.sinh(2*x) * np.log(x))))13 return z14 15def function_p(x):16 z = uniform.pdf(x, loc=0.8, scale=3)17 return z18def function_g(x, mu, std):19 z = norm.pdf(x, loc=mu, scale=std)20 return z21print "MONTE CARLO ------------------------"22max_iterations = 5023monte_carlo_estimates = []24for i in range(0,max_iterations): 25 test_x1 = np.random.uniform(0.8,3,size=1000)26 #test_z1 = map(lambda t: function_f(t),test_x1)27 test_z1 = (3 - 0.8) * (1/ (1 + (np.sinh(2*test_x1) * np.log(test_x1))))28 monte_carlo_estimates.append(np.mean(test_z1))29 30print "Mean Integration Value- Simple Monte Carlo: ", np.mean(monte_carlo_estimates)31print "Variance Integration Value- Simple Monte Carlo: ", np.var(monte_carlo_estimates)32'''33test_x1 = np.linspace(0.8,3,1000)34test_z1 = (3 - 0.8) * (1/ (1 + (np.sinh(2*test_x1) * np.log(test_x1))))35plt.figure()36plt.plot(test_x1,test_z1)37plt.xlabel('Samples')38plt.ylabel('Function Value')39plt.title('Integration Function Plot - Stratification')40'''41print "\nMONTE CARLO USING STRATIFICATION ------------------------"42n=100043n1=70044n2=30045value1 = []46value2 = []47for i in range (0,50):48 X1_1 = np.random.uniform(0.8, 1.24, n1) 49 Fnc1_1 = (1.24-0.8) * pow((1 + (np.sinh(2*X1_1)*np.log(X1_1))),-1) 50 value1_1 = (np.sum(Fnc1_1))/ n151 52 X1_2 = np.random.uniform(1.24, 3, n2) 53 Fnc1_2 = (3-1.24)*pow((1 + (np.sinh(2*X1_2)*np.log(X1_2))),-1) 54 value1_2 = (np.sum(Fnc1_2))/ n255 56 value2.append(value1_1 + value1_2)57variance_stratified_Fnc1 = np.var(value2)58Mean_stratified_Fnc1 = np.mean(value2)59print "Mean Integration Value- Stratification: ", Mean_stratified_Fnc160print "Variance Integration Value- Stratification: ", variance_stratified_Fnc161'''62plt.scatter(X1_1,Fnc1_1, color='r')63plt.scatter(X1_2,Fnc1_2, color='g')64'''65print "\nMONTE CARLO USING IMPORTANCE SAMPLING ------------------------"66importance_sampling_estimates = []67mu = 0.568std = 0.769for i in range(0,max_iterations):70 test_x3 = np.random.normal(loc=mu, scale=std, size=1000)71 72 fx3 = (3 - 0.8) * (1/ (1 + (np.sinh(2*test_x3) * np.log(test_x3))))73 px3 = uniform.pdf(test_x3, loc=0.8, scale=3)74 gx3 = norm.pdf(test_x3, loc=mu, scale=std)75 76 z3 = (np.array(fx3)) * (np.array(px3) / np.array(gx3))77 z3 = z3[~np.isnan(z3)]78 importance_sampling_estimates.append(np.mean(z3))79 80print "Mean Integration Value- Importance sampling: ", np.mean(importance_sampling_estimates)...

Full Screen

Full Screen

test_given_reuse.py

Source:test_given_reuse.py Github

copy

Full Screen

...23 pass24given_named_booleans = given(z=st.text())25def test_fail_independently():26 @given_named_booleans27 def test_z1(z):28 assert False29 @given_named_booleans30 def test_z2(z):31 pass32 with pytest.raises(AssertionError):33 test_z1()...

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 hypothesis 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