Skip to content

how to make NumSharp.NDArray from Numpy.NDarray? #416

Closed

Description

@djagatiya
No description provided.

Activity

  1. changed the title [-]is there any way to convert NumSharp.NDArray to Numpy.NDarray?[/-] [+]how to make NumSharp.NDarray from Numpy.NDarray?[/+] on Jul 6, 2020
  2. changed the title [-]how to make NumSharp.NDarray from Numpy.NDarray?[/-] [+]how to make NumSharp.NDArray from Numpy.NDarray?[/+] on Jul 6, 2020
  3. djagatiya commented on Jul 7, 2020

    @djagatiya
    Author

    I found a solution, but is this the right way to do it?

    Numpy.NDarray numpyArray = Numpy.np.random.randn(224,224,3);
    byte[] v = numpyArray.GetData<byte>();
    fixed (byte* packet = v)
    {
        var block = new UnmanagedMemoryBlock<byte>(packet, v.Length);
        var storage = new UnmanagedStorage(new ArraySlice<byte>(block), numpyArray.shape.Dimensions);
        NumSharp.NDArray numSharpArray = new NumSharp.NDArray(storage);
    }
    
  4. Nucs commented on Sep 7, 2026

    @Nucs
    Member

    @djagatiya, your manual UnmanagedMemoryBlock approach worked, but the new NumSharp.Interop.pythonnet package (0.70.0, #628) makes it a one-liner, zero-copy (strided/offset included, read-only stays read-only).

    Set up the Python engine once at startup - either point pythonnet at an installed Python, or deploy one from NuGet with Python.Included (no system install needed):

    using NumSharp;
    using NumSharp.Interop.PythonNet;
    using Python.Runtime;
    
    // Option A - existing Python: the PYTHONNET_PYDLL env var, or the DLL directly
    Runtime.PythonDLL = "python312.dll";   // or Environment.GetEnvironmentVariable("PYTHONNET_PYDLL")
    PythonEngine.Initialize();
    
    // Option B - deploy an embedded Python from NuGet (dotnet add package Python.Included)
    await Python.Included.Installer.SetupPython();
    await Python.Included.Installer.PipInstallModule("numpy");
    PythonEngine.Initialize();

    Explicit:

    using (Py.GIL())
    {
        dynamic np_ = Py.Import("numpy");
        PyObject npArr = np_.arange(4);   // a numpy array
    
        NDArray nd = npArr.AsNDArray();    // -> NumSharp, no manual UnmanagedMemoryBlock
        Console.WriteLine(nd);             // [0 1 2 3]
    }

    Implicit (register the codec once, then use pythonnet's own As<NDArray>()):

    NDArrayPythonInterop.RegisterCodec();   // once per process
    
    using (Py.GIL())
    {
        dynamic np_ = Py.Import("numpy");
        PyObject npArr = np_.arange(4);
    
        NDArray nd = npArr.As<NDArray>();   // implicit decode via the codec
        Console.WriteLine(nd);              // [0 1 2 3]
    }

    Requires NumSharp.Interop.pythonnet (dotnet add package NumSharp.Interop.pythonnet). Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions