示例#1
0
        public static NDArray array <T>(this NumPy np, IEnumerable <T> array, int ndim = 1)
        {
            var nd = new NDArray(typeof(T));

            nd.Set(array.ToArray());
            nd.Shape = new Shape(new int[] { array.Count() });

            return(nd);
        }
示例#2
0
        public static NDArray array <T>(this NumPy np, System.Drawing.Bitmap image)
        {
            var imageArray = new NDArray(typeof(T));

            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;

            imageArray.Set(new byte[dataSize]);
            System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, (imageArray.Storage.values as byte[]), 0, imageArray.Size);
            image.UnlockBits(bmpd);

            imageArray.Shape = new Shape(new int[] { bmpd.Height, bmpd.Width, System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8 });

            return(imageArray);
        }
示例#3
0
        public NDArray delete(IEnumerable delete)
        {
            var np = new NumPy();

            var sysArr = this.Storage.GetData();

            NDArray res = null;

            switch (sysArr)
            {
            case double[] castedSysArr:
            {
                var castedDelete = delete as double[];

                res = np.array(castedSysArr.Where(x => !castedDelete.Contains(x)).ToArray());

                break;
            }

            case float[] castedSysArr:
            {
                var castedDelete = delete as float[];

                res = np.array(castedSysArr.Where(x => !castedDelete.Contains(x)).ToArray());

                break;
            }

            case int[] castedSysArr:
            {
                var castedDelete = delete as int[];

                res = np.array(castedSysArr.Where(x => !castedDelete.Contains(x)).ToArray());

                break;
            }

            default:
            {
                throw new IncorrectTypeException();
            }
            }

            return(res);
        }
示例#4
0
        public NDArray permutation(int max)
        {
            var random = new Random();

            int[] orders = new int[max];

            var np = new NumPy().arange(max);

            for (int i = 0; i < max; i++)
            {
                var pos  = random.Next(0, max);
                var zero = np.Data <int>(0);
                np[0]   = np.Data <int>(pos);
                np[pos] = zero;
            }

            return(np);
        }
示例#5
0
        public void normalize()
        {
            var np  = new NumPy();
            var min = this.min(0);
            var max = this.max(0);

            if (ndim == 2)
            {
                for (int col = 0; col < shape.Shapes[1]; col++)
                {
                    double der = max.Data <double>(col) - min.Data <double>(col);
                    for (int row = 0; row < shape.Shapes[0]; row++)
                    {
                        this[row, col] = (Data <double>(row, col) - min.Data <double>(col)) / der;
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
示例#6
0
        public static NDArray array <T>(this NumPy np, T[][] data)
        {
            int size = data.Length * data[0].Length;
            var all  = new T[size];

            int idx = 0;

            for (int row = 0; row < data.Length; row++)
            {
                for (int col = 0; col < data[row].Length; col++)
                {
                    all[idx] = data[row][col];
                    idx++;
                }
            }

            var nd = new NDArray(typeof(T));

            nd.Set(all.ToArray());
            nd.Shape = new Shape(new int[] { data.Length, data[0].Length });

            return(nd);
        }
示例#7
0
        /// <summary>
        /// Convolution of 2 series
        /// </summary>
        /// <param name="numSharpArray1"></param>
        /// <param name="numSharpArray2"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public NDArray Convolve(NDArray numSharpArray2, string mode = "full")
        {
            int nf = this.shape.Shapes[0];
            int ng = numSharpArray2.shape.Shapes[0];

            if (shape.NDim > 1)
            {
                throw new IncorrectShapeException();
            }

            var numSharpReturn = new NDArray(typeof(double));

            double[] np1 = this.Storage.GetData <double>();
            double[] np2 = numSharpArray2.Storage.GetData <double>();

            switch (mode)
            {
            case "full":
            {
                int n = nf + ng - 1;

                var outArray = new double[n];

                for (int idx = 0; idx < n; ++idx)
                {
                    int jmn = (idx >= ng - 1) ? (idx - (ng - 1)) : 0;
                    int jmx = (idx < nf - 1) ? idx : nf - 1;

                    for (int jdx = jmn; jdx <= jmx; ++jdx)
                    {
                        outArray[idx] += (np1[jdx] * np2[idx - jdx]);
                    }
                }

                numSharpReturn.Storage = NDStorage.CreateByShapeAndType(numSharpReturn.dtype, new Shape(outArray.Length));
                numSharpReturn.Storage.SetData(outArray);

                break;
            }

            case "valid":
            {
                var min_v = (nf < ng) ? np1 : np2;
                var max_v = (nf < ng) ? np2 : np1;

                int n = Math.Max(nf, ng) - Math.Min(nf, ng) + 1;

                double[] outArray = new double[n];

                for (int idx = 0; idx < n; ++idx)
                {
                    int kdx = idx;

                    for (int jdx = (min_v.Length - 1); jdx >= 0; --jdx)
                    {
                        outArray[idx] += min_v[jdx] * max_v[kdx];
                        ++kdx;
                    }
                }

                numSharpReturn.Storage = NDStorage.CreateByShapeAndType(numSharpReturn.dtype, new Shape(outArray.Length));
                numSharpReturn.Storage.SetData(outArray);

                break;
            }

            case "same":
            {
                // followed the discussion on
                // https://stackoverflow.com/questions/38194270/matlab-convolution-same-to-numpy-convolve
                // implemented numpy convolve because we follow numpy
                var npad = numSharpArray2.shape.Shapes[0] - 1;

                double[] np1New = null;

                if (npad % 2 == 1)
                {
                    npad = (int)Math.Floor(((double)npad) / 2.0);

                    np1New = (double[])np1.Clone();

                    np1New.ToList().AddRange(new double[npad + 1]);
                    var puffer = (new double[npad]).ToList();
                    puffer.AddRange(np1New);
                    np1New = puffer.ToArray();
                }
                else
                {
                    npad = npad / 2;

                    np1New = (double[])np1.Clone();

                    var puffer = np1New.ToList();
                    puffer.AddRange(new double[npad]);
                    np1New = puffer.ToArray();

                    puffer = (new double[npad]).ToList();
                    puffer.AddRange(np1New);
                    np1New = puffer.ToArray();
                }

                var numSharpNew = new NumPy().array(np1New, dtype);

                numSharpReturn = numSharpNew.Convolve(numSharpArray2, "valid");
                break;
            }
            }

            return(numSharpReturn);
        }