Skip to content

KosmosWerner/ScikitLearn.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sklearn.NET

(Work In Progress)

A C# binding 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

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>();

PythonEngine.Shutdown()

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

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.

DemoWPF.mp4

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.

About

C# bindings for Scikit-Learn (WIP 🏗)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages