How to use test_set_target method in avocado

Best Python code snippet using avocado_python

test_optimizer.py

Source:test_optimizer.py Github

copy

Full Screen

1import numpy as np2import unittest3from icet.fitting import Optimizer4class TestOptimizer(unittest.TestCase):5 """Unittest class for Optimizer."""6 def __init__(self, *args, **kwargs):7 super().__init__(*args, **kwargs)8 self.n_rows = 2009 self.n_cols = 5010 self.tol = 1.0 / self.n_rows11 # set up dummy linear problem data12 self.A = np.random.normal(0, 1, (self.n_rows, self.n_cols))13 self.x = np.random.normal(0, 5, (self.n_cols, ))14 self.noise = np.random.normal(0, 0.1, (self.n_rows, ))15 self.y = np.dot(self.A, self.x) + self.noise16 def shortDescription(self):17 """Prevents unittest from printing docstring in test cases."""18 return None19 def test_get_rows_via_sizes(self):20 """Tests _get_rows_via_sizes functionality."""21 opt = Optimizer((self.A, self.y))22 # test with only train_size defined23 train_size, test_size = int(0.8 * self.n_rows), None24 train_set, test_set = opt._get_rows_via_sizes(train_size, test_size)25 self.assertEqual(train_size, len(train_set))26 self.assertEqual(self.n_rows - train_size, len(test_set))27 # test with only test_size defined28 train_size, test_size = None, int(0.8 * self.n_rows)29 train_set, test_set = opt._get_rows_via_sizes(train_size, test_size)30 self.assertEqual(test_size, len(test_set))31 self.assertEqual(self.n_rows - test_size, len(train_set))32 # test with both defined33 train_size, test_size = int(0.8 * self.n_rows), int(0.15 * self.n_rows)34 train_set, test_set = opt._get_rows_via_sizes(train_size, test_size)35 self.assertEqual(train_size, len(train_set))36 self.assertEqual(test_size, len(test_set))37 # test with fractions38 train_size, test_size = 0.7, 0.239 train_set, test_set = opt._get_rows_via_sizes(train_size, test_size)40 self.assertLess(abs(train_size*self.n_rows - len(train_set)), self.tol)41 self.assertLess(abs(test_size*self.n_rows - len(test_set)), self.tol)42 # test edge case with full training set43 test_size = None44 for train_size in [1.0, self.n_rows]:45 train_set, test_set = opt._get_rows_via_sizes(46 train_size, test_size)47 self.assertEqual(len(train_set), self.n_rows)48 self.assertIsNone(test_set)49 # test invalid sizes50 with self.assertRaises(ValueError):51 train_size, test_size = None, 1.052 opt._get_rows_via_sizes(train_size, test_size)53 with self.assertRaises(ValueError):54 train_size, test_size = None, None55 opt._get_rows_via_sizes(train_size, test_size)56 def test_get_rows_from_indices(self):57 """Tests _get_rows_from_indices."""58 opt = Optimizer((self.A, self.y))59 all_rows = np.arange(self.n_rows)60 train_size = int(0.8 * self.n_rows)61 train_set_target = np.random.choice(62 all_rows, train_size, replace=False)63 test_set_target = sorted(np.setdiff1d(all_rows, train_set_target))64 # specify only train_set, test set should default to remaining rows65 train_set, test_set = opt._get_rows_from_indices(66 train_set_target, None)67 self.assertSequenceEqual(sorted(train_set_target), sorted(train_set))68 self.assertSequenceEqual(sorted(test_set_target), sorted(test_set))69 # specify only test_set, train set should default to remaining rows70 train_set, test_set = opt._get_rows_from_indices(None, test_set_target)71 self.assertSequenceEqual(sorted(train_set_target), sorted(train_set))72 self.assertSequenceEqual(sorted(test_set_target), sorted(test_set))73 # specify partial sets meaning not all rows are used74 train_set_target = np.delete(train_set_target, [0, 1, 2])75 test_set_target = np.delete(test_set_target, [0, 1, 2])76 train_set, test_set = opt._get_rows_from_indices(77 train_set_target, test_set_target)78 self.assertSequenceEqual(sorted(train_set_target), sorted(train_set))79 self.assertSequenceEqual(sorted(test_set_target), sorted(test_set))80 # test invalid input81 with self.assertRaises(ValueError):82 opt._get_rows_from_indices(None, None)83 def test_setup_rows(self):84 """85 Tests _setup_rows.86 Simply test that function raise when no training data available87 """88 opt = Optimizer((self.A, self.y))89 # no training data from train_size90 with self.assertRaises(ValueError):91 train_size, test_size = 0, 0.592 opt._setup_rows(train_size, test_size, None, None)93 # no training data from train_set94 with self.assertRaises(ValueError):95 train_set, test_set = [], np.arange(0, int(0.5*self.n_rows))96 opt._setup_rows(None, None, train_set, test_set)97 # overlapping indices in train_set and test_set98 with self.assertRaises(ValueError):99 train_set, test_set = [1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 10]100 opt._setup_rows(None, None, train_set, test_set)101 def test_train(self):102 """Tests train."""103 # with test set104 train_size = 0.75105 opt = Optimizer((self.A, self.y), train_size=train_size)106 self.assertIsNone(opt._rmse_train)107 self.assertIsNone(opt._rmse_test)108 self.assertIsNone(opt.train_scatter_data)109 self.assertIsNone(opt.test_scatter_data)110 opt.train()111 self.assertIsNotNone(opt._rmse_train)112 self.assertIsNotNone(opt._rmse_test)113 self.assertIsNotNone(opt.train_scatter_data)114 self.assertIsNotNone(opt.test_scatter_data)115 # without testing116 train_size = 1.0117 opt = Optimizer((self.A, self.y), train_size=train_size)118 self.assertIsNone(opt._rmse_train)119 self.assertIsNone(opt._rmse_test)120 self.assertIsNone(opt.train_scatter_data)121 self.assertIsNone(opt.test_scatter_data)122 opt.train()123 self.assertIsNotNone(opt._rmse_train)124 self.assertIsNone(opt._rmse_test)125 self.assertIsNotNone(opt.train_scatter_data)126 self.assertIsNone(opt.test_scatter_data)127 def test_summary_property(self):128 """Tests summary property."""129 # without having trained130 opt = Optimizer((self.A, self.y))131 self.assertIsInstance(opt.summary, dict)132 # with having trained133 opt.train()134 self.assertIsInstance(opt.summary, dict)135 self.assertIn('rmse_train', opt.summary.keys())136 self.assertIn('rmse_test', opt.summary.keys())137 def test_repr(self):138 """Tests repr dunder."""139 opt = Optimizer((self.A, self.y))140 self.assertIsInstance(repr(opt), str)141 def test_size_properties(self):142 """Tests the properties in regards to training/test sets and sizes."""143 # test without test_set144 train_set = np.arange(0, self.n_rows)145 opt = Optimizer((self.A, self.y), train_set=train_set)146 self.assertSequenceEqual(opt.train_set.tolist(), train_set.tolist())147 self.assertEqual(len(train_set), opt.train_size)148 self.assertEqual(1.0, opt.train_fraction)149 self.assertIsNone(opt.test_set)150 self.assertEqual(opt.test_size, 0)151 self.assertEqual(opt.test_fraction, 0)152 # test with test set153 test_set = np.arange(int(0.7 * self.n_rows), int(0.8 * self.n_rows))154 opt = Optimizer((self.A, self.y), test_set=test_set)155 self.assertSequenceEqual(test_set.tolist(), opt.test_set.tolist())156 self.assertEqual(opt.test_size, len(test_set))157 self.assertAlmostEqual(opt.test_fraction, len(test_set) / self.n_rows)158 def test_zero_error_with_least_square_fit(self):159 """ Test that the error is zero if training without noise and with160 least-squares. """161 # set up dummy linear problem data162 for standardize in [True, False]:163 y = np.dot(self.A, self.x)164 opt = Optimizer((self.A, y), fit_method='least-squares', standardize=standardize)165 opt.train()166 self.assertAlmostEqual(opt.rmse_train, 0.0)167 self.assertAlmostEqual(opt.rmse_test, 0.0)168 self.assertAlmostEqual(np.abs(self.x - opt.parameters).max(), 0)169if __name__ == '__main__':...

