If you like the NumPy package from python, then in this tutorial, I will show you how to use the NumSharp package.
You can read more about this package on the GitHub webpage.
Let’s see some definitions and save and load features with this package:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using NumSharp; namespace NumSharp001 { internal class Program { static void Main(string[] args) { Console.WriteLine("Hello World using the NumSharp package!"); Console.WriteLine("basic definitions "); var array001 = np.full(5, 6); //[5, 5, 5 .. 5] Console.WriteLine("This array001 variable has these 6 values : " + array001); var array_zeros = np.zeros(6); Console.WriteLine("This array_zeros variable has these 6 values : " + array_zeros); var array_arange = np.arange(6); Console.WriteLine("This array_arange variable has these 6 values : " + array_arange); var matrix_3x3 = np.zeros((3, 3)); Console.WriteLine("This matrix_3x3 variable has these values : " + matrix_3x3); Console.WriteLine("Save array to disk"); Console.WriteLine("NumSharp cannot save arrays has more than int byteMaxSize = 2_147_483_591 elements"); NDArray disk_array = new double[]{1,2,3}; disk_array.ToString(); np.Save((Array)disk_array, "my_array.npy"); Console.WriteLine("Load array from disk."); NDArray load_array = np.Load<double[]>("my_array.npy"); var disk_load_array = load_array.ToString(); Console.WriteLine(disk_load_array); } } } |
The result of this running source code will create a file named my_array.npy and will show this output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | dotnet run Hello World using the NumSharp package! basic definitions This array001 variable has these 6 values : [5, 5, 5, 5, 5, 5] This array_zeros variable has these 6 values : [0, 0, 0, 0, 0, 0] This array_arange variable has these 6 values : [0, 1, 2, 3, 4, 5] This matrix_3x3 variable has these values : [[0, 0, 0], [0, 0, 0], [0, 0, 0]] Save array to disk NumSharp cannot save arrays has more than int byteMaxSize = 2_147_483_591 elements Load array from disk. [1, 2, 3] |