Blog Logo

NumSharp - The Fundamental Package for Scientific Computing with C# and F#

NumSharp (NS) is a NumPy port to C# targeting .NET Standard. It provides a fundamental package for scientific computing with C# and F#. With NumSharp, it is no longer difficult to translate Python machine learning code into .NET. NumSharp is the C# version of NumPy, which is as consistent as possible with the NumPy programming interface, including function names and parameter locations. The package offers a wide range of features, including use of unmanaged memory and fast unsafe algorithms, broadcasting n-d shapes against each other, NDArray slicing and nested/recursive slicing, axis iteration and support in all of our implemented functions, full and precise automatic type resolving and conversion, non-copy almost all the time, wide support for System.Drawing.Bitmap and more. The APIs are implemented in a high-level abstraction of NDArray that minimizes API differences caused by programming language features, allowing .NET developers to maximize utilize a wide range of NumPy code resources. NumSharp can easily convert Python code to C# or F# code. The package can be installed in NuGet by using the command Install-Package NumSharp. Here is a sample code for using NumSharp package in C#: using NumSharp; var nd = np.full(5, 12); //[5, 5, 5 .. 5] nd = np.zeros(12); //[0, 0, 0 .. 0] nd = np.arange(12); //[0, 1, 2 .. 11] // create a matrix nd = np.zeros((3, 4)); //[0, 0, 0 .. 0] nd = np.arange(12).reshape(3, 4); // access data by index var data = nd[1, 1]; // create a tensor nd = np.arange(12); // reshaping data = nd.reshape(2, -1); //returning ndarray shaped (2, 6) Shape shape = (2, 3, 2); data = nd.reshape(shape); //Tuple implicitly casted to Shape //or: nd = nd.reshape(2, 3, 2); // slicing tensor data = nd[:, 0, :]; //returning ndarray shaped (2, 1, 2) data = nd[Slice.All, 0, Slice.All]; //equivalent to the line above. // nd is currently the reshaped tensor as (2, 3, 2).