• Stars
    star
    1,362
  • Rank 34,505 (Top 0.7 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.

NumSharp

NuGet Join the chat at https://gitter.im/publiclab/publiclab AppVeyor codecov Badge

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#):

comparision

Bold Features

  • Use of Unmanaged Memory and fast unsafe algorithms.
  • Broadcasting n-d shapes against each other. (intro)
  • NDArray Slicing and nested/recusive slicing (nd["-1, ::2"]["1::3, :, 0"])
  • Axis iteration and support in all of our implemented functions.
  • Full and precise (to numpy) automatic type resolving and conversion (upcasting, downcasting and other cases)
  • Non-copy - most cases, similarly to numpy, does not perform copying but returns a view instead.
  • Almost non-effort copy-pasting numpy code from python to C#.
  • Wide support for System.Drawing.Bitmap. (read more)

Implemented APIs

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.

Install NumSharp in NuGet

PM> Install-Package NumSharp

How to use

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.
}

How to run benchmark

C: \> dotnet NumSharp.Benchmark.dll nparange

NumSharp is referenced by

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.

Regen Templating

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.

More Repositories

1

TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
C#
3,224
star
2

LLamaSharp

A C#/.NET library to run LLM (πŸ¦™LLaMA/LLaVA) on your local device efficiently.
C#
2,508
star
3

BotSharp

The AI Agent Framework in .NET
C#
2,166
star
4

Numpy.NET

C#/F# bindings for NumPy - a fundamental library for scientific computing, machine learning and AI
C#
685
star
5

Keras.NET

Keras.NET is a high-level neural networks API for C# and F#, with Python Binding and capable of running on top of TensorFlow, CNTK, or Theano.
C#
594
star
6

Pandas.NET

Pandas port for C# and F#, data analysis tool, process multi-dim array in DataFrame.
C#
569
star
7

SiaNet

An easy to use C# deep learning library with CUDA/OpenCL support
C#
380
star
8

SciSharp-Stack-Examples

Practical examples written in SciSharp's machine learning libraries
C#
318
star
9

SharpCV

A Computer Vision library for C# and F# that combines OpenCV and NDArray together in .NET Standard.
C#
292
star
10

Torch.NET

.NET bindings for PyTorch. Machine Learning with C# / F# with Multi-GPU/CPU support
C#
276
star
11

Gym.NET

openai/gym's popular toolkit for developing and comparing reinforcement learning algorithms port to C#.
C#
120
star
12

CherubNLP

Natural Language Processing in .NET Core
C#
114
star
13

SciSharp

SciSharp STACK is focused on building tools for Machine Learning development.
Vue
105
star
14

SciSharpCube

Quickly experience all the latest features of SciSharp Machine Learning tools in docker container.
Jupyter Notebook
95
star
15

BotSharp-UI

Build, test and manage your AI Agents in the central place.
SCSS
82
star
16

ICSharpCore

Jupyter kernel in C# .NET Core which is the standard interface for SciSharp STACK.
C#
76
star
17

Plot.NET

.NET wrapper of plotly.js for ICSharpCore
C#
63
star
18

Bigtree.Algorithm

Machine Learning algorithm library in .NET Core
C#
62
star
19

Tensor.NET

A lightweight and high-performance tensor library which provides numpy-like operations but .NET style interfaces. It supports generic tensor, Linq, C# native slices and so on. (Qushui student project))
C#
59
star
20

Matplotlib.Net

.NET wrapper for the Python plotting library Matplotlib
C#
53
star
21

dotnet-mysql-replication

C# Implementation of MySQL replication client
C#
49
star
22

scikit-learn.net

Machine Learning in .NET Core.
C#
39
star
23

CodeMinion

A code generator framework capable of auto-generating the APIs of several SciSharp libraries.
C#
34
star
24

OpenAIGym.NET

A toolkit for developing and comparing reinforcement learning algorithms.
C#
27
star
25

Ludwig.NET

Ludwig is a toolbox that allows to train and test deep learning models without the need to write code.
C#
26
star
26

SciSharp.Models

Image Classification, Time Series, Transformer, Object Detection
Jupyter Notebook
25
star
27

Microcharts.Matplotlib

Microcharts.Matplotlib is a wrapper of Microcharts for Data Science and Machine Learning
C#
22
star
28

protobuf.Text

Text format support for protobuf
C#
21
star
29

GGMLSharp

Use GGML with C#/.NET
C#
19
star
30

SharpPythonCompiler

A compiler which can transform the convention of C# code to the convention of Python
C#
19
star
31

SpaCy.NET

.NET wrapper of spaCy (Industrial-strength NLP)
18
star
32

tensorflow-net-docs

Tensorflow.NET documentation
C#
16
star
33

unity-ml-agents.net

C#
15
star
34

NumSharp.Lite

NumSharp compact version without full datatype supported.
C#
11
star
35

TensorFlow.NET.OpencvAdapter

A library which enables using tensorflow.net with opencvsharp. It reuses the memory to provide a good performance.
C#
9
star
36

PillowSharp

The friendly PIL fork (Python Imaging Library) in C#.
9
star
37

SciSharpStudio

SciSharp Studio is a web based AI/ ML development tool.
HTML
6
star
38

qdrant-csharp

Qdrant .NET Client
C#
6
star
39

TensorDebuggerVisualizers

The Sheet Viewer, which provides instant view of the contents of the sheet when debugging.
C#
5
star
40

ChillXML

2
star