def test_permutations_length_of_output(self): # asserts the length of output based off input size assert len(permutations([])) == 0 assert len(permutations([1])) == 1 assert len(permutations([1, 2])) == 2 assert len(permutations([1, 2, 3])) == 6 assert len(permutations([1, 2, 3, 4])) == 24 assert len(permutations([1, 2, 3, 4, 5])) == 120 assert len(permutations([1, 2, 3, 4, 5, 6])) == 720 assert len(permutations([1, 2, 3, 4, 5, 6, 7])) == 5040
def test_generator_with_custom_start(self): results = [] for p in permutations(3, Permutation.from_list([1, 2, 0])): results.append(p) self.assertEqual(len(results), 3) self.assertEqual(results[0], Permutation.from_list([1, 2, 0])) self.assertEqual(results[1], Permutation.from_list([2, 0, 1])) self.assertEqual(results[2], Permutation.from_list([2, 1, 0]))
def test_generator_from_the_beginning(self): results = [] for p in permutations(3): results.append(p) self.assertEqual(len(results), 6) self.assertEqual(results[0], Permutation.from_list([0, 1, 2])) self.assertEqual(results[1], Permutation.from_list([0, 2, 1])) self.assertEqual(results[2], Permutation.from_list([1, 0, 2])) self.assertEqual(results[3], Permutation.from_list([1, 2, 0])) self.assertEqual(results[4], Permutation.from_list([2, 0, 1])) self.assertEqual(results[5], Permutation.from_list([2, 1, 0]))
def test_permutations_with_integer_inputs(self): # tests contains of outputs because order can be different assert permutations([]) == [] assert permutations([1]) == [1] self.assertCountEqual(permutations([1, 2]), self.perm_two) self.assertCountEqual(permutations([1, 2, 3]), self.perm_three) self.assertCountEqual(permutations([1, 2, 3, 4]), self.perm_four) self.assertCountEqual(permutations([1, 2, 3, 4, 5]), self.perm_five)
def permutationTest(X, y, feature_list, dataset): n_tests = 5 n_permutations = 100 scores = np.zeros(n_tests) pvalues = np.zeros(n_tests) permutation_scores = np.zeros(n_tests*n_permutations) for i in range(n_tests): # model = models.RF(feature_list) model = xgb.XGBClassifier() Xt = copy.deepcopy(X) yt = copy.deepcopy(y) rs = randint(1, 100) score, permutation_score, pvalue = permutations(model, Xt, yt, scoring='f1', cv=StratifiedKFold(n_splits=5, shuffle=True, random_state=rs), n_permuations=n_permutations) print("\nPermutation {}:\n".format(i+1)) print("p-value is {:.2f}".format(pvalue)) print("Score is {:.2f}".format(score)) scores[i] = score pvalues[i] = pvalue permutation_scores[n_permutations*i:n_permutations*(i+1)] = permutation_score p_mean = np.mean(pvalues) p_stdev = np.std(pvalues) s_mean = np.mean(scores) print("\nMean p-value is {:.2f}".format(p_mean)) print("Stdev p-value is {:.2f}".format(p_stdev)) print("Mean score is {:.2f}\n".format(s_mean)) r = np.amax(permutation_scores) - np.amin(permutation_scores) width = 0.03 print("Range is: {}".format(r)) plt.hist(permutation_scores, bins=ceil(r/width)) plt.axvline(x=s_mean, color='r', label='Average score value') plt.title("Histogram plot of permutation score values for " + dataset) plt.xlabel("permutation_scores") plt.ylabel("Frequency of permutation_scores") plt.legend() plt.savefig('Histogram_' + dataset + '_.png') plt.show()
def test_permutations_with_mixed_inputs(self): self.assertCountEqual(permutations([2, 1]), self.perm_two) self.assertCountEqual(permutations([2, 1, 3]), self.perm_three) self.assertCountEqual(permutations([4, 2, 1, 3]), self.perm_four) self.assertCountEqual(permutations([2, 5, 4, 3, 1]), self.perm_five)
def test_inverse(self): e = Permutation(4) for p in permutations(4): pinv = p.inverse() self.assertEqual(pinv * p, e) self.assertEqual(p * pinv, e)
def test_cycle_decomposition(self): for p in permutations(5): cycle_list = p.as_cycles() self.assertEqual(Permutation.from_cycles(cycle_list, 5), p)