Esempio n. 1
0
        /// <summary>
        /// Return a new array of given shape and type, filled with ones.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="np"></param>
        /// <param name="shape"></param>
        /// <returns></returns>
        public static NDArray ones(Shape shape, Type dtype = null)
        {
            if (dtype == null)
            {
                dtype = typeof(double);
            }

            var nd = new NDArray(dtype, shape);

            switch (dtype.Name)
            {
            case "Int32":
                nd.SetData(Enumerable.Range(0, nd.size).Select(x => 1).ToArray());
                break;

            case "Double":
                nd.SetData(Enumerable.Range(0, nd.size).Select(x => 1.0).ToArray());
                break;

            case "Boolean":
                nd.SetData(Enumerable.Range(0, nd.size).Select(x => true).ToArray());
                break;
            }

            return(nd);
        }
Esempio n. 2
0
        public static NDArray asanyarray <T>(T data) where T : struct
        {
            var nd = new NDArray(typeof(T), new int[0]);

            nd.SetData(new T[] { data });
            return(nd);
        }
Esempio n. 3
0
        public NDArray randint(int low, int?high = null, Shape shape = null)
        {
            var rng = new Random();

            if (high == null)
            {
                high = int.MaxValue;
            }
            if (shape == null)
            {
                shape = new Shape(high.Value - low);
            }
            var data = new int[shape.Size];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = rng.Next(low, high.Value);
            }

            var np = new NDArray(typeof(int), shape.Dimensions.ToArray());

            np.SetData(data);

            return(np);
        }
Esempio n. 4
0
        public static NDArray asanyarray <T>(T[] data, int ndim = 1) where T : struct
        {
            var nd = new NDArray(typeof(T), data.Length);

            nd.SetData(data);
            return(nd);
        }
Esempio n. 5
0
        /// <summary>
        ///     Draw samples from a uniform distribution.
        ///     Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.
        /// </summary>
        /// <param name="low">Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.</param>
        /// <param name="high">Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.</param>
        /// <param name="size">Output shape. If the given shape is, e.g., m, n, k, then m * n * k samples are drawn. If size is None (default), a single value is returned if low and high are both scalars. </param>
        /// <returns>NDArray with values of type <see cref="double"/></returns>
        public NDArray uniform(double low, double high, params int[] size)
        {
            if (size == null || size.Length == 0) //return scalar
            {
                var ret  = new NDArray <double>(new Shape(1));
                var data = new double[] { low + randomizer.NextDouble() * (high - low) };
                ret.SetData(data);
                return(ret);
            }

            var result = new NDArray <double>(size);

            double[] resultArray = result.Data <double>();

            //parallelism is prohibited to make sure the result come out presistant
            double diff = high - low;

            for (int i = 0; i < result.size; ++i)
            {
                resultArray[i] = low + randomizer.NextDouble() * diff;
            }

            result.SetData(resultArray); //incase of a view
            return(result);
        }
Esempio n. 6
0
        public static NDArray asanyarray(string data)
        {
            var nd = new NDArray(typeof(string), new int[0]);

            nd.SetData(new string[] { data });
            return(nd);
        }
Esempio n. 7
0
        public static NDArray asanyarray(string[] data, int ndim = 1)
        {
            var nd = new NDArray(typeof(string), data.Length);

            nd.SetData(data);
            return(nd);
        }
Esempio n. 8
0
        public static NDArray ravel(matrix mx)
        {
            var nd = new NDArray(mx.dtype, mx.size);

            switch (mx.dtype.Name)
            {
            case "Double":
                nd.SetData(mx.Data <double>());
                break;

            case "Int32":
                nd.SetData(mx.Data <int>());
                break;
            }

            return(nd);
        }
Esempio n. 9
0
        /// <summary>
        ///     Random values in a given shape.
        ///     Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
        /// </summary>
        public NDArray rand(Shape shape)
        {
            NDArray ndArray = new NDArray(typeof(double), shape);

            double[] numArray = ndArray.Data <double>();
            for (int index = 0; index < ndArray.size; ++index)
            {
                numArray[index] = randomizer.NextDouble();
            }

            ndArray.SetData <double[]>(numArray);
            return(ndArray);
        }
Esempio n. 10
0
        public static NDArray array <T>(T[,] data)
        {
            var nd = new NDArray(typeof(T), new Shape(data.GetLength(0), data.GetLength(1)));

            for (int dim0 = 0; dim0 < data.GetLength(0); dim0++)
            {
                for (int dim1 = 0; dim1 < data.GetLength(1); dim1++)
                {
                    nd.SetData(data[dim0, dim1], dim0, dim1);
                }
            }

            return(nd);
        }
Esempio n. 11
0
        /*public static NDArray array(System.Drawing.Bitmap image)
         * {
         *  var imageArray = new NDArray(typeof(Byte));
         *
         *  var bmpd = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
         *  var dataSize = bmpd.Stride * bmpd.Height;
         *
         *  var bytes = new byte[dataSize];
         *  System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bytes, 0, dataSize);
         *  image.UnlockBits(bmpd);
         *
         *  imageArray.Storage.Allocate(typeof(byte),new Shape(bmpd.Height, bmpd.Width, System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8),1);
         *  imageArray.Storage.SetData(bytes);
         *
         *  return imageArray;
         * }*/

        public static NDArray array <T>(T[][] data)
        {
            var nd = new NDArray(typeof(T), new Shape(data.Length, data[0].Length));

            for (int row = 0; row < data.Length; row++)
            {
                for (int col = 0; col < data[row].Length; col++)
                {
                    nd.SetData(data[row][col], row, col);
                }
            }

            return(nd);
        }
