Giter VIP home page Giter VIP logo

Comments (4)

henon avatar henon commented on August 11, 2024

I extended the documentation with an example:

Create a Numpy array from a C# array and vice versa

To work with data from C# in Numpy it has to be copied into the Python engine by using np.array(...). You get an NDarray that you can use for further processing of the data. Here we calculate the square root:

// create an NDarray from a C# array
var a = np.array(new[] { 2, 4, 9, 25 });
Console.WriteLine("a: "+ a.repr);
// a: array([ 2,  4,  9, 25])
// apply the square root to each element
var roots = np.sqrt(a);
Console.WriteLine(roots.repr);
// array([1.41421356, 2.        , 3.        , 5.        ])

After processing the data you can copy it back into a C# array use a.GetData<int>(), but be aware of the datatype of the NDarray in order to get correct values back:

// Copy the NDarray roots into a C# array from NDarray (incorrect datatype)
Console.WriteLine(string.Join(", ", roots.GetData<int>()));
// 1719614413, 1073127582, 0, 1073741824 
Console.WriteLine("roots.dtype: " + roots.dtype);
// roots.dtype: float64
Console.WriteLine(string.Join(", ", roots.GetData<double>()));
// 1.4142135623731, 2, 3, 5

from numpy.net.

rsinghbu-65 avatar rsinghbu-65 commented on August 11, 2024

How do we do this forint[,]?
I get Can not copy the data with data type due to limitations of Marshal.Copy: Int32[]

from numpy.net.

henon avatar henon commented on August 11, 2024

yeah, it doesn't work. you need to use int[] and calculate the index position yourself when accessing the data in c#. if you want help you can use my library SliceAndDice which allows you to reshape in c# and use like a numpy array.

from numpy.net.

henon avatar henon commented on August 11, 2024

https://github.com/henon/SliceAndDice

from numpy.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.