|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace NumSharp.UnitTest.RandomSampling |
| 8 | +{ |
| 9 | + [TestClass] |
| 10 | + public class NpRandomChoiceTests : TestClass |
| 11 | + { |
| 12 | + [TestMethod] |
| 13 | + public void UniformOneSample() |
| 14 | + { |
| 15 | + // Generate a uniform random sample from np.arange(5) of size 1: |
| 16 | + // This is equivalent to np.random.randint(0,5,1) |
| 17 | + int low = 0; |
| 18 | + int high = 5; |
| 19 | + int nrSamples = 1; |
| 20 | + |
| 21 | + NDArray actual = np.random.choice(high); // Not specifying size means 1 single value is wanted |
| 22 | + |
| 23 | + Assert.AreEqual(actual.len, nrSamples, "Unexpected number of elements"); |
| 24 | + |
| 25 | + // Verify that all elements in output are within the range |
| 26 | + for (int i = 0; i < actual.len; i++) |
| 27 | + { |
| 28 | + Assert.IsTrue(actual[i] >= low, "Element was less than expected"); |
| 29 | + Assert.IsTrue(actual[i] < high, "Element was greater than expected"); |
| 30 | + } |
| 31 | + } |
| 32 | + [TestMethod] |
| 33 | + public void UniformMultipleSample() |
| 34 | + { |
| 35 | + // Generate a uniform random sample from np.arange(5) of size 3: |
| 36 | + // This is equivalent to np.random.randint(0,5,3) |
| 37 | + int low = 0; |
| 38 | + int high = 5; |
| 39 | + int nrSamples = 3; |
| 40 | + |
| 41 | + NDArray actual = np.random.choice(high, nrSamples); |
| 42 | + |
| 43 | + Assert.AreEqual(actual.len, nrSamples, "Unexpected number of elements"); |
| 44 | + |
| 45 | + // Verify that all elements in output are within the range |
| 46 | + for (int i = 0; i < actual.len; i++) |
| 47 | + { |
| 48 | + Assert.IsTrue(actual[i] >= low, "Element was less than expected"); |
| 49 | + Assert.IsTrue(actual[i] < high, "Element was greater than expected"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public void NonUniformSample() |
| 55 | + { |
| 56 | + // Generate a non-uniform random sample from np.arange(5) of size 3: |
| 57 | + int low = 0; |
| 58 | + int high = 5; |
| 59 | + int nrSamples = 3; |
| 60 | + double[] probabilities = new double[] {0.1, 0, 0.3, 0.6, 0}; |
| 61 | + |
| 62 | + NDArray actual = np.random.choice(5, nrSamples, probabilities: probabilities); |
| 63 | + |
| 64 | + Assert.AreEqual(actual.len, nrSamples, "Unexpected number of elements"); |
| 65 | + |
| 66 | + // Verify that all elements in output are within the range |
| 67 | + for (int i = 0; i < actual.len; i++) |
| 68 | + { |
| 69 | + Assert.IsTrue(actual[i] >= low, "Element was less than expected"); |
| 70 | + Assert.IsTrue(actual[i] < high, "Element was greater than expected"); |
| 71 | + Assert.IsTrue(actual[i] != 1, "Sampled zero-probability element"); |
| 72 | + Assert.IsTrue(actual[i] != 4, "Sampled zero-probability element"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + [Ignore("Choice without replacement not implemented yet")] |
| 78 | + public void UniformSampleWithoutReplace() |
| 79 | + { |
| 80 | + NDArray actual = np.random.choice(5, 3, replace: false); |
| 81 | + Assert.Fail("Not implemented"); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + [Ignore("Choice without replacement not implemented yet")] |
| 86 | + public void NonUniformSampleWithoutReplace() |
| 87 | + { |
| 88 | + double[] probabilities = new double[] { 0.1, 0, 0.3, 0.6, 0 }; |
| 89 | + NDArray actual = np.random.choice(5, 3, replace: false, probabilities: probabilities); |
| 90 | + Assert.Fail("Not implemented"); |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + [Ignore("Choice with string arrays not implemented yet")] |
| 95 | + public void StringArraySample1() |
| 96 | + { |
| 97 | + int nrSamples = 5; |
| 98 | + |
| 99 | + NDArray aa_milne_arr = new string[] { "pooh", "rabbit", "piglet", "Christopher" }; |
| 100 | + double[] probabilities = new double[] { 0.5, 0.1, 0.0, 0.3 }; |
| 101 | + |
| 102 | + NDArray actual = np.random.choice(aa_milne_arr, nrSamples, probabilities: probabilities); |
| 103 | + |
| 104 | + Assert.AreEqual(actual.len, nrSamples, "Unexpected number of elements"); |
| 105 | + |
| 106 | + // Verify that all elements in output are within the possibilities |
| 107 | + for (int i = 0; i < actual.len; i++) |
| 108 | + { |
| 109 | + Assert.IsTrue((string)actual[i] != (string)aa_milne_arr[2], "Sampled zero-probability element"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void IntegerArraySample() |
| 115 | + { |
| 116 | + int nrSamples = 5; |
| 117 | + |
| 118 | + NDArray int_arr = new int[] { 42, 96, 3, 101 }; |
| 119 | + double[] probabilities = new double[] { 0.5, 0.1, 0.0, 0.3 }; |
| 120 | + |
| 121 | + NDArray actual = np.random.choice(int_arr, nrSamples, probabilities: probabilities); |
| 122 | + |
| 123 | + Assert.AreEqual(actual.len, nrSamples, "Unexpected number of elements"); |
| 124 | + |
| 125 | + // Verify that all elements in output are within the possibilities |
| 126 | + for (int i = 0; i < actual.len; i++) |
| 127 | + { |
| 128 | + Assert.IsTrue((int)actual[i] != (int)int_arr[2], "Sampled zero-probability element"); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments