NumSharp is an open-source library for .NET designed to facilitate numerical computing and data analysis, emulating the functionality of NumPy, one of the most popular libraries in the Python ecosystem.
NumSharp provides a simple API for manipulating arrays, performing mathematical calculations, and working with numerical data in .NET applications.
Features of NumSharp:
- N-dimensional Arrays: Support for multidimensional arrays, similar to NumPy arrays.
- Mathematical Operations: Functions for linear algebra, statistics, and general mathematical operations.
- Interoperability with ML.NET: Facilitates integration with ML.NET for machine learning model development.
- Compatibility: Works in .NET Core and .NET Framework projects.
Installing NumSharp
You can install NumSharp via the NuGet package manager. Open your terminal or the NuGet Package Manager console in Visual Studio and run the following command:
Install-package NumSharp
Or through the NuGet interface in Visual Studio, search for NumSharp
and install it in your project.
How to Use NumSharp
Creating Arrays and Matrices
NumSharp provides an NDArray
class for working with multidimensional arrays. Here is a basic example of how to create and manipulate arrays:
using NumSharp;
class Program
{
static void Main(string[] args)
{
// Create a one-dimensional array
var array1D = np.array(new int[] { 1, 2, 3, 4, 5 });
// Create a two-dimensional array
var array2D = np.array(new int[,] { { 1, 2 }, { 3, 4 } });
// Display the arrays
Console.WriteLine("Array 1D:");
Console.WriteLine(array1D.ToString());
Console.WriteLine("Array 2D:");
Console.WriteLine(array2D.ToString());
}
}
Array 1D:
[1, 2, 3, 4, 5]
Array 2D:
[[1, 2],
[3, 4]]
Basic Mathematical Operations
NumSharp includes a series of useful mathematical functions for basic operations on arrays and matrices:
using NumSharp;
class Program
{
static void Main(string[] args)
{
var array = np.array(new double[] { 1, 2, 3, 4, 5 });
// Mathematical operations
var squared = np.power(array, 2); // Raises each element to the power of two
var sum = np.sum(array); // Sums all elements
Console.WriteLine("Original Array:");
Console.WriteLine(array.ToString());
Console.WriteLine("Squared Array:");
Console.WriteLine(squared.ToString());
Console.WriteLine("Sum of Elements:");
Console.WriteLine(sum.ToString());
}
}
Data Manipulation
NumSharp allows for complex operations on matrices, such as transposition, reshaping, and advanced mathematical operations:
using NumSharp;
class Program
{
static void Main(string[] args)
{
// Create a 3x3 matrix
var matrix = np.array(new double[,] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
});
// Transpose the matrix
var transpose = np.transpose(matrix);
// Reshape the matrix to 1x9
var reshape = np.reshape(matrix, new Shape(1, 9));
Console.WriteLine("Original Matrix:");
Console.WriteLine(matrix.ToString());
Console.WriteLine("Transposed Matrix:");
Console.WriteLine(transpose.ToString());
Console.WriteLine("Reshaped Matrix:");
Console.WriteLine(reshape.ToString());
}
}
Integration with ML.NET
NumSharp is compatible with ML.NET, making it easier to work with numerical data in the context of machine learning:
using NumSharp;
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// Create a feature array
var features = np.array(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0 });
// Use NumSharp to prepare data for ML.NET
var normalizedFeatures = (features - np.min(features)) / (np.max(features) - np.min(features));
Console.WriteLine("Normalized Features:");
Console.WriteLine(normalizedFeatures.ToString());
}
}
For more information and to explore the complete documentation, visit the official NumSharp repository on GitHub. Here you will find additional examples, tutorials, and resources.