/// <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 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.Set(Enumerable.Range(0, nd.size).Select(x => 1).ToArray()); break; case "Double": nd.Set(Enumerable.Range(0, nd.size).Select(x => 1.0).ToArray()); break; case "Boolean": nd.Set(Enumerable.Range(0, nd.size).Select(x => true).ToArray()); break; } return(nd); }
/// <summary> /// Stack arrays in sequence vertically (row wise). /// </summary> /// <param name="nps"></param> /// <returns></returns> public NDArray vstack <T>(params NDArray[] nps) { if (nps == null || nps.Length == 0) { throw new Exception("Input arrays can not be empty"); } List <T> list = new List <T>(); var np = new NDArray(typeof(T)); foreach (NDArray ele in nps) { if (nps[0].shape != ele.shape) { throw new Exception("Arrays mush have same shapes"); } list.AddRange(ele.Data <T>()); } np.Set(list.ToArray()); if (nps[0].ndim == 1) { np.Storage.Shape = new Shape(new int[] { nps.Length, nps[0].shape.Shapes[0] }); } else { int[] shapes = nps[0].shape.Shapes.ToArray(); shapes[0] *= nps.Length; np.Storage.Shape = new Shape(shapes); } return(np); }
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.Shapes.ToArray()); np.Set(data); return(np); }
public static NDArray operator /(double scalar, NDArray np1) { var nd = new NDArray(typeof(double), np1.shape); switch (np1.dtype.Name) { case "Double": nd.Set(np1.float64.Select(x => scalar / x).ToArray()); break; case "Int32": nd.Set(np1.int32.Select(x => scalar / x).ToArray()); break; } return(nd); }
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); }
public NDArray unique <T>() { var nd = new NDArray(dtype); var data = Data <T>().Distinct().ToArray(); nd.Set(data); nd.Storage.Shape = new Shape(data.Length); return(nd); }
public new matrix transpose() { Storage.Shape = new Shape(this.Storage.Shape.Shapes.Reverse().ToArray()); var nd = new NDArray(this.dtype, this.shape); switch (this.dtype.Name) { case "Double": nd.Set(this.float64); break; case "Int32": nd.Set(this.int32); break; } if (ndim == 1) { Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(1, shape.Shapes[0])); Storage.SetData(nd.float64); } else if (ndim == 2) { for (int idx = 0; idx < shape.Shapes[0]; idx++) { for (int jdx = 0; jdx < shape.Shapes[1]; jdx++) { this[idx, jdx] = nd[jdx, idx]; } } } else { throw new NotImplementedException(); } return(this); }
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); }
public NDArray randint(int low, int?high = null, Shape size = null) { var rng = new Random(); var data = new int[size.Size]; for (int i = 0; i < data.Length; i++) { data[i] = rng.Next(low, high.HasValue ? high.Value : int.MaxValue); } var np = new NDArray(typeof(int), size.Shapes.ToArray()); np.Set(data); return(np); }
public NDArray randint(int low, int size = 1) { var rng = new Random(); var data = new int[size]; for (int i = 0; i < data.Length; i++) { data[i] = rng.Next(low, int.MaxValue); } var np = new NDArray(typeof(int), size); np.Set(data); return(np); }
public 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.Set(doubleArray); return(nd); }
public NDArray array <T>(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.Storage.Shape = new Shape(new int[] { data.Length, data[0].Length }); return(nd); }