Full Screen

Full Screen

week11.py

Source:week11.py Github

copy

Full Screen

1from __future__ import division2import hashlib3import json4from sklearn.ensemble import RandomForestClassifier as rfc5directory = "data/"6def bag_of_words(lines):7 uniq = set()8 for line in lines:9 # Iterate over all words10 for word in line[0].split():11 # Put words not already found into dictionary12 uniq.add(word)13 uniq_dict = {}14 for j, word in enumerate(uniq):15 uniq_dict[word] = j16 uniq_len = len(uniq)17 # Use word dictionary to create bag-of-words18 bag = []19 for line in lines:20 vec = [0] * uniq_len21 for word in line[0].split():22 vec[uniq_dict[word]] += 123 bag.append((vec, (1 if 'earn' in line[1] else 0)))24 return bag25def bag_of_words_feature_hashed(lines, N):26 uniq = set()27 for line in lines:28 # Iterate over all words29 for word in line[0].split():30 # Put words not already found into dictionary31 uniq.add(word)32 uniq_dict = {}33 for j, word in enumerate(uniq):34 uniq_dict[word] = j35 # Use word dictionary to create bag-of-words36 bag = []37 for line in lines:38 vec = [0] * N39 for word in line[0].split():40 h = int(hashlib.md5(word).hexdigest(), 16) % N41 vec[h] += 142 bag.append((vec, (1 if 'earn' in line[1] else 0)))43 return bag44if __name__ == "__main__":45 articles = []46 for i in range(21):47 f = open(directory + "reuters-0" + (str(i) if i >= 10 else "0" + str(i)) + ".json", 'r')48 articles += json.loads(f.read())49 f.close()50 articles = reduce(lambda x, y: x + ([y] if ('topics' in y.keys())51 and ('body' in y.keys())52 and (len(y['topics']) > 0)53 and (len(y['body']) > 0)54 else []), articles, [])55 lines = map(lambda x: (x['body'].lower().encode('ascii', errors='ignore'), x['topics']), articles)56 print "Calcualting bag of words."57 bow = bag_of_words(lines)58 print "Amount of lines: " + str(len(bow))59 print "Amount of words: " + str(len(bow[0][0]))60 training_set = bow[:int(round(len(bow)*0.8))]61 training_set_data = [row[0] for row in training_set]62 training_set_target = [row[1] for row in training_set]63 test_set = bow[-int(round(len(bow)*0.2)):]64 test_set_data = [row[0] for row in test_set]65 test_set_target = [row[1] for row in test_set]66 classifier = rfc(n_estimators=50)67 classifier.fit(training_set_data, training_set_target)68 predictions = classifier.predict(test_set_data)69 accuracy = []70 for i, prediction in enumerate(predictions):71 accuracy += [test_set_target[i] == prediction]72 accuracy = (accuracy.count(True) / len(test_set_target)) * 10073 print "Accuracy of classifier: " + str(accuracy) + "%"74 print "\nCalculating bag of words using feature hashing."75 bow = bag_of_words_feature_hashed(lines, 1000)76 print "Amount of lines: " + str(len(bow))77 print "Amount of words: " + str(len(bow[0][0]))78 training_set = bow[:int(round(len(bow)*0.8))]79 training_set_data = [row[0] for row in training_set]80 training_set_target = [row[1] for row in training_set]81 test_set = bow[-int(round(len(bow)*0.2)):]82 test_set_data = [row[0] for row in test_set]83 test_set_target = [row[1] for row in test_set]84 classifier = rfc(n_estimators=50)85 classifier.fit(training_set_data, training_set_target)86 predictions = classifier.predict(test_set_data)87 accuracy = []88 for i, prediction in enumerate(predictions):89 accuracy += [test_set_target[i] == prediction]90 accuracy = (accuracy.count(True) / len(test_set_target)) * 100...

