ScikitLearn 1.1.0

dotnet add package ScikitLearn --version 1.1.0                
NuGet\Install-Package ScikitLearn -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ScikitLearn" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ScikitLearn --version 1.1.0                
#r "nuget: ScikitLearn, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ScikitLearn as a Cake Addin
#addin nuget:?package=ScikitLearn&version=1.1.0

// Install ScikitLearn as a Cake Tool
#tool nuget:?package=ScikitLearn&version=1.1.0                

Sklearn.Net

alternate text is missing from this package README image alternate text is missing from this package README image

C# bindings for Scikit-Learn, focused on bringing Machine Learning to the C# environment. This library provides easy access to machine learning models, results, parameters and datasets.

Powered by Numpy.Bare and generated based on Scikit-Learn’s documentation, implementing most of the classes and methods.

🔧 Installation

If Python is Already Installed

Set the path to your python311.dll file as follows:

Runtime.PythonDLL = "your_path_to_python311.dll";

Replace "your_path_to_python311.dll" with the actual path where python311.dll is located on your system. This setting allows Sklearn.NET to use your existing Python installation directly.

For a Local Python Installation

If you prefer to install Python locally, you’ll need to first install Python.Included Nuget v3.11.6. Then, add the following code to your program’s startup to install and configure the local Python installation:

using Python.Included;
using Python.Runtime;
using Numpy;
using ScikitLearn;
internal class Program
{
    // Define an asynchronous method to install numpy and scikit-learn; this may take a few minutes the first time.
    // Once installed, startup will be almost immediate. You can also copy the installation to a fixed location
    // for multiple projects to find it instantly
    private static async Task InitializeInstallerAsync()
    {
        Installer.InstallPath = Path.GetFullPath("."); // Specify the path for the local installation
    
        await Installer.SetupPython();
        await Installer.TryInstallPip();
        await Installer.PipInstallModule("numpy");
        await Installer.PipInstallModule("scikit-learn");
    }

    // Run this task in your application’s constructor or at the start of your code,
    // just ensure it runs before calling np or sklearn
    public static void Main(string[] args)
    {
        Task.Run(InitializeInstallerAsync).Wait();

        // Your code here
    }
}

⚙ Usage

Replicating the DBSCAN example:

var X = np.array(new int[,] {
    { 1, 2 }, { 2, 2 }, { 2, 3 },
    { 8, 7 }, { 8,8 }, { 25, 25 } });

var clustering = new sklearn.cluster.DBSCAN(eps: 3, min_samples: 2).fit(X);
Console.WriteLine(clustering.labels_);

Output

[ 0  0  0  1  1 -1]

💻 Full code here

⚠ Notes

1. Getting an Array from my_model.labels_

ScikitLearn typically uses ndarray(int64), which is equivalent to long[] in C# rather than int[]. To convert labels_ to a C# array format, use:

long[] labels = my_model.labels_.GetData<long>();

2. PythonEngine.Shutdown()

For desktop applications, be sure to call PythonEngine.Shutdown() when closing the application to prevent it from continuing in the background.

3. Can't Find the Methods or Constructors You Need

Each static class has a self field of type PyObject, from which you can create class instances or call omitted methods.
When creating objects, you will receive an instance of PyObject. If you are sure of the type, each class has a static method Encapsulate(PyObject pyObject), which allows you to access the class's attributes and methods.

PyObject obj = sklearn.cluster.self.InvokeMethod("DBSCAN", your_custom_args);

var model = sklearn.cluster.DBSCAN.Encapsule(obj);
model.fit(...);

Desktop Applications

This project originated from the need to use classification algorithms within the UI benefits offered by C#, such as WinForms and WPF, for developing complex applications. Here’s a small example of an application that interactively compares different clustering types: DBSCAN, OPTICS, and Mean Shift using the ScottPlot graphics library.

alternate text is missing from this package README image

💻 Full code here

Final Notes

This project is just starting, so some parts may still lack full implementation or proper error handling. However, I’ll be adding tests to verify return types and improve typing accuracy.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 50 11/22/2024
1.0.0 79 11/17/2024
0.0.1 73 11/13/2024