예제 #1
0
        public void test_randn_UC_1()
        {
            var random = new np.random(new UserDefinedRandomGenerator());

            double  fr  = random.randn();
            ndarray arr = random.randn(new shape(2, 3, 4));

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_DOUBLE);
            AssertShape(arr, 2, 3, 4);

            random.seed(1234);

            fr = random.randn();
            print(fr);
            //Assert.AreEqual(-0.29042058667075177, fr);

            arr = random.randn(new shape(5000000));
            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_DOUBLE);

            var amax = np.amax(arr);

            print(amax);
            Assert.AreEqual(5.070295032710753, (double)amax);

            var amin = np.amin(arr);

            print(amin);
            Assert.AreEqual(-5.04633622266897, (double)amin);

            var avg = np.average(arr);

            print(avg);
            Assert.AreEqual(-0.00044428007674556522, (double)avg);
        }
예제 #2
0
        public void test_histogram2d_3()
        {
            var random = new np.random();

            random.seed(8765);

            var x = random.normal(2, 1, new shape(100));
            var y = random.normal(1, 1, new shape(100));

            var xedges = np.array(new int[] { 0, 1, 3, 5 });
            var yedges = np.array(new int[] { 0, 2, 3, 4, 6 });

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

            weights.fill(0.5);

            var bins = new List <ndarray>()
            {
                xedges, yedges
            };

            var result = np.histogram2d(x, y, bins: bins.ToArray());

            AssertArray(result.H, new double[, ] {
                { 11.0, 1.0, 2.0, 0.0 }, { 37.0, 15.0, 2.0, 0.0 }, { 13.0, 1.0, 0.0, 0.0 }
            });
            print(result.H);
            AssertArray(result.xedges, new double[] { 0, 1, 3, 5 });
            print(result.xedges);
            AssertArray(result.yedges, new double[] { 0, 2, 3, 4, 6 });
            print(result.yedges);
        }
예제 #3
0
        public void test_rand_UD_1()
        {
            var     random = new np.random(new UserDefinedRandomGenerator());
            ndarray arr    = random.rand(new shape(2, 3, 4));

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_DOUBLE);
            AssertShape(arr, 2, 3, 4);

            random.seed(8765);
            double f = random.rand();

            print(f);
            Assert.AreEqual(0.78412213771795958, f);

            arr = random.rand(new shape(5000000));

            var amax = np.amax(arr);

            print(amax);
            Assert.AreEqual(0.99999997671693563, (double)amax);

            var amin = np.amin(arr);

            print(amin);
            Assert.AreEqual(5.1688402915228346E-08, (double)amin);

            var avg = np.average(arr);

            print(avg);
            Assert.AreEqual(0.50011801294498859, (double)avg);
        }
예제 #4
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));
        }
예제 #5
0
        private static void Main(string[] args)
        {
            var npx = new np.random().uniform(newdims: new shape(3, 3));
            var x   = new NDArray(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new Shape(3, 3)).Reshape(new Shape(3, 3));
            var ndx = nd.Array(npx.astype(np.Int8));



            var buff = x.GetBuffer();
            var x1   = NDArray.LoadFromBuffer(buff);

            var zeros = nd.Zeros(new Shape(3, 3));

            x[":,3"] = x[":,2"];
            var data1 = x.AsArray <float>();

            x = nd.Flip(x, 1);
            x = nd.Square(x);
            var a = Autograd.GetSymbol(x);
            var y = nd.EqualScalar(x, 3);
            //var acc = new Accuracy();
            var data = y.GetValues <float>();
            //acc.Update(x, y);
            var eq = nd.Equal(x, y);

            for (var i = 1; i <= 100000; i++)
            {
                x.SampleUniform();
                x = nd.Square(x);
                Console.WriteLine(i);
            }

            Console.ReadLine();
        }
예제 #6
0
        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);
        }
