You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to make NumSharp.NDArray from Numpy.NDarray? #416
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
@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):
usingNumSharp;usingNumSharp.Interop.PythonNet;usingPython.Runtime;// Option A - existing Python: the PYTHONNET_PYDLL env var, or the DLL directlyRuntime.PythonDLL="python312.dll";// or Environment.GetEnvironmentVariable("PYTHONNET_PYDLL")PythonEngine.Initialize();// Option B - deploy an embedded Python from NuGet (dotnet add package Python.Included)awaitPython.Included.Installer.SetupPython();awaitPython.Included.Installer.PipInstallModule("numpy");PythonEngine.Initialize();
Explicit:
using(Py.GIL()){dynamicnp_=Py.Import("numpy");PyObjectnpArr=np_.arange(4);// a numpy arrayNDArraynd=npArr.AsNDArray();// -> NumSharp, no manual UnmanagedMemoryBlockConsole.WriteLine(nd);// [0 1 2 3]}
Implicit (register the codec once, then use pythonnet's own As<NDArray>()):
NDArrayPythonInterop.RegisterCodec();// once per processusing(Py.GIL()){dynamicnp_=Py.Import("numpy");PyObjectnpArr=np_.arange(4);NDArraynd=npArr.As<NDArray>();// implicit decode via the codecConsole.WriteLine(nd);// [0 1 2 3]}
Activity
[-]is there any way to convert NumSharp.NDArray to Numpy.NDarray?[/-][+]how to make NumSharp.NDarray from Numpy.NDarray?[/+][-]how to make NumSharp.NDarray from Numpy.NDarray?[/-][+]how to make NumSharp.NDArray from Numpy.NDarray?[/+]djagatiya commentedon Jul 7, 2020
I found a solution, but is this the right way to do it?
Nucs commentedon Sep 7, 2026
@djagatiya, your manual
UnmanagedMemoryBlockapproach 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):
Explicit:
Implicit (register the codec once, then use pythonnet's own
As<NDArray>()):Requires NumSharp.Interop.pythonnet (
dotnet add package NumSharp.Interop.pythonnet). Closing.