ScikitLearn 1.7.1-rc1

This is a prerelease version of ScikitLearn.
dotnet add package ScikitLearn --version 1.7.1-rc1
                    
NuGet\Install-Package ScikitLearn -Version 1.7.1-rc1
                    
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.7.1-rc1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ScikitLearn" Version="1.7.1-rc1" />
                    
Directory.Packages.props
<PackageReference Include="ScikitLearn" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ScikitLearn --version 1.7.1-rc1
                    
#r "nuget: ScikitLearn, 1.7.1-rc1"
                    
#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.
#:package ScikitLearn@1.7.1-rc1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ScikitLearn&version=1.7.1-rc1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ScikitLearn&version=1.7.1-rc1&prerelease
                    
Install as a Cake Tool

ScikitLearn.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 into the C# environment. This library provides easy access to machine learning models, results, parameters, and datasets.

Built on top of Numpy.Bare and made from Scikit-Learn’s documentation, supporting most classes and methods.

📘 Leer en Español 📗 Read in English 📙 Ler em Português BR

🔧 Installation

If You Already Have Python 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 to python311.dll on your system. This allows Sklearn.NET to use your existing Python installation.

Installing Python Locally

If you prefer a local Python installation, install Python.Included NuGet package (version 3.11.6) and then add the following code to initialize the environment:

using Python.Included;
using Python.Runtime;
using Numpy;
using ScikitLearn;
internal class Program
{
    // 🚨 WARNING: This method REQUIRES an internet connection and may take several minutes 
    // on the first attempt. It can also FAIL on the first tries.
    // ✅ RECOMMENDATION: Once the Python folder is created, reuse it across multiple projects 
    // to avoid this delay in the future.

    // This asynchronous method installs NumPy and Scikit-learn.
    private static async Task InitializeInstallerAsync()
    {
        // Set the desired installation path
        Installer.InstallPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    
        await Installer.SetupPython();
        await Installer.TryInstallPip();
        await Installer.PipInstallModule("numpy");
        await Installer.PipInstallModule("scikit-learn");
    }

    public static void Main(string[] args)
    {
        Task.Run(InitializeInstallerAsync).Wait();

        // Your code here
    }
}

⚠️ Note: The installation process requires an active internet connection and may take several minutes on the first run. It might also fail on initial attempts.

✅ Recommendation: Once the Python folder is created, reuse it across multiple projects to avoid this delay.

🖥️ [Console Example]

💻 [Desktop Example]

⚙ Usage

Example replicating DBSCAN:

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]

💻 [See full code]

⚠ Notes

1. 🚨 Accessing the labels_ Array or Floating-Point Arrays 🚨

Scikit-Learn typically uses ndarray(int64), which corresponds to long[] in C#. Use the following to extract data:

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

If you need to convert floating-point arrays to a C# format, be extremely careful with type conversion!

var x = sklearn.datasets.make_circles(n_samples: 1).X;
Console.WriteLine(x.GetData<float>()[0]);
Console.WriteLine(x.GetData<double>()[0]);
-1.5881868E-23 // using float  [ERROR]
0.8            // using double [OK]

In the future, expressions of the form NDarray<double> are planned to be returned where appropriate instead of NDarray. In the meantime, make sure to unit test and verify that the results match expectations.

2. PythonEngine.Shutdown()

For desktop apps, always call PythonEngine.Shutdown() when the app closes to avoid Python processes running in the background.

💻 [Desktop Example]

3. Missing Methods or Constructors?

Each static class contains a self field of type PyObject, which you can use to create instances or call missing methods. If you create an object and are confident of its type, use the static Wrap(PyObject pyObject) method to convert it into a typed wrapper:

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

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

💻 Desktop Applications

This project was born from the need to use classification algorithms within the rich UI features of C#, such as WinForms and WPF, for building complex applications. Here’s a simple app that interactively compares clustering methods like DBSCAN, OPTICS, and Mean Shift using the ScottPlot graphing library:

alternate text is missing from this package README image

💻 [Full code here]

🤝 How to Contribute

To make the project easier to maintain, all class and method declarations are located in the ScikitLearn.Signatures project.

If a method, constructor, or class has incorrect parameters, correct or implement it there. Then run the CodeGenerator to apply changes.

📝 Final Notes

This project is still in early development, so some parts may be incomplete or lack error handling. Work is ongoing to add tests that verify return types and improve type safety.

Licenses

This project is licensed under the MIT License.

It wraps and makes use of the Scikit-Learn project, which is licensed under the BSD 3-Clause License.
See LICENSE.scikit-learn for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.7.1-rc1 91 7/29/2025
1.1.0 264 11/22/2024
1.0.0 125 11/17/2024
0.0.1 123 11/13/2024