예제 #7
0
        public void test_histogram2d_2()
        {
            var random = new np.random();

            random.seed(8765);

            var x = random.normal(2, 1, new shape(100));
            var y = random.normal(1, 1, new shape(100));

            var xedges = new int[] { 0, 1, 3, 5 };
            var yedges = new int[] { 0, 2, 3, 4, 6 };

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

            weights.fill(0.5);

            var bins = new List <Int32[]>()
            {
                xedges, yedges
            };

            var result = np.histogram2d(x, y, bins: 2);

            AssertArray(result.H, new double[, ] {
                { 29.0, 17.0 }, { 33.0, 21.0 }
            });
            print(result.H);
            AssertArray(result.xedges, new double[] { -0.267399252084981, 2.06371407063072, 4.39482739334641 });
            print(result.xedges);
            AssertArray(result.yedges, new double[] { -0.984644701475661, 1.45595900692665, 3.89656271532895 });
            print(result.yedges);
        }
예제 #8
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 });
        }
예제 #9
0
        public override IEnumerator <int[]> GetEnumerator()
        {
            ndarray sample_ids = null;

            if (this._num_parts > 1)
            {
                this._shuffled_ids = new np.random(new NumpyDotNet.RandomAPI.RandomState()).permutation(this._shuffled_ids);
            }
            if (this._shuffle)
            {
                sample_ids = new np.random().permutation(this._shuffled_ids[$"{_start}:{_end}"]);
            }
            else
            {
                sample_ids = np.array(Enumerable.Range(_start, this._end - this._start).ToArray());
            }
            var bucket_size = Convert.ToInt32(this._mult * this._batch_size);
            var buckets     = Enumerable.Range(0, Convert.ToInt32(Math.Ceiling(Convert.ToDouble(sample_ids.Size - 0) / bucket_size))).Select(_x_1 => 0 + _x_1 * bucket_size).ToList();

            for (int bucket_begin = 0; bucket_begin < buckets.Count; bucket_begin++)
            {
                var bucket_end = (int)Math.Min(bucket_begin + bucket_size, sample_ids.Size);
                if (bucket_end - bucket_begin < this._batch_size)
                {
                    bucket_begin = bucket_end - this._batch_size;
                }

                var sorted_sample_ids = (ndarray)sample_ids[$"{bucket_begin}:{bucket_end}"];
                sorted_sample_ids = np.sort(sorted_sample_ids, kind: NumpyLib.NPY_SORTKIND.NPY_QUICKSORT);

                var batch_begins = Enumerable.Range(0, Convert.ToInt32(Math.Ceiling(Convert.ToDouble(sorted_sample_ids.Size - 0) / this._batch_size))).Select(_x_2 => 0 + _x_2 * this._batch_size).ToList();
                if (this._shuffle)
                {
                    batch_begins.Shuffle();
                }
                foreach (var batch_begin in batch_begins)
                {
                    var batch_end = Math.Min(batch_begin + this._batch_size, sorted_sample_ids.Size);
                    if (batch_end - batch_begin < this._batch_size)
                    {
                        yield return(((ndarray)sorted_sample_ids[$"{(batch_end - _batch_size)}:{batch_end}"]).AsInt32Array());
                    }
                    else
                    {
                        yield return(((ndarray)sorted_sample_ids[$"{batch_begin}:{batch_end}"]).AsInt32Array());
                    }
                }
            }
        }
예제 #10
0
        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);
            /////
        }
