Example #1
0
        public void test_randbool_UC_1()
        {
            var random = new np.random(new UserDefinedRandomGenerator());

            ndarray arr = random.randint(2, 3, new shape(4), dtype: np.Bool);

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BOOL);
            AssertShape(arr, 4);
            print(arr);
            //AssertArray(arr, new bool[] { false, false, false, false });

            arr = random.randint(20, null, new shape(4, 5), dtype: np.Bool);
            AssertShape(arr, 4, 5);
            print(arr);

            arr = random.randint(20, 21, new shape(2, 3), dtype: np.Bool);
            AssertShape(arr, 2, 3);
            print(arr);
            //AssertArray(arr, new SByte[,] { { 20, 20, 20 }, { 20, 20, 20 } });

            arr = random.randint(-2, 3, new shape(5000000), dtype: np.Bool);
            print(np.amax(arr));
            print(np.amin(arr));
            print(np.average(arr));
        }
Example #2
0
        public void test_randint8_UD_1()
        {
            var random = new np.random(new UserDefinedRandomGenerator());

            random.seed(9292);

            ndarray arr = random.randint(2, 3, new shape(4), dtype: np.Int8);

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE);
            AssertShape(arr, 4);
            print(arr);
            AssertArray(arr, new SByte[] { 2, 2, 2, 2 });

            arr = random.randint(2, 8, new shape(5000000), dtype: np.Int8);
            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE);

            var amax = np.amax(arr);

            print(amax);
            Assert.AreEqual((sbyte)7, (sbyte)amax);

            var amin = np.amin(arr);

            print(amin);
            Assert.AreEqual((sbyte)2, (sbyte)amin);

            var avg = np.average(arr);

            print(avg);
            Assert.AreEqual(2.4605506, (double)avg);

            var first10 = arr["0:10:1"] as ndarray;

            print(first10);
            AssertArray(first10, new sbyte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 });


            arr = random.randint(-2, 3, new shape(5000000), dtype: np.Int8);
            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE);

            amax = np.amax(arr);
            print(amax);
            Assert.AreEqual((sbyte)2, (sbyte)amax);

            amin = np.amin(arr);
            print(amin);
            Assert.AreEqual((sbyte)(-2), (sbyte)amin);

            avg = np.average(arr);
            print(avg);
            Assert.AreEqual(-1.6648296, (double)avg);

            first10 = arr["0:10:1"] as ndarray;
            print(first10);
            AssertArray(first10, new sbyte[] { -2, -2, 0, -1, -2, -2, -2, -2, -2, -2 });
        }
        public void test_histogramdd_3()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(300000));

            var weights = np.arange(300000 / 4, dtype: np.Float64);

            weights.fill(0.5);

            System.Tuple <int, int>[] range1 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            var x            = np.histogramdd(r.reshape(-1, 4), bins: new int[] { 2, 2, 2, 2 }, range: range1, weights: weights);
            var ExpectedHist = new double[, , , ]
            {
                { { { 149.5, 176.0 }, { 187.5, 223.5 } }, { { 171.0, 227.0 }, { 194.5, 259.5 } } },
                { { { 188.5, 186.0 }, { 217.5, 267.5 } }, { { 223.0, 248.5 }, { 258.0, 312.5 } } }
            };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[1], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[2], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[3], new double[] { 15, 20, 25 });
            print(x.hist);
            print(x.bin_edges);
        }
        public void test_histogramdd_4()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(300000));

            System.Tuple <int, int>[] range1 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            var x            = np.histogramdd(r.reshape(-1, 4), bins: 3, range: range1);
            var ExpectedHist = new double[, , , ]
            {
                { { { 131.0, 84.0, 112.0 }, { 100.0, 71.0, 95.0 }, { 125.0, 90.0, 127.0 } }, { { 79.0, 70.0, 97.0 }, { 69.0, 59.0, 79.0 }, { 80.0, 84.0, 99.0 } },
                  { { 112.0, 105.0, 113.0 }, { 80.0, 67.0, 91.0 }, { 107.0, 92.0, 129.0 } } }, { { { 109.0, 64.0, 78.0 }, { 73.0, 41.0, 64.0 }, { 91.0, 77.0, 110.0 } },
                                                                                                 { { 65.0, 47.0, 59.0 }, { 59.0, 50.0, 56.0 }, { 71.0, 59.0, 71.0 } }, { { 96.0, 66.0, 103.0 }, { 89.0, 60.0, 65.0 }, { 95.0, 59.0, 85.0 } } },
                { { { 127.0, 81.0, 100.0 }, { 89.0, 66.0, 82.0 }, { 126.0, 78.0, 143.0 } }, { { 86.0, 68.0, 76.0 }, { 65.0, 52.0, 69.0 }, { 78.0, 63.0, 101.0 } },
                  { { 123.0, 95.0, 124.0 }, { 111.0, 68.0, 99.0 }, { 102.0, 77.0, 122.0 } } }
            };

            var ExpectedBinEdges = new double[] { 15.0, 18.3333333333333, 21.6666666666667, 25.0 };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], ExpectedBinEdges);
            AssertArray(x.bin_edges[1], ExpectedBinEdges);
            AssertArray(x.bin_edges[2], ExpectedBinEdges);
            AssertArray(x.bin_edges[3], ExpectedBinEdges);
            print(x.hist);
            print(x.bin_edges);

            /////
            System.Tuple <int, int>[] range3 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            x = np.histogramdd(r.reshape(-1, 2), bins: 2, density: true, range: range3);
            print(x.hist);
            print(x.bin_edges);
            /////
        }
        public void test_histogramdd_5()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(300000));

            System.Tuple <int, int>[] range1 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            var x            = np.histogramdd(r.reshape(-1, 4), bins: np.array(new int[] { 2, 2, 2, 2 }), range: range1);
            var ExpectedHist = new double[, , , ]
            {
                { { { 299, 352 }, { 375, 447 } }, { { 342, 454 }, { 389, 519 } } },
                { { { 377, 372 }, { 435, 535 } }, { { 446, 497 }, { 516, 625 } } }
            };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[1], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[2], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[3], new double[] { 15, 20, 25 });
            print(x.hist);
            print(x.bin_edges);

            /////
            System.Tuple <int, int>[] range2 = new Tuple <int, int>[]
            {
                Tuple.Create(20, 20),
                Tuple.Create(20, 20),
                Tuple.Create(20, 20),
                Tuple.Create(20, 20),
            };
            x            = np.histogramdd(r.reshape(-1, 4), bins:  np.array(new double[] { 2, 2, 2, 2 }), range: range2);
            ExpectedHist = new double[, , , ]
            {
                { { { 0, 0 }, { 0, 0 } }, { { 0, 0 }, { 0, 0 } } },
                { { { 0, 0 }, { 0, 0 } }, { { 0, 0 }, { 0, 0 } } }
            };
            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 19.5, 20, 20.5 });
            AssertArray(x.bin_edges[1], new double[] { 19.5, 20, 20.5 });
            AssertArray(x.bin_edges[2], new double[] { 19.5, 20, 20.5 });
            AssertArray(x.bin_edges[3], new double[] { 19.5, 20, 20.5 });
            print(x.hist);
            print(x.bin_edges);

            /////
            System.Tuple <int, int>[] range3 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            x = np.histogramdd(r.reshape(-1, 2), bins: np.array(new Int32[] { 3, 3 }), density: true, range: range3);
            var ExpectedHist2 = new double[, ]
            {
                { 0.0119444935087874, 0.00873134328358209, 0.011668285789985 },
                { 0.00910293208513644, 0.00670052106332243, 0.00903537048485383 },
                { 0.0117239247549236, 0.00896184756689923, 0.0121312814625099 }
            };

            AssertArray(x.hist, ExpectedHist2);
            AssertArray(x.bin_edges[0], new double[] { 15.0, 18.3333333333333, 21.6666666666667, 25.0 });
            AssertArray(x.bin_edges[1], new double[] { 15.0, 18.3333333333333, 21.6666666666667, 25.0 });
            print(x.hist);
            print(x.bin_edges);
            /////

            System.Tuple <int, int>[] range4 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            x = np.histogramdd(r.reshape(-1, 3), bins: np.array(new Int32[] { 4, 4, 4 }), density: false, range: range4);
            var ExpectedHist3 = new double[, , ]
            {
                { { 317.0, 222.0, 322.0, 333.0 }, { 198.0, 151.0, 230.0, 228.0 }, { 339.0, 196.0, 333.0, 360.0 }, { 340.0, 211.0, 341.0, 324.0 } },
                { { 221.0, 164.0, 231.0, 213.0 }, { 147.0, 101.0, 162.0, 162.0 }, { 226.0, 165.0, 228.0, 235.0 }, { 239.0, 161.0, 242.0, 220.0 } },
                { { 334.0, 213.0, 361.0, 364.0 }, { 224.0, 157.0, 232.0, 217.0 }, { 372.0, 226.0, 331.0, 351.0 }, { 350.0, 249.0, 344.0, 347.0 } },
                { { 348.0, 214.0, 337.0, 313.0 }, { 207.0, 169.0, 234.0, 206.0 }, { 347.0, 225.0, 305.0, 343.0 }, { 331.0, 225.0, 357.0, 374.0 } }
            };

            AssertArray(x.hist, ExpectedHist3);
            AssertArray(x.bin_edges[0], new double[] { 15.0, 17.5, 20.0, 22.5, 25.0 });
            AssertArray(x.bin_edges[1], new double[] { 15.0, 17.5, 20.0, 22.5, 25.0 });
            AssertArray(x.bin_edges[2], new double[] { 15.0, 17.5, 20.0, 22.5, 25.0 });
            print(x.hist);
            print(x.bin_edges);
        }
        public void test_histogramdd_1()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(3000));


            var x            = np.histogramdd(r.reshape(-1, 4), bins: new int[] { 2, 2, 2, 2 });
            var ExpectedHist = new double[, , , ]
            {
                { { { 48.0, 48.0 }, { 38.0, 58.0 } }, { { 50.0, 45.0 }, { 46.0, 49.0 } } },
                { { { 44.0, 40.0 }, { 44.0, 50.0 } }, { { 43.0, 36.0 }, { 60.0, 51.0 } } }
            };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[2], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[3], new double[] { 10.0, 19.5, 29 });
            print(x.hist);
            print(x.bin_edges);

            /////
            x            = np.histogramdd(r.reshape(-1, 4), bins: new double[] { 2, 2, 2, 2 });
            ExpectedHist = new double[, , , ]
            {
                { { { 48.0, 48.0 }, { 38.0, 58.0 } }, { { 50.0, 45.0 }, { 46.0, 49.0 } } },
                { { { 44.0, 40.0 }, { 44.0, 50.0 } }, { { 43.0, 36.0 }, { 60.0, 51.0 } } }
            };
            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[2], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[3], new double[] { 10.0, 19.5, 29 });
            print(x.hist);
            print(x.bin_edges);

            /////
            x = np.histogramdd(r.reshape(-1, 2), bins: new Int32[] { 3, 3 }, density: true);
            var ExpectedHist2 = new double[, ]
            {
                { 0.00307479224376731, 0.00259279778393352, 0.00282548476454294 },
                { 0.00245983379501385, 0.00237673130193906, 0.00280886426592798 },
                { 0.00292520775623269, 0.00250969529085873, 0.0033573407202216 }
            };

            AssertArray(x.hist, ExpectedHist2);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 16.3333333333333, 22.6666666666667, 29.0 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 16.3333333333333, 22.6666666666667, 29.0 });
            print(x.hist);
            print(x.bin_edges);

            x = np.histogramdd(r.reshape(-1, 3), bins: new Int32[] { 4, 4, 4 }, density: false);
            var ExpectedHist3 = new double[, , ]
            {
                { { 19.0, 21.0, 19.0, 15.0 }, { 22.0, 11.0, 11.0, 10.0 }, { 17.0, 13.0, 7.0, 12.0 }, { 12.0, 11.0, 7.0, 23.0 } },
                { { 12.0, 13.0, 16.0, 14.0 }, { 12.0, 14.0, 15.0, 20.0 }, { 17.0, 16.0, 19.0, 12.0 }, { 13.0, 13.0, 17.0, 16.0 } },
                { { 23.0, 22.0, 9.0, 13.0 }, { 12.0, 23.0, 20.0, 21.0 }, { 23.0, 14.0, 14.0, 25.0 }, { 19.0, 19.0, 13.0, 21.0 } },
                { { 18.0, 18.0, 13.0, 11.0 }, { 11.0, 11.0, 15.0, 20.0 }, { 14.0, 15.0, 15.0, 24.0 }, { 13.0, 15.0, 11.0, 16.0 } }
            };

            AssertArray(x.hist, ExpectedHist3);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 14.75, 19.5, 24.25, 29.0 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 14.75, 19.5, 24.25, 29.0 });
            AssertArray(x.bin_edges[2], new double[] { 10.0, 14.75, 19.5, 24.25, 29.0 });
            print(x.hist);
            print(x.bin_edges);
        }