Full Screen

Full Screen

evaluate_digit_classifier.py

Source:evaluate_digit_classifier.py Github

copy

Full Screen

1import numpy as np2from sklearn import datasets3# initialize test set4def load_digits_and_test_set(test_start=0, test_count=1000):5 global digits6 global t_start, t_count, t_end7 global test_set_images, test_set_target8 digits = datasets.load_digits()9 t_images = len(digits.images)10 t_start = test_start11 t_count = test_count12 t_end = t_start + t_count13 if (t_images < t_end):14 t_end = t_images15 test_set_images = digits.images[t_start:t_end]16 test_set_target = digits.target[t_start:t_end]17 return len(test_set_images)18# test classifier over test set19def test_digit_classifier(classifier):20 digits = datasets.load_digits()21 correct = 022 print(f'test classifier for {t_count} images: {t_start} to {t_end-1}.')23 for img, target in zip(test_set_images, test_set_target):24 v = np.matrix.flatten(img) / 15.25 output = classifier(v)26 answer = list(output).index(max(output))27 if answer == target:28 correct += 129 return (correct/t_count)30# calculate total cost for classifier over test set31def y_vec(digit):32 return np.array([1 if i == digit else 0 for i in range(0,10)])33def cost_one(classifier,x,i):34 return sum([(classifier(x)[j] - y_vec(i)[j])**2 for j in range(10)])35def calculate_total_cost(classifier):36 digits = datasets.load_digits()37 print(f'calculate total cost for {t_count} images: {t_start} to {t_end-1}.')38 x = np.array([np.matrix.flatten(img) for img in test_set_images]) / 15.039 y = test_set_target...

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