예제 #11
0
        public override NDArrayOrSymbol Forward(NDArrayOrSymbol x, params NDArrayOrSymbol[] args)
        {
            NDArray matches = x;
            NDArray ious    = args[0];
            ndarray disable_indices;
            var     max_pos = Convert.ToInt32(Math.Round(this._pos_ratio * this._num_sample));
            var     max_neg = Convert.ToInt32(this._neg_ratio * this._num_sample);
            var     results = new List <NDArray>();

            foreach (var i in Enumerable.Range(0, matches.Shape[0]))
            {
                // init with 0s, which are ignored
                var result = nd.ZerosLike(matches[0]);
                // positive samples
                var ious_max = ious.Max(axis: -1)[i];
                result = nd.Where(matches[i] >= 0, nd.OnesLike(result), result);
                result = nd.Where(ious_max >= this._pos_thresh, nd.OnesLike(result), result);
                // negative samples with label -1
                var neg_mask = ious_max < this._neg_thresh_high;
                neg_mask = neg_mask * (ious_max >= this._neg_thresh_low);
                result   = nd.Where(neg_mask, nd.OnesLike(result) * -1, result);
                var np_result = result.AsNumpy();
                // re-balance if number of positive or negative exceed limits
                var num_pos = Convert.ToInt32((result > 0).Sum());
                if (num_pos > max_pos)
                {
                    disable_indices            = new np.random().choice(np.where (np_result > 0), size: num_pos - max_pos, replace: false);
                    np_result[disable_indices] = 0;
                }
                var num_neg = Convert.ToInt32((result < 0).Sum());
                if (this._fill_negative)
                {
                    // if pos_sample is less than quota, we can have negative samples filling the gap
                    max_neg = Math.Max(this._num_sample - Math.Min(num_pos, max_pos), max_neg);
                }
                if (num_neg > max_neg)
                {
                    disable_indices            = new np.random().choice(np.where (result < 0), size: num_neg - max_neg, replace: false);
                    np_result[disable_indices] = 0;
                }

                results.Add(nd.Array(np_result));
            }
            return(nd.Stack(results, results.Count, axis: 0));
        }
예제 #12
0
        public double[] GenerateArt(int batch_size = 1, int net_size = 16, int h_size = 8, int width = 512, int height = 512, float scaling = 10.0f, bool RGB = true, int?seed = null)
        {
            int?KeyId = null;

            if (seed != null)
            {
                KeyId  = seed;
                random = new np.random();
                random.seed(KeyId);
            }
            else
            {
                KeyId  = Math.Abs(DateTime.Now.GetHashCode());
                random = new np.random();
                random.seed(KeyId);
            }

            var art = new ArtGenNumpyDotNet();

            art.InitialiseCPPN(batch_size, net_size, RGB: RGB);

            if (RGB)
            {
                C_Dim = 3;
            }
            else
            {
                C_Dim = 1;
            }

            var imageData = art.Generate(width, height, scaling);

            var imgData = np.array(np.array(1) - imageData);

            if (C_Dim > 1)
            {
                imgData = np.array(imgData.reshape((height, width, C_Dim)) * 255.0);
            }
            else
            {
                imgData = np.array(imgData.reshape((height, width)) * 255.0);
            }

            return(imgData.ToArray <double>());
        }
예제 #13
0
        public override void Forward(bool is_train, OpGradReq[] req, NDArrayList in_data, NDArrayList out_data, NDArrayList aux)
        {
            ndarray disable_indices;
            var     matches = in_data[0];
            var     ious    = in_data[1];
            var     max_pos = Convert.ToInt32(Math.Round(this._pos_ratio * this._num_sample));
            var     max_neg = Convert.ToInt32(this._neg_ratio * this._num_sample);

            foreach (var i in Enumerable.Range(0, matches.Shape[0]))
            {
                // init with 0s, which are ignored
                var result = nd.ZerosLike(matches[i]);
                // negative samples with label -1
                var ious_max = ious.Max(axis: -1)[i];
                var neg_mask = ious_max < this._neg_thresh_high;
                neg_mask = neg_mask * (ious_max >= this._neg_thresh_low);
                result   = nd.Where(neg_mask, nd.OnesLike(result) * -1, result);
                // positive samples
                result = nd.Where(matches[i] >= 0, nd.OnesLike(result), result);
                result = nd.Where(ious_max >= this._pos_thresh, nd.OnesLike(result), result);
                // re-balance if number of positive or negative exceed limits
                var np_result = result.AsNumpy();
                var num_pos   = Convert.ToInt32((result > 0).Sum());
                if (num_pos > max_pos)
                {
                    disable_indices            = new np.random().choice(np.where (np_result > 0), size: num_pos - max_pos, replace: false);
                    np_result[disable_indices] = 0;
                }
                var num_neg = Convert.ToInt32((result < 0).Sum());
                if (this._fill_negative)
                {
                    // if pos_sample is less than quota, we can have negative samples filling the gap
                    max_neg = Math.Max(this._num_sample - Math.Min(num_pos, max_pos), max_neg);
                }
                if (num_neg > max_neg)
                {
                    disable_indices            = new np.random().choice(np.where (np_result < 0), size: num_neg - max_neg, replace: false);
                    np_result[disable_indices] = 0;
                }

                this.Assign(out_data[0][i], req[0], nd.Array(np_result));
            }
        }
