Skip to content

failed to use "where" in Numpy.NET #128

Closed

Description

@yiJiangHen

In python, we write like this when using "where":

grid=np.array([[1,2,3,0,0,4],[5,6,7,0,0,8]])
xs, ys = np.where(grid == 0)

but I don't know how to use "where" in Numpy.NET. I wrote like this but it proves to be wrong:

var array = new int[2, 6] { { 1, 2, 3, 0, 0, 4 },{ 5, 6, 7, 0, 0, 8 } };
var a = np.array(array);
var b = np.where(np.array(a==np.array(0)));
Console.WriteLine(b[0]);

Activity

  1. henon commented on Jun 22, 2024

    @henon
    Contributor

    Python does some magic by converting == to equals(...). Here is how you write the same in C#: np.where(grid.equals(0));

            [TestMethod]
            public void IssueByPandath()
            {
                //>>> grid = np.array([[1, 2, 3, 0, 0, 4], [5, 6, 7, 0, 0, 8]])
                //>>> xs, ys = np.where(grid == 0)
                //>>> xs
                //array([0, 0, 1, 1], dtype = int64)
                //>>> ys
                //array([3, 4, 3, 4], dtype = int64)
    
                var grid = np.array(new[,] {{ 1, 2, 3, 0, 0, 4 }, { 5, 6, 7, 0, 0, 8 } });
                var result = np.where(grid.equals(0));
                Console.WriteLine(result[0].repr);
                Console.WriteLine(result[1].repr);
                Assert.AreEqual("array([0, 0, 1, 1], dtype=int64)", result[0].repr);
                Assert.AreEqual("array([3, 4, 3, 4], dtype=int64)", result[1].repr);
            }
  2. added a commit that references this issue on Jun 22, 2024
  3. yiJiangHen commented on Jun 24, 2024

    @yiJiangHen
    Author

    Thank you, dear henon! You saved my day.

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