NumSharp (NS) is a NumPy port to C# targetting .NET Standard.
NumSharp is the fundamental package needed for scientific computing with C# and F#.
Is it difficult to translate python machine learning code into .NET? Because too many functions can’t be found in the corresponding code in the .NET SDK. NumSharp is the C# version of NumPy, which is as consistent as possible with the NumPy programming interface, including function names and parameter locations. By introducing the NumSharp tool library, you can easily convert from python code to C# or F# code. Here is a comparison code between NumSharp and NumPy (left is python, right is C#):
nd["-1, ::2"]["1::3, :, 0"]
)System.Drawing.Bitmap
. (read more)The NumPy class is a high-level abstraction of NDArray that allows NumSharp to be used in the same way as Python's NumPy, minimizing API differences caused by programming language features, allowing .NET developers to maximize Utilize a wide range of NumPy code resources to seamlessly translate python code into .NET code.
PM> Install-Package NumSharp
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 shaped (2, 3, 2)
// get the 2nd vector in the 1st dimension
data = nd[1]; //returning ndarray shaped (3, 2)
// get the 3rd vector in the (axis 1, axis 2) dimension
data = nd[1, 2]; //returning ndarray shaped (2, )
// get flat representation of nd
data = nd.flat; //or nd.flatten() for a copy
// interate ndarray
foreach (object val in nd)
{
// val can be either boxed value-type or a NDArray.
}
var iter = nd.AsIterator<int>(); //a different T can be used to automatically perform cast behind the scenes.
while (iter.HasNext())
{
//read
int val = iter.MoveNext();
//write
iter.MoveNextReference() = 123; //set value to the next val
//note that setting is not supported when calling AsIterator<T>() where T is not the dtype of the ndarray.
}
C: \> dotnet NumSharp.Benchmark.dll nparange
You might also be interested in NumSharp's sister project Numpy.NET which provides a more whole implementation of numpy by using pythonnet and behind-the-scenes deployment of python (read more).
NumSharp is a member project of SciSharp.org which is the .NET based ecosystem of open-source software for mathematics, science, and engineering.
Our library contains over 150,000 lines of repetitive generated code, mostly for handling different data types without hurting performance.
The templates can be recognized with #if _REGEN
blocks and are powered by Regen Templating Engine.
Regen is a powerful external tool (Visual studio extension, download here) that generates on demand based on a C#-like regen-lang
.