예제 #14
0
파일: ESN.cs 프로젝트: thild/csESN
        //  """
        // Args:
        //     n_inputs: nr of input dimensions
        //         n_outputs: nr of output dimensions
        //         n_reservoir: nr of reservoir neurons
        //         spectral_radius: spectral radius of the recurrent weight matrix
        //     sparsity: proportion of recurrent weights set to zero
        //     noise: noise added to each neuron(regularization)
        //         input_shift: scalar or vector of length n_inputs to add to each
        //                     input dimension before feeding it to the network.
        //     input_scaling: scalar or vector of length n_inputs to multiply
        //                 with each input dimension before feeding it to the netw.
        //     teacher_forcing: if True, feed the target back into output units
        //     teacher_scaling: factor applied to the target signal
        //     teacher_shift: additive term applied to the target signal
        //     out_activation: output activation function (applied to the readout)
        //         inverse_out_activation: inverse of the output activation function
        //         random_state: positive integer seed, np.rand.RandomState object,
        //                   or None to use numpy's builting RandomState.
        //     silent: supress messages
        // """
        public ESN(int n_inputs, int n_outputs, int n_reservoir = 200,
                   double spectral_radius   = 0.95, double sparsity = 0, double noise = 0.001,
                   ndarray input_shift      = null,
                   ndarray input_scaling    = null,
                   bool teacher_forcing     = true,
                   ndarray feedback_scaling = null,
                   double teacher_scaling   = 0, double teacher_shift = 0,
                   Func <ndarray, ndarray> out_activation         = null,
                   Func <ndarray, ndarray> inverse_out_activation = null,
                   object random_state = null,
                   bool silent         = true)
        {
            if (out_activation == null)
            {
                out_activation = IdentityFunction <ndarray>();
            }
            if (inverse_out_activation == null)
            {
                inverse_out_activation = IdentityFunction <ndarray>();
            }
            // check for proper dimensionality of all arguments and write them down.
            this.n_inputs        = n_inputs;
            this.n_reservoir     = n_reservoir;
            this.n_outputs       = n_outputs;
            this.spectral_radius = spectral_radius;
            this.sparsity        = sparsity;
            this.noise           = noise;
            this.input_shift     = correct_dimensions(input_shift, n_inputs);
            this.input_scaling   = correct_dimensions(input_scaling, n_inputs);

            this.teacher_scaling = teacher_scaling;
            this.teacher_shift   = teacher_shift;

            this.out_activation         = out_activation;
            this.inverse_out_activation = inverse_out_activation;

            // the given random_state might be either an actual RandomState object,
            // a seed or None (in which case we use numpy's builtin RandomState)
            if (random_state is RandomState)
            {
                this.random_state_ = random_state as np.random;
            }
            else if (random_state is int)
            {
                try
                {
                    var rnd = new np.random();
                    rnd.seed((int)random_state);
                    this.random_state_ = rnd;
                }
                catch (System.Exception ex)
                {
                    throw new Exception("Invalid seed: " + ex);
                }
            }
            else
            {
                this.random_state_ = new np.random();
            }

            this.teacher_forcing = teacher_forcing;
            this.silent          = silent;
            this.initweights();
        }
예제 #15
0
        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);
        }
예제 #16
0
        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);
        }