NumSharp — Numerical .NET

Haiping Chen
SciSharp STACK
Published in
2 min readNov 8, 2018

--

NumPy is the most basic and a powerful package for working with data in python. If you are going to work on data analysis or machine learning projects, then having a solid understanding of numpy is nearly mandatory. Other packages for data analysis (like pandas) is built on top of numpy and the scikit-learn package which is used to build machine learning applications works heavily with numpy as well. But for .NET developers, there is no such tool library. Although there are open source libraries like Deedle and Math.NET, they are not very easy to use and cannot borrow a lot of existing python code.

NumSharp (Numerical .NET) is a linear algebra library in C#. It is written in C# and is compliant with the .net standard 2.0 library. It’s goal is to allow .NET developers to write machine learning code using NumPy’s syntax, minimizing the cost of migrating python code. NumSharp uses the latest Span technology to securely access memory directly, optimizing performance for each simulation API. NumSharp is very useful for performing mathematical and logical operations on Arrays. It provides an abundance of useful features for operations on n-arrays and matrices in .NET.

Let’s give a code snippet to illustrate how NumSharp is used.

// init a np instance
var np = new NumPy<int>();
// generate a vector with number 0-9
np.arange(10)
// generate a 3-dimension tensor
np.arange(12).reshape(2, 3, 2);
// generate 10 random number and convert to 5 * 5 matrix.
np.random.randint(low: 0, high: 10, size: new Shape(5, 5));

Is the above code very close to the python code? It’s just like twins. The purpose of NumSharp is to make it easy to copy and paste Python code into C#.

How to install:

PM> Install-Package NumSharp

If you feel that this library is helpful to you, please actively participate in the discussion. Welcome to star me here on Github, there is also a sample code which is Artificial Neural Network implementation in NumSharp from scratch.

Without NumSharp I only think of Python to write a machine learning project but while i am familiar with C#. From now, this is good point to start a new ML project with C# dotnet. Thank you all!

--

--