Esempio n. 12
0
        public NDArray randint(int low, int size = 1)
        {
            var data = new int[size];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = randomizer.Next(low, int.MaxValue);
            }

            var np = new NDArray(typeof(int), size);

            np.SetData(data);

            return(np);
        }
Esempio n. 13
0
        private NDArray setValue2D <T>(NDArray indexes)
        {
            var buf = Data <T>();
            var idx = indexes.Data <int>();
            var nd  = new NDArray(dtype, new Shape(indexes.size, shape[1]));

            Parallel.For(0, nd.shape[0], (row) =>
            {
                for (int col = 0; col < nd.shape[1]; col++)
                {
                    nd.SetData(buf[Storage.Shape.GetIndexInShape(idx[row], col)], row, col);
                }
            });

            return(nd);
        }
Esempio n. 14
0
        private NDArray setValue2D <T>(NDArray indexes)
        {
            var buf            = Data <T>();
            var idx            = indexes.Data <int>();
            var selectedValues = new NDArray(dtype, new Shape(indexes.size, shape[1]));

            Parallel.ForEach(Enumerable.Range(0, selectedValues.shape[0]), (row) =>
            {
                for (int col = 0; col < selectedValues.shape[1]; col++)
                {
                    selectedValues.SetData(buf[Storage.Shape.GetIndexInShape(idx[row], col)], row, col);
                }
            });

            return(selectedValues);
        }
Esempio n. 15
0
        public static NDArray array(Array array, Type dtype = null, int ndim = 1)
        {
            dtype = (dtype == null) ? array.GetType().GetElementType() : dtype;

            var nd = new NDArray(dtype, new Shape(new int[] { array.Length }));

            if ((array.Rank == 1) && (!array.GetType().GetElementType().IsArray))
            {
                nd.SetData(array);
            }
            else
            {
                throw new Exception("Method is not implemeneted for multidimensional arrays or jagged arrays.");
            }

            return(nd);
        }
Esempio n. 16
0
        public static NDArray linspace <T>(double start, double stop, int num, bool entdpoint = true)
        {
            double steps = (stop - start) / ((entdpoint) ? (double)num - 1.0 : (double)num);

            double[] doubleArray = new double[num];

            for (int idx = 0; idx < doubleArray.Length; idx++)
            {
                doubleArray[idx] = start + idx * steps;
            }

            var nd = new NDArray(typeof(T), doubleArray.Length);

            nd.SetData(doubleArray);

            return(nd);
        }
Esempio n. 17
0
        /// <summary>
        /// Draw random samples from a normal (Gaussian) distribution.
        /// </summary>
        /// <param name="loc">Mean of the distribution</param>
        /// <param name="scale">Standard deviation of the distribution</param>
        /// <param name="dims"></param>
        /// <returns></returns>
        public NDArray normal(double loc, double scale, params int[] dims)
        {
            var array = new NDArray(typeof(double), new Shape(dims));

            double[] arr = array.Data <double>();

            for (int i = 0; i < array.size; i++)
            {
                double u1            = 1.0 - randomizer.NextDouble(); //uniform(0,1] random doubles
                double u2            = 1.0 - randomizer.NextDouble();
                double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                                       Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
                double randNormal = loc + scale * randStdNormal;     //random normal(mean,stdDev^2)
                arr[i] = randNormal;
            }

            array.SetData(arr);

            return(array);
        }
Esempio n. 18
0
        public static NDArray arange(int start, int stop, int step = 1)
        {
            if (start > stop)
            {
                throw new Exception("parameters invalid, start is greater than stop.");
            }

            int length = (int)Math.Ceiling((stop - start + 0.0) / step);
            int index  = 0;

            var nd = new NDArray(np.int32, new Shape(length));

            var a = new int[length];

            for (int i = start; i < stop; i += step)
            {
                a[index++] = i;
            }
            nd.SetData(a);

            return(nd);
        }
Esempio n. 19
0
        private NDArray setValue4D <T>(NDArray indexes)
        {
            var buf            = Data <T>();
            var selectedValues = new NDArray(dtype, new Shape(indexes.size, shape[1], shape[2], shape[3]));
            var idx            = indexes.Data <int>();

            Parallel.ForEach(Enumerable.Range(0, selectedValues.shape[0]), (item) =>
            {
                for (int row = 0; row < selectedValues.shape[1]; row++)
                {
                    for (int col = 0; col < selectedValues.shape[2]; col++)
                    {
                        for (int channel = 0; channel < selectedValues.shape[3]; channel++)
                        {
                            selectedValues.SetData(buf[Storage.Shape.GetIndexInShape(idx[item], row, col, channel)], item, row, col, channel);
                        }
                    }
                }
            });

            return(selectedValues);
        }