question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can numpy.net convert ndarray to Array type?

See original GitHub issue

If ndarray has N dimensions, it will be very troublesome to restore it to T[,]. Maybe numpy.net can use implicit operators to convert ndarray to Array type. https://learn.microsoft.com/en-us/dotnet/api/system.array?view=net-7.0 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

This way I can easily convert ndarray to torch.tensor🤣 https://github.com/dotnet/TorchSharp/blob/c55e9f535aa46c4dfa104d839019553b693616c0/src/TorchSharp/Tensor/Tensor.Factories.cs#L2654

int[,,] a = new int[3, 5, 7];
Array b = a;
Tensor c = as_tensor(b);
Console.WriteLine(c);

// [3x5x7], type = Int32, device = cpu

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:12

github_iconTop GitHub Comments

3reactions
KevinBaselineswcommented, Dec 10, 2022

I figured out how to use your sample function. It does eliminate several hundred lines of redundant code. See it in the new release.

thank you!

1reaction
KevinBaselineswcommented, Dec 7, 2022

I did not get an email on this comment like I usually do. It is lucky that I checked these boards this morning.

Internally, Numpy stores all arrays as single dimension arrays. When you add a shape to it, you are just adjusting some internal values that make it appear to be multi-dimensional.

Are you suggesting that we add something like this to the library? It would have to be expanded to include all 16 datatypes. If we make it work for 5 dimensions, that is 64 separate explicit operators.

Can you think of an easier way to do this?

  public static explicit operator int[,](ndarray nd)
      {
          if (nd.ndim != 2)
              throw new Exception("ndarray does not have 2 dimensions");

          // todo: convert nd to int[,] array

          return new int[,] { { 0 }, { 1 } };
      }

      public static explicit operator int[,,] (ndarray nd)
      {
          if (nd.ndim != 3)
              throw new Exception("ndarray does not have 3 dimensions");

          // todo: convert nd to int[,,] array

          return new int[,,] { { { 1, 2 }, { 3, 4 }, { 5, 6 } } };
      }

      public static explicit operator int[,,,] (ndarray nd)
      {
          if (nd.ndim != 4)
              throw new Exception("ndarray does not have 4 dimensions");

          // todo: convert nd to int[,,,] array

          return new int[,,,] { { { { 1, 2 }, { 3, 4 }, { 5, 6 } } } };
      }

      public static explicit operator double[,,,] (ndarray nd)
      {
          if (nd.ndim != 4)
              throw new Exception("ndarray does not have 4 dimensions");

          // todo: convert nd to double[,,,] array

          return new double[,,,] { { { { 1, 2 }, { 3, 4 }, { 5, 6 } } } };
      }

      public static explicit operator System.Numerics.Complex[,,,] (ndarray nd)
      {
          if (nd.ndim != 4)
              throw new Exception("ndarray does not have 4 dimensions");

          // todo: convert nd to System.Numerics.Complex[,,,] array

          return new System.Numerics.Complex[,,,] { { { { 1, 2 }, { 3, 4 }, { 5, 6 } } } };
      }


The code to call them would look like this:

  [TestMethod]
      public void test_AsMultiDimension()
      {
          ndarray a = np.array(new int[,] { { 0 }, { 1 } } );
          AssertArray(a, new int[,] { { 0 }, { 1 } });

          int[,] b = (int[,])a;
          AssertArray(a, b);


          ndarray c = np.array(new int[,,] { { { 1, 2 }, { 3, 4 }, { 5, 6 } } });
          AssertArray(c, new int[,,] { { { 1, 2 }, { 3, 4 }, { 5, 6 } } });
          int[,,] d = (int[,,])c;
          AssertArray(c, d);

      }
Read more comments on GitHub >

github_iconTop Results From Across the Web

HOW to change NDArray to C#'s int[] · Issue #40
To work with data from C# in Numpy it has to be copied into the Python engine by using np.array(...) . You get...
Read more >
numpy.ndarray.astype — NumPy v1.25 Manual
Copy of the array, cast to a specified type. Parameters: dtypestr or dtype. Typecode or data-type to which the array is cast. order{'C',...
Read more >
c# - Numpy.NET Getting Values
The problem is, I can't convert my NDarrays to C# arrays to use the values. I tried this method (in README - section...
Read more >
SciSharp/Numpy.NET: C#/F# bindings for ...
NET: C#/F# bindings for NumPy - a fundamental library for scientific computing ... The alternative is to cast value types to an array...
Read more >
Change data type of given numpy array
Solution : We will use numpy.astype() function to change the data type of the underlying data of the given numpy array.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found